Wrapper library for github.com/tucnak/telebot to create simple text-based Telegram bots

tbwrap

Wrapper library for github.com/tucnak/telebot to create simple text-based Telegram bots

Installation

go get github.com/enrico5b1b4/tbwrap

Examples

Ping Bot - A bot which responds to your ping with a pong
Greet Bot - A bot which greets you
Todo List Bot - A bot that keeps a todo list for you
Joke Bot - A bot that tells you a joke
Weather Bot - A bot that tells you the weather forecast

Usage

Simple

package main

import (
	"log"

	"github.com/enrico5b1b4/tbwrap"
)

func main() {
	telegramBotToken := "TELEGRAM_BOT_TOKEN"

	botConfig := tbwrap.Config{
		Token: telegramBotToken,
	}
	telegramBot, err := tbwrap.NewBot(botConfig)
	if err != nil {
		log.Fatal(err)
	}

	telegramBot.Handle(`/ping`, func(c tbwrap.Context) error {
		_, err := c.Send("pong!")

		return err
	})
	telegramBot.Start()
}

Handlers

There are three ways you can register handlers to respond to a user's message

Handle

Invokes handler if message matches the text provided

// ...
func main() {
	// ...
	telegramBot.Handle(`/ping`, func(c tbwrap.Context) error {
		// ...
		_, err := c.Send("...")

		return err
	})
	// ...
}

HandleRegExp

Invokes handler if message matches the regular expression provided

// ...
func main() {
	// ...
	telegramBot.HandleRegExp(`\/greet (?P<name>.*)`, func(c tbwrap.Context) error {
		// ...
		_, err := c.Send("...")

		return err
    })
	// ...
}

HandleMultiRegExp

Invokes handler if message matches any of the regular expressions provided

// ...
func main() {
	// ...
	telegramBot.HandleMultiRegExp([]string{
		`\/greet (?P<name>.*)`,
		`\/welcome (?P<name>.*)`,
	}, func(c tbwrap.Context) error {
		// ...
		_, err := c.Send("...")

		return err
    })
	// ...
}

Messages

Read the user's incoming message with c.Text()

// ...

func main() {
	// ...
	telegramBot.HandleRegExp(`\/greet (?P<name>.*)`, func(c tbwrap.Context) error {
		// ...
		fmt.Println(c.Text()) // "/greet Enrico"

		_, err := c.Send("Hello!")

		return err
    })
	// ...
}

Use regular expression named capturing groups to parametrise incoming messages

// ...

type GreetMessage struct {
    Name string `regexpGroup:"name"`
}

func main() {
	// ...
	telegramBot.HandleRegExp(`\/greet (?P<name>.*)`, func(c tbwrap.Context) error {
		message := new(GreetMessage)
		if err := c.Bind(message); err != nil {
			return err
		}

		fmt.Println(message.Name) // "Enrico"

		_, err := c.Send(fmt.Sprintf("Hello %s!", message.Name))

		return err
    })
	// ...
}

Private bot

You can make the bot respond only to certain chats (users or groups) by passing a list of ids.

// ...
func main() {
	// ...
	botConfig := tbwrap.Config{
		Token:  telegramBotToken,
		AllowedChats:  []int{"123456","234567","-999999"}, // only user and group chats with these ids can interact with the bot
	}
	telegramBot, err := tbwrap.NewBot(botConfig)
	if err != nil {
		log.Fatal(err)
	}
	// ...
}
Similar Resources

A fast responsive, machine learning, conversational dialog engine for creating chat bots, written in Go.

chatbot English | 简体中文 项目说明 chatbot 是一个通过已知对话数据集快速生成回答的 Go 问答引擎。比 ChatterBot 快非常多,我们在1.2亿对话上的对比结果是:ChatterBot 回答需要21秒,chatbot 只需要18毫秒。 bot 问答引擎 cli tr

Jan 6, 2023

HellPot is a portal to endless suffering meant to punish unruly HTTP bots.

HellPot is a portal to endless suffering meant to punish unruly HTTP bots.

HellPot Summary HellPot is an endless honeypot based on Heffalump that sends unruly HTTP bots to hell. Notably it implements a toml configuration file

Jan 2, 2023

Automated Trader (at). Framework for building trading bots.

Automated Trader (at). Framework for building trading bots.

