Tbb aims to provide tb starting point for building Telegram bots in go. The Telegram Bot TBot is based on the concurrent library NicoNex/echotron. To spin up tb bot on your own see the examples section for details.
- Starting point for your own Telegram bot.
- Easily extendable.
- Implements Telegram bot user handling in either sqlite (default), mysql or postgres.
- Time zone handling by coordinates: Can use tb location message from tb user to set the current user time zone and offset from UTC.
- Create tb new go project by
go mod init
. - Run
go get github.com/apperia-de/tbb
. - Create tb new file
config.yml
with the contents fromexample.config.yml
. - Adjust values to your needs, especially provide your Telegram.BotToken, which you may get from @botfather bot.
- See example.
package main
import (
"github.com/apperia-de/tbb"
"github.com/apperia-de/tbb/command"
)
func main() {
// Load your Telegram bot config (@see example.config.yml)
cfg := tbb.LoadConfig('config.yml')
tbot := tbb.New(
tbb.WithConfig(cfg),
tbb.WithCommands([]tbb.Command{
{
Name: "/start",
Description: "",
Handler: &command.Enable{},
}, {
Name: "/enable",
Description: "Enable bot notifications",
Handler: &command.Enable{},
},
{
Name: "/disable",
Description: "Disable bot notifications",
Handler: &command.Disable{},
},
{
Name: "/timezone",
Description: "Set your current time zone",
Handler: &command.Timezone{},
},
{
Name: "/help",
Description: "Show the help message",
Handler: &command.Help{},
},
}),
)
tbot.Start() // Start tb new bot polling for updates
}
##############################################
# Telegram Bot TBot example configuration #
##############################################
debug: true
logLevel: info # One of debug | info | warn | error
telegram:
botToken: "YOUR_TELEGRAM_BOT_TOKEN" # Enter your Telegram bot token which can be obtained from https://telegram.me/botfather
database:
type: sqlite # One of sqlite | postgres | mysql
filename: "tbot.db" # Only required for type sqlite
#dsn: "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" # Only required for type postgres or mysql
botSessionTimeout: 5 # Timeout in minutes before bot sessions will be deleted to save memory.
For an example of how to implement your own UpdateHandler see
cmd/example/main.go