Skip to content

apperia-de/tbb

Repository files navigation

Telegram Bot TBot (tbb)

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.

Go Report Card GitHub go.mod Go version GitHub Licence

  • 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.

How to use tbb

  1. Create tb new go project by go mod init.
  2. Run go get github.com/apperia-de/tbb.
  3. Create tb new file config.yml with the contents from example.config.yml.
  4. Adjust values to your needs, especially provide your Telegram.BotToken, which you may get from @botfather bot.
  5. See example.

Example

Telegram bot

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
}

example.config.yml

##############################################
# 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