Documentation
¶
Overview ¶
Package tbb provides a base for creating custom Telegram bots. It can be used to spin up a custom bot in one minute which is capable of handling bot users via an implemented sqlite database, but can easily be switched to use mysql or postgres instead.
Index ¶
- Constants
- func BuildInlineKeyboardButtonRow(buttons []InlineKeyboardButton) []echotron.InlineKeyboardButton
- func GetUserFromUpdate(u *echotron.Update) echotron.User
- func PrintAsJson(v any, indent bool) string
- type App
- func (a *App) API() echotron.API
- func (a *App) Config() *Config
- func (a *App) DB() *DB
- func (a *App) GetCurrentTimeOffset(lat, lon float64) int
- func (a *App) GetTimezoneInfo(lat, lon float64) (*TZInfo, error)
- func (a *App) SetBotCommands(bc []echotron.BotCommand) error
- func (a *App) Start()
- func (a *App) StartWithWebhook(webhookURL string)
- type AppOption
- type Bot
- func (b *Bot) API() echotron.API
- func (b *Bot) App() *App
- func (b *Bot) ChatID() int64
- func (b *Bot) Command() *Command
- func (b *Bot) DB() *DB
- func (b *Bot) DeleteMessage(q *echotron.CallbackQuery)
- func (b *Bot) DisableUser()
- func (b *Bot) EnableUser()
- func (b *Bot) GetUsersTimezoneOffset() (int, error)
- func (b *Bot) IsUserActive() bool
- func (b *Bot) Log() *slog.Logger
- func (b *Bot) ReplaceMessage(q *echotron.CallbackQuery, text string, ...)
- func (b *Bot) Update(u *echotron.Update)
- func (b *Bot) User() *model.User
- type ChatType
- type Command
- type CommandHandler
- type CommandRegistry
- type Config
- type DB
- type DefaultCommandHandler
- type DefaultUpdateHandler
- func (h *DefaultUpdateHandler) Bot() *Bot
- func (h *DefaultUpdateHandler) HandleCallbackQuery(c echotron.CallbackQuery) StateFn
- func (h *DefaultUpdateHandler) HandleChannelPost(m echotron.Message) StateFn
- func (h *DefaultUpdateHandler) HandleChatJoinRequest(c echotron.ChatJoinRequest) StateFn
- func (h *DefaultUpdateHandler) HandleChatMember(c echotron.ChatMemberUpdated) StateFn
- func (h *DefaultUpdateHandler) HandleChosenInlineResult(c echotron.ChosenInlineResult) StateFn
- func (h *DefaultUpdateHandler) HandleEditedChannelPost(m echotron.Message) StateFn
- func (h *DefaultUpdateHandler) HandleEditedMessage(m echotron.Message) StateFn
- func (h *DefaultUpdateHandler) HandleInlineQuery(i echotron.InlineQuery) StateFn
- func (h *DefaultUpdateHandler) HandleMessage(m echotron.Message) StateFn
- func (h *DefaultUpdateHandler) HandleMyChatMember(c echotron.ChatMemberUpdated) StateFn
- func (h *DefaultUpdateHandler) HandlePreCheckoutQuery(p echotron.PreCheckoutQuery) StateFn
- func (h *DefaultUpdateHandler) HandleShippingQuery(s echotron.ShippingQuery) StateFn
- func (h *DefaultUpdateHandler) SetBot(bot *Bot)
- type InlineKeyboardButton
- type StateFn
- type TZInfo
- type UpdateHandler
- type UpdateHandlerFn
Examples ¶
Constants ¶
const ( DB_TYPE_SQLITE = "sqlite" DB_TYPE_MYSQL = "mysql" DB_TYPE_POSTGRES = "postgres" )
Variables ¶
This section is empty.
Functions ¶
func BuildInlineKeyboardButtonRow ¶
func BuildInlineKeyboardButtonRow(buttons []InlineKeyboardButton) []echotron.InlineKeyboardButton
BuildInlineKeyboardButtonRow helper function for creating Telegram inline keyboards
func GetUserFromUpdate ¶
GetUserFromUpdate returns the echotron.User from a given echotron.Update
func PrintAsJson ¶
PrintAsJson returns the JSON representation of a given go struct.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
func NewApp ¶
NewApp creates a new Telegram bot based on the given configuration. It uses functional options for configuration.
Example ¶
package main
import (
"github.com/apperia-de/tbb"
)
func main() {
type CustomConfig struct {
Version string `yaml:"version"`
Blacklist []string `yaml:"blacklist"`
}
cfg := tbb.LoadCustomConfig[CustomConfig]("config.yml")
app := tbb.NewApp(tbb.WithConfig(cfg))
app.Start()
}
func (*App) DB ¶
DB returns the database handle for the bot so that the database can easily be adjusted and extended.
func (*App) GetCurrentTimeOffset ¶
GetCurrentTimeOffset returns the time offset in seconds for the given coordinates or zero if no time zone info may be obtained from coordinates.
func (*App) GetTimezoneInfo ¶
GetTimezoneInfo returns the time zone info for the given coordinates if available.
func (*App) SetBotCommands ¶
func (a *App) SetBotCommands(bc []echotron.BotCommand) error
SetBotCommands registers the given command list for your Telegram bot. Will delete registered bot commands if parameter bc is nil.
func (*App) StartWithWebhook ¶
StartWithWebhook starts the Telegram bot server with a given webhook url.
type AppOption ¶
type AppOption func(*App)
func WithCommands ¶
WithCommands is used for providing and registering custom bot commands. Bot commands always start with a / like /start and a Handler, which implements the CommandHandler interface. If you want a command to be available in the command list on Telegram, the provided Command must contain a Description.
func WithConfig ¶
WithConfig is the only required option because it provides the config for the app to function properly.
func WithHandlerFunc ¶
func WithHandlerFunc(hFn UpdateHandlerFn) AppOption
WithHandlerFunc option can be used to override the default UpdateHandlerFn for custom echotron.Update message handling.
func WithLogger ¶
WithLogger option can be used to override the default logger with a custom one.
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
func (*Bot) DeleteMessage ¶
func (b *Bot) DeleteMessage(q *echotron.CallbackQuery)
DeleteMessage deletes the given CallbackQuery message
func (*Bot) DisableUser ¶
func (b *Bot) DisableUser()
DisableUser disable the current user and update the database
func (*Bot) EnableUser ¶
func (b *Bot) EnableUser()
EnableUser enables the current user and updates the database
func (*Bot) GetUsersTimezoneOffset ¶
GetUsersTimezoneOffset returns the time zone offset in seconds if the user has already provided coordinates. This can be used, for example, to calculate the current time of the user.
func (*Bot) IsUserActive ¶
IsUserActive returns true if the user is active or false otherwise
func (*Bot) ReplaceMessage ¶
func (b *Bot) ReplaceMessage(q *echotron.CallbackQuery, text string, buttons [][]echotron.InlineKeyboardButton)
ReplaceMessage replaces the given CallbackQuery message with new Text and Keyboard
type ChatType ¶ added in v0.5.0
type ChatType string
func GetChatTypeFromUpdate ¶ added in v0.5.0
GetChatTypeFromUpdate returns the ChatType from a given echotron.Update
type Command ¶
type Command struct {
Name string
Description string
Params []string
Data any
Handler CommandHandler
}
type CommandHandler ¶
type CommandRegistry ¶
type Config ¶
type Config struct {
Admin struct {
BotToken string `yaml:"botToken"` // Telegram bot token for an admin bot to use when sending messages
ChatIDs []int64 `yaml:"chatIDs"` // Telegram chat IDs of admins
} `yaml:"admin"`
AllowedChatIDs []int64 `yaml:"allowedChatIDs"` // If set, only the specified chatIDs are allowed to use the bot. If not set or empty, all chat ids are allowed to use the bot.
Database struct {
Type string `yaml:"type"` // one of sqlite, mysql, postgres
DSN string `yaml:"dsn"` // in the case of mysql or postgres
Filename string `yaml:"filename"` // in the case of sqlite
} `yaml:"database"`
Debug bool `yaml:"debug"`
BotSessionTimeout int `yaml:"botSessionTimeout"` // Timeout in minutes, after which the bot instance will be deleted to save memory. Defaults to 15 minutes.
LogLevel string `yaml:"logLevel"`
Telegram struct {
BotToken string `yaml:"botToken"`
} `yaml:"telegram"`
CustomData any `yaml:"customData"`
}
func LoadConfig ¶
LoadConfig returns the yaml config with the given name
func LoadCustomConfig ¶ added in v0.2.0
LoadCustomConfig returns the config but also takes your custom struct for the "customData" into account.
type DB ¶
type DefaultCommandHandler ¶
type DefaultCommandHandler struct {
// contains filtered or unexported fields
}
func (*DefaultCommandHandler) Bot ¶
func (h *DefaultCommandHandler) Bot() *Bot
func (*DefaultCommandHandler) Handle ¶
func (h *DefaultCommandHandler) Handle() StateFn
func (*DefaultCommandHandler) SetBot ¶ added in v0.4.0
func (h *DefaultCommandHandler) SetBot(bot *Bot)
type DefaultUpdateHandler ¶
type DefaultUpdateHandler struct {
// contains filtered or unexported fields
}
DefaultUpdateHandler implements the UpdateHandler interface
func (*DefaultUpdateHandler) Bot ¶
func (h *DefaultUpdateHandler) Bot() *Bot
func (*DefaultUpdateHandler) HandleCallbackQuery ¶
func (h *DefaultUpdateHandler) HandleCallbackQuery(c echotron.CallbackQuery) StateFn
func (*DefaultUpdateHandler) HandleChannelPost ¶
func (h *DefaultUpdateHandler) HandleChannelPost(m echotron.Message) StateFn
func (*DefaultUpdateHandler) HandleChatJoinRequest ¶
func (h *DefaultUpdateHandler) HandleChatJoinRequest(c echotron.ChatJoinRequest) StateFn
func (*DefaultUpdateHandler) HandleChatMember ¶
func (h *DefaultUpdateHandler) HandleChatMember(c echotron.ChatMemberUpdated) StateFn
func (*DefaultUpdateHandler) HandleChosenInlineResult ¶
func (h *DefaultUpdateHandler) HandleChosenInlineResult(c echotron.ChosenInlineResult) StateFn
func (*DefaultUpdateHandler) HandleEditedChannelPost ¶
func (h *DefaultUpdateHandler) HandleEditedChannelPost(m echotron.Message) StateFn
func (*DefaultUpdateHandler) HandleEditedMessage ¶
func (h *DefaultUpdateHandler) HandleEditedMessage(m echotron.Message) StateFn
func (*DefaultUpdateHandler) HandleInlineQuery ¶
func (h *DefaultUpdateHandler) HandleInlineQuery(i echotron.InlineQuery) StateFn
func (*DefaultUpdateHandler) HandleMessage ¶
func (h *DefaultUpdateHandler) HandleMessage(m echotron.Message) StateFn
func (*DefaultUpdateHandler) HandleMyChatMember ¶
func (h *DefaultUpdateHandler) HandleMyChatMember(c echotron.ChatMemberUpdated) StateFn
func (*DefaultUpdateHandler) HandlePreCheckoutQuery ¶
func (h *DefaultUpdateHandler) HandlePreCheckoutQuery(p echotron.PreCheckoutQuery) StateFn
func (*DefaultUpdateHandler) HandleShippingQuery ¶
func (h *DefaultUpdateHandler) HandleShippingQuery(s echotron.ShippingQuery) StateFn
func (*DefaultUpdateHandler) SetBot ¶
func (h *DefaultUpdateHandler) SetBot(bot *Bot)
type InlineKeyboardButton ¶
type UpdateHandler ¶
type UpdateHandler interface {
Bot() *Bot
SetBot(*Bot)
HandleMessage(echotron.Message) StateFn
HandleEditedMessage(echotron.Message) StateFn
HandleChannelPost(echotron.Message) StateFn
HandleEditedChannelPost(echotron.Message) StateFn
HandleInlineQuery(echotron.InlineQuery) StateFn
HandleChosenInlineResult(echotron.ChosenInlineResult) StateFn
HandleCallbackQuery(echotron.CallbackQuery) StateFn
HandleShippingQuery(echotron.ShippingQuery) StateFn
HandlePreCheckoutQuery(echotron.PreCheckoutQuery) StateFn
HandleChatMember(echotron.ChatMemberUpdated) StateFn
HandleMyChatMember(echotron.ChatMemberUpdated) StateFn
HandleChatJoinRequest(echotron.ChatJoinRequest) StateFn
}
type UpdateHandlerFn ¶
type UpdateHandlerFn func() UpdateHandler