Automated Trader (at) Purpose: Framework for building automated trading strategies in three steps: Build your own strategy. Verify it with the backtes

Dec 14, 2022

Make discord bots to drain discriminators

allthebotses make discord bots to drain discriminators a stupid idea You can only make ~5 bots before getting ratelimited, and it gets tougher every t

Dec 12, 2021

Bot that polls activity API for Github organisation and pushes updates to Telegram.

git-telegram-bot Telegram bot for notifying org events Requirements (for building) Go version 1.16.x Setup If you don't have a telegram bot token yet,

Apr 8, 2022

网易云歌词自动生成《捧读》用的 epub, 帮助学习。(GitHub Actions + shortcuts + telegram)

网易云歌词自动生成《捧读》用的 epub, 帮助学习。(GitHub Actions + shortcuts + telegram)

pengdu_helper 网易云歌词自动生成《捧读》用的 epub, 帮助学习。(GitHub Actions + shortcuts + telegram) 如何使用 前提 有访问自由互联网的环境,有 telegram 请自行查询如何写 telegram bot 有《捧读》app fork 或者

Oct 10, 2022

An high level discord interactions wrapper, simple, clean, extensible. Based on outgoing webhooks.

corde corde is a discord webhook callback API wrapper. It's nowhere near prod-ready, but it's honestly usable, and it's open to contributions. Be awar

Oct 13, 2022

Golang Based Account Generator Telegram Bot

Account Generator Bot Account Generator Bot, written in GoLang via gotgbot library. Variables Env Vars - BOT_TOKEN - Get it from @BotFather CHANNEL_ID

Nov 21, 2022

A bot based on Telegram Bot API written in Golang allows users to download public Instagram photos, videos, and albums without receiving the user's credentials.

InstagramRobot InstagramRobot is a bot based on Telegram Bot API written in Golang that allows users to download public Instagram photos, videos, and

Dec 16, 2021
Related tags
🐿️ Revoltgo is a go package for writing bots / self-bots in revolt easily.

Revoltgo Revoltgo is a go package for writing bots / self-bots in revolt easily. NOTE: This package is still under development and not finished. Creat

Oct 15, 2022
A Telegram Repo For Bots Under Maintenance Which Gives Faster Response To Users
A Telegram Repo For Bots Under Maintenance Which Gives Faster Response To Users

Maintenance Bot A Telegram Repo For Bots Under Maintenance Which Gives Faster Response To Users Requests » Report a Bug | Request Feature Table of Con

Mar 21, 2022
Golang telegram bot API wrapper, session-based router and middleware

go-tgbot Pure Golang telegram bot API wrapper generated from swagger definition, session-based routing and middlewares. Usage benefits No need to lear

Nov 16, 2022
The serverless OTP telegram service use telegram as OTP service, and send OTP through webhook

Setup OTP First thing, you need prepare API(webhook) with POST method, the payload format as below { "first_name": "Nolan", "last_name": "Nguyen",

Jul 24, 2022
Dlercloud-telegram-bot - A Telegram bot for managing your Dler Cloud account

Dler Cloud Telegram Bot A Telegram bot for managing your Dler Cloud account. Usa

Dec 30, 2021
Pro-bot - A telegram bot to play around with the community telegram channels

pro-bot ?? Pro Bot A Telegram Bot to Play Around With The Community Telegram Cha

Jan 24, 2022
Library for working with golang telegram client + bot based on tdlib. This library was taken from the user Arman92 and changed for the current version of tdlib.

go-tdlib Golang Telegram TdLib JSON bindings Install To install, you need to run inside a docker container (it is given below) go get -u github.com/ka

Dec 2, 2022
Golang Framework for writing Slack bots

hanu - Go for Slack Bots! The Go framework hanu is your best friend to create Slack bots! hanu uses allot for easy command and request parsing (e.g. w

Oct 24, 2022
its the same idea as bruh-bot, but with golang, and add more bots
its the same idea as bruh-bot, but with golang, and add more bots

bruh-bot but more powerful! requirements python go you can used on mac and linux the idea its really simple, can make a lot of bots with the same task

Jul 7, 2021
A set of mastodon (fediverse) bots written in Go

Mastodon Bot Bots Hagh Hagh reblogs certain toots to itself creating a Hagh page. Users from local instance are able to make a toot hagh. Setup Have g

Sep 17, 2021