Documentation
¶
Overview ¶
Package bote provides a comprehensive metrics system for Telegram bot monitoring. It includes metrics for updates, handlers, messages, users, webhooks, and errors.
Index ¶
- Constants
- Variables
- func AllowWithoutReply() any
- func CreateBtnData(dataList ...string) string
- func F(msg string, formats ...Format) string
- func FB(msg string) string
- func FBC(msg string) string
- func FBI(msg string) string
- func FBU(msg string) string
- func FBf(msg string, args ...any) string
- func FBold(msg string) string
- func FBoldCode(msg string) string
- func FBoldItalic(msg string) string
- func FBoldUnderline(msg string) string
- func FBoldf(msg string, args ...any) string
- func FC(msg string) string
- func FCf(msg string, args ...any) string
- func FCode(msg string) string
- func FCodef(msg string, args ...any) string
- func FI(msg string) string
- func FIf(msg string, args ...any) string
- func FItalic(msg string) string
- func FItalicf(msg string, args ...any) string
- func FP(msg string) string
- func FPf(msg string, args ...any) string
- func FPre(msg string) string
- func FPref(msg string, args ...any) string
- func FS(msg string) string
- func FSf(msg string, args ...any) string
- func FStrike(msg string) string
- func FStrikef(msg string, args ...any) string
- func FU(msg string) string
- func FUf(msg string, args ...any) string
- func FUnderline(msg string) string
- func FUnderlinef(msg string, args ...any) string
- func Ff(msg string, args ...any) string
- func ForceReply() any
- func GetFilledMessage(left, right, sep, fill string, maxLeft, maxRight, maxLen int) string
- func HTML() any
- func Inline(rowLength int, btns ...tele.Btn) *tele.ReplyMarkup
- func InlineBuilder(columns int, runesTypes RuneSizeType, btns ...tele.Btn) *tele.ReplyMarkup
- func Markdown() any
- func MarkdownV2() any
- func NewHMAC(id int64, hmacKey *EncryptionKey) string
- func NoPreview() any
- func OneTimeKeyboard() any
- func Protected() any
- func RegisterTextStates(state ...State) bool
- func RemoveKeyboard() *tele.ReplyMarkup
- func Silent() any
- func SingleRow(btn ...tele.Btn) *tele.ReplyMarkup
- func WithAllowedUpdates(updates ...string) func(opts *Options)
- func WithBotConfig(cfg BotConfig) func(opts *Options)
- func WithConfig(cfg Config) func(opts *Options)
- func WithCustomPoller(poller tele.Poller) func(opts *Options)
- func WithDebugIncomingUpdates() func(opts *Options)
- func WithDefaultLanguage(lang Language) func(opts *Options)
- func WithLogLevel(level string) func(opts *Options)
- func WithLogger(logger Logger, level ...string) func(opts *Options)
- func WithLongPolling(timeout ...time.Duration) func(opts *Options)
- func WithLongPollingConfig(cfg LongPollingConfig) func(opts *Options)
- func WithLowPrivacyMode() func(opts *Options)
- func WithMetricsConfig(metrics MetricsConfig) func(opts *Options)
- func WithMode(mode PollingMode) func(opts *Options)
- func WithMsgsProvider(msgs MessageProvider) func(opts *Options)
- func WithOffline(poller ...tele.Poller) func(opts *Options)
- func WithStrictPrivacyMode(encryptionKey *string, encryptionKeyVersion *int64, hmacKey *string, ...) func(opts *Options)
- func WithStrictPrivacyModeKeyProvider(keysProvider KeysProvider) func(opts *Options)
- func WithUpdateLogger(logger UpdateLogger) func(opts *Options)
- func WithUserDB(db UsersStorage) func(opts *Options)
- func WithWebhook(url string, listen ...string) func(opts *Options)
- func WithWebhookAllowedIPs(allowedIPs ...string) func(opts *Options)
- func WithWebhookAllowedTelegramIPs() func(opts *Options)
- func WithWebhookCertificate(certFile, keyFile string, loadCertificateInTelegram bool, startHTTPS bool) func(opts *Options)
- func WithWebhookConfig(cfg WebhookConfig) func(opts *Options)
- func WithWebhookDefaultMetrics(metricsPath ...string) func(opts *Options)
- func WithWebhookGenerateCertificate(directory ...string) func(opts *Options)
- func WithWebhookMetrics(metrics MetricsConfig, metricsPath ...string) func(opts *Options)
- func WithWebhookRateLimit(rps int, burst int) func(opts *Options)
- func WithWebhookSecretToken(secretToken string) func(opts *Options)
- func WithWebhookSecurityHeaders() func(opts *Options)
- func WithWebhookServer(listen string, isHTTPS bool) func(opts *Options)
- type Bot
- func (b *Bot) AddMiddleware(f MiddlewareFuncTele, chatType ...tele.ChatType)
- func (b *Bot) AddUserMiddleware(f ...MiddlewareFunc)
- func (b *Bot) Bot() *tele.Bot
- func (b *Bot) CreateUserFromModel(model UserModel, addToCache bool) User
- func (b *Bot) DeleteInChat(chatID int64, msgID int) error
- func (b *Bot) EditInChat(chatID int64, msgID int, msg string, kb *tele.ReplyMarkup, opts ...any) error
- func (b *Bot) GetAllUsersFromCache() []User
- func (b *Bot) GetUserID(userID FullUserID) (int64, error)
- func (b *Bot) Handle(endpoint any, f HandlerFunc)
- func (b *Bot) SendInChat(chatID int64, threadID int, msg string, kb *tele.ReplyMarkup, opts ...any) (int, error)
- func (b *Bot) SetMessageProvider(msgs MessageProvider)
- func (b *Bot) SetTextHandler(handler HandlerFunc)
- func (b *Bot) Start(ctx context.Context, startHandler HandlerFunc, stateMap map[State]InitBundle) chan struct{}
- type BotConfig
- type ButtonBuilder
- type Config
- type Context
- func NewContext(b *Bot, userID int64, callbackMsgID int, data ...string) Context
- func NewContextSecure(b *Bot, userIDSecure FullUserID, callbackMsgID int, data ...string) (Context, error)
- func NewContextText(b *Bot, userID int64, textMsgID int, text string) Context
- func NewContextTextSecure(b *Bot, userIDSecure FullUserID, textMsgID int, text string) (Context, error)
- type EncryptionKey
- type Format
- type FullUserID
- type HandlerFunc
- type InitBundle
- type Keyboard
- func (k *Keyboard) Add(btns ...tele.Btn) *Keyboard
- func (k *Keyboard) AddFooter(btns ...tele.Btn) *Keyboard
- func (k *Keyboard) AddRow(btns ...tele.Btn) *Keyboard
- func (k *Keyboard) CreateInlineMarkup() *tele.ReplyMarkup
- func (k *Keyboard) CreateReplyMarkup(oneTime bool) *tele.ReplyMarkup
- func (k *Keyboard) StartNewRow() *Keyboard
- type KeyboardWithContext
- func (k *KeyboardWithContext) AB(name string, callback HandlerFunc, dataList ...string) *Keyboard
- func (k *KeyboardWithContext) ABR(name string, callback HandlerFunc, dataList ...string) *Keyboard
- func (k *KeyboardWithContext) AddBtn(name string, callback HandlerFunc, dataList ...string) *Keyboard
- func (k *KeyboardWithContext) AddBtnRow(name string, callback HandlerFunc, dataList ...string) *Keyboard
- type KeysProvider
- type Language
- type LogConfig
- type Logger
- type LongPollingConfig
- type MessageBuilder
- func (b *MessageBuilder) IsEmpty() bool
- func (b *MessageBuilder) Write(msg ...string)
- func (b *MessageBuilder) WriteBytes(data ...[]byte)
- func (b *MessageBuilder) WriteIf(condition bool, writeTrue string, writeFalse ...string)
- func (b *MessageBuilder) WriteIfF(condition bool, writeTrue, writeFalse string, args ...any)
- func (b *MessageBuilder) Writebln(msg ...string)
- func (b *MessageBuilder) Writeblnf(msg string, args ...any)
- func (b *MessageBuilder) Writebn(msg ...string)
- func (b *MessageBuilder) Writebnf(msg string, args ...any)
- func (b *MessageBuilder) Writef(msg string, args ...any)
- func (b *MessageBuilder) Writeln(s ...string)
- func (b *MessageBuilder) WritelnIf(condition bool, writeTrue string, writeFalse ...string)
- func (b *MessageBuilder) WritelnIfF(condition bool, writeTrue, writeFalse string, args ...any)
- func (b *MessageBuilder) WritelnIfFf(condition bool, writeTrue string, args ...any)
- func (b *MessageBuilder) Writelnf(s string, args ...any)
- func (b *MessageBuilder) Writelnn(s ...string)
- func (b *MessageBuilder) Writelnnf(s string, args ...any)
- type MessageProvider
- type Messages
- type MessagesState
- type MetricsConfig
- type MiddlewareFunc
- type MiddlewareFuncTele
- type Options
- type PollingMode
- type PrivacyConfig
- type PrivacyMode
- type RuneSizeType
- type State
- type UpdateLogger
- type UpdateType
- type User
- type UserInfo
- type UserInfoDiff
- type UserMessages
- type UserMessagesDiff
- type UserModel
- type UserModelDiff
- type UserStat
- type UserStatDiff
- type UserState
- type UserStateDiff
- type UsersStorage
- type WebhookConfig
- type WebhookRateLimitConfig
- type WebhookSecurityConfig
Constants ¶
const ( // Error types for different failure scenarios MetricsErrorBotBlocked = "bot_blocked" // Bot is blocked by user MetricsErrorHandler = "handler" // Handler execution error MetricsErrorInternal = "internal" // Internal bote package error MetricsErrorTelegramAPI = "telegram_api" // Telegram API error MetricsErrorInvalidUserState = "invalid_user_state" // Invalid user state error MetricsErrorBadUsage = "bad_usage" // Package usage error MetricsErrorConnectionError = "connection_error" // Connection error // Error severity levels MetricsErrorSeverityLow = "low" // Low severity error MetricsErrorSeveritHigh = "high" // High severity error // Time window constants for active user metrics MetricsWindow1h = "1h" // 1 hour window MetricsWindow24h = "24h" // 24 hour window )
Error type constants for metrics categorization
const ( LogLevelDebug = "debug" LogLevelInfo = "info" LogLevelWarn = "warn" LogLevelError = "error" )
const ( // UserIDDBFieldName is a field name for plain user ID in DB. UserIDDBFieldName = "id.id_plain" // IDEncDBFieldName is a field name for encrypted user ID in DB. UserIDEncDBFieldName = "id.id_enc" // EncKeyVersionDBFieldName is a field name for encryption key version in DB. UserIDEncKeyVersionDBFieldName = "id.enc_key_version" // UserIDHMACDBFieldName is a field name for HMAC of user ID in DB. UserIDHMACDBFieldName = "id.id_hmac" // UserIDHMACKeyVersionDBFieldName is a field name for HMAC key version in DB. UserIDHMACKeyVersionDBFieldName = "id.hmac_key_version" )
const (
// MaxTextLenInLogs is the maximum length of the text in message logs.
MaxTextLenInLogs = 64
)
Variables ¶
var ( EmptyBtn tele.Btn EmptyKeyboard = Inline(maxButtonsInRow) )
var ( // Handler duration buckets for all handlers (50ms to 5s) BotHandlerDurationBuckets = []float64{0.05, 0.1, 0.25, 0.5, 1, 2, 5} // Webhook response time buckets for all webhooks (5ms to 5s) WebhookHistogramBuckets = []float64{0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 2.5} // Session length buckets for active users (10s to 1h) SessionLengthBucketsSeconds = []float64{10, 30, 60, 120, 300, 600, 1800} )
Predefined histogram buckets for different metric types
var EmptyHandler = func(Context) error { return nil }
EmptyHandler is a handler that does nothing.
Functions ¶
func AllowWithoutReply ¶
func AllowWithoutReply() any
func CreateBtnData ¶
CreateBtnData creates data string from dataList, that should be passed as data to callback button. This method can be useful when creating InitBundle with providing [InitBundle.Data].
func FBf ¶
FBf returns a string with bold formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FBoldItalic ¶
FBoldItalic returns a string with bold and italic formatting.
func FBoldUnderline ¶
FBoldUnderline returns a string with bold and underline formatting.
func FBoldf ¶
FBoldf returns a string with bold formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FCf ¶
FCf returns a string with code formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FCodef ¶
FCodef returns a string with code formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FIf ¶
FI returns a string with italic formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FItalicf ¶
FItalicf returns a string with italic formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FPf ¶
FPf returns a string with pre formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FPref ¶
FPref returns a string with pre formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FSf ¶
FSf returns a string with strike formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FStrikef ¶
FStrikef returns a string with strike formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FUf ¶
FUf returns a string with underline formatting. If args are provided, it uses fmt.Sprintf to format the string.
func FUnderline ¶
FUnderline returns a string with underline formatting.
func FUnderlinef ¶
FUnderlinef returns a string with underline formatting. If args are provided, it uses fmt.Sprintf to format the string.
func Ff ¶
Ff returns a formatted string, just like fmt.Sprintf.
func ForceReply ¶
func ForceReply() any
func GetFilledMessage ¶
GetFilledMessage returns a formatted string with aligned left and right parts.
func Inline ¶
func Inline(rowLength int, btns ...tele.Btn) *tele.ReplyMarkup
Inline creates inline keyboard from provided rows of buttons.
func InlineBuilder ¶
func InlineBuilder(columns int, runesTypes RuneSizeType, btns ...tele.Btn) *tele.ReplyMarkup
InlineBuilder creates inline keyboard from provided buttons and columns count.
func MarkdownV2 ¶
func MarkdownV2() any
func NewHMAC ¶
func NewHMAC(id int64, hmacKey *EncryptionKey) string
NewHMAC creates a new HMAC of provided ID using provided HMAC key.
func OneTimeKeyboard ¶
func OneTimeKeyboard() any
func RegisterTextStates ¶
RegisterTextState registers a state that expects text input from the user. It is used to handle text input from the user in a correct order.
func RemoveKeyboard ¶
func RemoveKeyboard() *tele.ReplyMarkup
RemoveKeyboard creates remove keyboard request.
func SingleRow ¶
func SingleRow(btn ...tele.Btn) *tele.ReplyMarkup
SingleRow creates inline keyboard from provided buttons with a single row.
func WithAllowedUpdates ¶
WithAllowedUpdates returns an option that sets the allowed updates.
func WithBotConfig ¶
WithBotConfig returns an option that sets the bot configuration.
func WithConfig ¶
WithConfig returns an option that sets the bot configuration.
func WithCustomPoller ¶
WithCustomPoller returns an option that sets the custom poller.
func WithDebugIncomingUpdates ¶
func WithDebugIncomingUpdates() func(opts *Options)
WithDebugIncomingUpdates returns an option that sets the debug incoming updates.
func WithDefaultLanguage ¶
WithDefaultLanguage returns an option that sets the default language.
func WithLogLevel ¶
WithDebug returns an option that sets the debug mode.
func WithLogger ¶
WithLogger returns an option that sets the logger.
func WithLongPolling ¶
WithLongPolling returns an option that sets the long polling configuration.
func WithLongPollingConfig ¶
func WithLongPollingConfig(cfg LongPollingConfig) func(opts *Options)
WithLongPolling returns an option that sets the long polling configuration.
func WithLowPrivacyMode ¶
func WithLowPrivacyMode() func(opts *Options)
WithLowPrivacyMode returns an option that sets the low privacy mode.
func WithMetricsConfig ¶
func WithMetricsConfig(metrics MetricsConfig) func(opts *Options)
func WithMode ¶
func WithMode(mode PollingMode) func(opts *Options)
WithMode returns an option that sets the polling mode.
func WithMsgsProvider ¶
func WithMsgsProvider(msgs MessageProvider) func(opts *Options)
WithMsgsProvider returns an option that sets the message provider.
func WithOffline ¶
WithOffline returns an option that sets the offline mode. If poller is provided, it will be used instead of the default poller. It is used to create a bot without network for testing purposes.
func WithStrictPrivacyMode ¶
func WithStrictPrivacyMode(encryptionKey *string, encryptionKeyVersion *int64, hmacKey *string, hmacKeyVersion *int64) func(opts *Options)
WithLowPrivacyMode returns an option that sets the low privacy mode.
func WithStrictPrivacyModeKeyProvider ¶
func WithStrictPrivacyModeKeyProvider(keysProvider KeysProvider) func(opts *Options)
WithStrictPrivacyMode returns an option that sets the strict privacy mode.
func WithUpdateLogger ¶
func WithUpdateLogger(logger UpdateLogger) func(opts *Options)
WithUpdateLogger returns an option that sets the update logger.
func WithUserDB ¶
func WithUserDB(db UsersStorage) func(opts *Options)
WithUserDB returns an option that sets the user storage.
func WithWebhook ¶
WithWebhook returns an option that creates a webhook configuration from URL and basic settings. This is a convenience function for simple webhook setups.
func WithWebhookAllowedIPs ¶
WithWebhookAllowedIPs returns an option that sets the webhook allowed IPs.
func WithWebhookAllowedTelegramIPs ¶
func WithWebhookAllowedTelegramIPs() func(opts *Options)
WithWebhookAllowedTelegramIPs returns an option that sets the webhook allowed Telegram IPs.
func WithWebhookCertificate ¶
func WithWebhookCertificate(certFile, keyFile string, loadCertificateInTelegram bool, startHTTPS bool) func(opts *Options)
WithWebhookTLS returns an option that creates a webhook configuration with TLS certificates.
func WithWebhookConfig ¶
func WithWebhookConfig(cfg WebhookConfig) func(opts *Options)
WithWebhookConfig returns an option that sets the webhook configuration.
func WithWebhookDefaultMetrics ¶
WithWebhookGenerateCertificate returns an option that generates self-signed certificate and uploads it to Telegram.
func WithWebhookGenerateCertificate ¶
WithWebhookGenerateCertificate returns an option that generates self-signed certificate and uploads it to Telegram.
func WithWebhookMetrics ¶
func WithWebhookMetrics(metrics MetricsConfig, metricsPath ...string) func(opts *Options)
WithWebhookGenerateCertificate returns an option that generates self-signed certificate and uploads it to Telegram.
func WithWebhookRateLimit ¶
WithWebhookRateLimit returns an option that sets the webhook rate limit.
func WithWebhookSecretToken ¶
WithWebhookSecretToken returns an option that sets the webhook secret token.
func WithWebhookSecurityHeaders ¶
func WithWebhookSecurityHeaders() func(opts *Options)
WithWebhookSecurityHeaders returns an option that sets the webhook security headers.
func WithWebhookServer ¶
WithWebhookServer returns an option that sets the webhook server listen address.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot is a main struct of this package. It contains all necessary components for working with Telegram bot.
func New ¶
New creates the bot with optional options. It starts the bot in a separate goroutine. You should call [Bot.Stop] to gracefully shutdown the bot.
func NewWithOptions ¶
NewWithOptions starts the bot with options. It starts the bot in a separate goroutine. You should call [Bot.Stop] to gracefully shutdown the bot.
func (*Bot) AddMiddleware ¶
func (b *Bot) AddMiddleware(f MiddlewareFuncTele, chatType ...tele.ChatType)
AddMiddleware adds middleware functions that will be called on each update for a specific chat type.
func (*Bot) AddUserMiddleware ¶
func (b *Bot) AddUserMiddleware(f ...MiddlewareFunc)
AddUserMiddleware adds middleware functions that will be called on each update for a user.
func (*Bot) CreateUserFromModel ¶
CreateUserFromModel creates a new user from UserModel. If addToCache is true, the user will be added to the cache. It is useful when you want to preinit user in cache before he makes a request. User should have plain ID to be added to the cache.
func (*Bot) EditInChat ¶
func (*Bot) GetAllUsersFromCache ¶
GetAllUsersFromCache returns all loaded users.
func (*Bot) Handle ¶
func (b *Bot) Handle(endpoint any, f HandlerFunc)
Handle sets handler for any endpoint. Endpoint can be string or callback button.
func (*Bot) SendInChat ¶
func (b *Bot) SendInChat(chatID int64, threadID int, msg string, kb *tele.ReplyMarkup, opts ...any) (int, error)
SendInChat sends a message to a specific chat ID and thread ID. chatID is the target chat ID, threadID is the target thread ID (0 for no thread). msg is the message to send. kb is the keyboard to send. opts are additional options for sending the message.
func (*Bot) SetMessageProvider ¶
func (b *Bot) SetMessageProvider(msgs MessageProvider)
SetMessageProvider sets message provider.
func (*Bot) SetTextHandler ¶
func (b *Bot) SetTextHandler(handler HandlerFunc)
SetTextHandler sets handler for text messages. You should provide a single handler for all text messages, that will call another handlers based on the state.
func (*Bot) Start ¶
func (b *Bot) Start(ctx context.Context, startHandler HandlerFunc, stateMap map[State]InitBundle) chan struct{}
Start starts the bot in a separate goroutine. StartHandler is a handler for /start command. StateMap is a map for initing users after bot restart. It runs an assigned handler for every active user message when user makes a request by message's state. Inline buttons will trigger onCallback handler if you don't init them after bot restart. You can pass nil map if you don't need to reinit messages.
type BotConfig ¶
type BotConfig struct {
// ParseMode is the default parse mode for the bot.
// Default: HTML.
// Environment variable: BOTE_PARSE_MODE.
// It can be one of the following:
// - "HTML"
// - "Markdown"
// - "MarkdownV2"
ParseMode tele.ParseMode `yaml:"mode" json:"mode" env:"BOTE_PARSE_MODE"`
// Privacy is a configuration for privacy mode.
Privacy PrivacyConfig `yaml:"privacy" json:"privacy"`
// DefaultLanguage is the default language code for the bot in ISO 639-1 format.
// Default: "en".
// Environment variable: BOTE_DEFAULT_LANGUAGE.
DefaultLanguage Language `yaml:"default_language" json:"default_language" env:"BOTE_DEFAULT_LANGUAGE"`
// NoPreview is a flag that disables link preview in bot messages.
// Default: false.
// Environment variable: BOTE_NO_PREVIEW.
NoPreview bool `yaml:"no_preview" json:"no_preview" env:"BOTE_NO_PREVIEW"`
// DeleteMessages is a flag that enables deleting every user message.
// Default: true.
// Environment variable: BOTE_DELETE_MESSAGES.
DeleteMessages *bool `yaml:"delete_messages" json:"delete_messages" env:"BOTE_DELETE_MESSAGES"`
// UserCacheCapacity is the capacity of the user cache. Cache will evict users with least activity.
// Default: 10000.
// Environment variable: BOTE_USER_CACHE_CAPACITY.
UserCacheCapacity int `yaml:"user_cache_capacity" json:"user_cache_capacity" env:"BOTE_USER_CACHE_CAPACITY"`
// UserCacheTTL is the TTL of the user cache.
// Default: 24 hours.
// Environment variable: BOTE_USER_CACHE_TTL.
UserCacheTTL time.Duration `yaml:"user_cache_ttl" json:"user_cache_ttl" env:"BOTE_USER_CACHE_TTL"`
}
type ButtonBuilder ¶
type ButtonBuilder interface {
Btn(name string, callback HandlerFunc, dataList ...string) tele.Btn
}
ButtonBuilder is an interface for creating buttons. Use this interface to provide Context to handlers without other methods.
type Config ¶
type Config struct {
// Mode is the polling mode to use.
// Default: long.
// Possible values:
// - "long" - long polling
// - "webhook" - webhook
// - "custom" - custom poller (you should provide poller using [WithPoller] option)
// Environment variable: BOTE_MODE.
Mode PollingMode `yaml:"mode" json:"mode" env:"BOTE_MODE"`
// LongPolling contains long polling configuration.
LongPolling LongPollingConfig `yaml:"long_polling" json:"long_polling"`
// Webhook contains webhook configuration.
Webhook WebhookConfig `yaml:"webhook" json:"webhook"`
// Bot contains bot configuration.
Bot BotConfig `yaml:"bot" json:"bot"`
// Log contains log configuration.
Log LogConfig `yaml:"log" json:"log"`
}
Config contains bote configuration.
type Context ¶
type Context interface {
// Tele returns underlying telebot context.
Tele() tele.Context
// User returns current User context if chat type is private.
// WARNING: If chat type is not private, returns public read only user.
User() User
// IsPrivate returns true if the current chat type is private.
IsPrivate() bool
// ChatID returns the ID of the current chat.
ChatID() int64
// ChatType returns the type of the current chat.
ChatType() tele.ChatType
// IsMentioned returns true if the bot is mentioned in the current message.
IsMentioned() bool
// IsReply returns true if the current message is a reply to the bot's message.
IsReply() bool
// MessageID returns an ID of the message.
// If handler was called from a callback button, message is the one with keyboard.
// If handler was called from a text message and user's state IsText == true,
// message is the one with an active text handler with this state (not sent message!).
// In other case it is an ID of the message sent to the chat.
MessageID() int
// ButtonID returns an ID of the pressed callback button.
ButtonID() string
// Data returns callback button data. If there are many items in data, they will be separated by '|'.
Data() string
// DataParsed returns all items of button data.
DataParsed() []string
// Text returns a text sended by the user.
Text() string
// TextWithMessage returns a text sended by the user and the ID of this message (deleted by default).
TextWithMessage() (string, int)
// Set sets custom data for the current context.
Set(key, value string)
// Get returns custom data from the current context.
Get(key string) string
// Btn creates button and registers handler for it. You can provide data for the button.
// Data items will be separated by '|' in a single data string.
// Button unique value is generated from hexing button name with 10 random bytes at the end.
Btn(name string, callback HandlerFunc, dataList ...string) tele.Btn
// Send sends new main and head messages to the user.
// Old head message will be deleted. Old main message will becomve historical.
// newState is a state of the user which will be set after sending message.
// opts are additional options for sending messages.
// WARNING: It works only in private chats.
Send(newState State, mainMsg, headMsg string, mainKb, headKb *tele.ReplyMarkup, opts ...any) (err error)
// SendMain sends new main message to the user.
// Old head message will be deleted. Old main message will becomve historical.
// newState is a state of the user which will be set after sending message.
// opts are additional options for sending message.
// WARNING: It works only in private chats.
SendMain(newState State, msg string, kb *tele.ReplyMarkup, opts ...any) error
// SendNotification sends a notification message to the user.
// opts are additional options for sending message.
// WARNING: It works only in private chats.
SendNotification(msg string, kb *tele.ReplyMarkup, opts ...any) error
// SendError sends an error message to the user.
// opts are additional options for sending message.
// WARNING: It works only in private chats.
SendError(msg string, opts ...any) error
// SendFile sends a file to the user.
// name is the name of the file to send.
// file is the file to send.
// opts are additional options for sending the file.
// WARNING: It works only in private chats.
SendFile(name string, file []byte, opts ...any) error
// SendInChat sends a message to a specific chat ID and thread ID.
// chatID is the target chat ID, threadID is the target thread ID (0 for no thread).
// msg is the message to send.
// kb is the keyboard to send.
// opts are additional options for sending the message.
SendInChat(chatID int64, threadID int, msg string, kb *tele.ReplyMarkup, opts ...any) (int, error)
// Edit edits main and head messages of the user.
// newState is a state of the user which will be set after editing message.
// opts are additional options for editing messages.
// WARNING: It works only in private chats.
Edit(newState State, mainMsg, headMsg string, mainKb, headKb *tele.ReplyMarkup, opts ...any) (err error)
// EditMain edits main message of the user.
// newState is a state of the user which will be set after editing message.
// opts are additional options for editing message.
// WARNING: It works only in private chats.
EditMain(newState State, msg string, kb *tele.ReplyMarkup, opts ...any) error
// EditMainReplyMarkup edits reply markup of the main message.
// opts are additional options for editing message.
// WARNING: It works only in private chats.
EditMainReplyMarkup(kb *tele.ReplyMarkup, opts ...any) error
// EditHistory edits message of the user by provided ID.
// newState is a state of the user which will be set after editing message.
// msgID is the ID of the message to edit.
// opts are additional options for editing message.
// WARNING: It works only in private chats.
EditHistory(newState State, msgID int, msg string, kb *tele.ReplyMarkup, opts ...any) error
// EditHistoryReplyMarkup edits reply markup of the history message.
// opts are additional options for editing message.
// WARNING: It works only in private chats.
EditHistoryReplyMarkup(msgID int, kb *tele.ReplyMarkup, opts ...any) error
// EditHead edits head message of the user.
// opts are additional options for editing message.
// WARNING: It works only in private chats.
EditHead(msg string, kb *tele.ReplyMarkup, opts ...any) error
// EditHeadReplyMarkup edits reply markup of the head message.
// opts are additional options for editing message.
// WARNING: It works only in private chats.
EditHeadReplyMarkup(kb *tele.ReplyMarkup, opts ...any) error
// EditInChat edits message in a specific chat ID and thread ID.
// chatID is the target chat ID, threadID is the target thread ID (0 for no thread).
// msg is the message to edit.
// kb is the keyboard to edit.
// opts are additional options for editing message.
EditInChat(chatID int64, msgID int, msg string, kb *tele.ReplyMarkup, opts ...any) error
// DeleteHead deletes head message of the user.
// WARNING: It works only in private chats.
DeleteHead() error
// DeleteHistory deletes provided history message.
// msgID is the ID of the history message to delete.
// WARNING: It works only in private chats.
DeleteHistory(msgID int) error
// DeleteNotification deletes notification message of the user.
// WARNING: It works only in private chats.
DeleteNotification() error
// DeleteError deletes error message of the user.
// WARNING: It works only in private chats.
DeleteError() error
// DeleteAll deletes all messages of the user from the specified ID.
// WARNING: It works only in private chats.
DeleteAll(from int)
// Delete deletes message by provided chat ID and message ID.
DeleteInChat(chatID int64, msgID int) error
// DeleteUser deletes user from the memory and deletes all messages of the user.
// Returns true if all messages were deleted successfully, false otherwise.
// It doesn't delete user from the persistent database, so you should make it manually.
// WARNING: It works only in private chats.
DeleteUser() bool
}
Context is an interface that provides to every handler.
func NewContext ¶
NewContext creates a new context for the given user simulating that callback button was pressed. It creates a minimal update to handle all possible methods in Context without panics. It can be useful if you want to start a handler without user action (by some external event). Warning! IT WON'T WORK WITH TEXT HANDLERS. Use NewContextText instead.
func NewContextSecure ¶
func NewContextSecure(b *Bot, userIDSecure FullUserID, callbackMsgID int, data ...string) (Context, error)
NewContextSecure creates a new context for the given user from encrypted ID simulating that callback button was pressed. It creates a minimal update to handle all possible methods in Context without panics. It can be useful if you want to start a handler without user action (by some external event). If strict privacy mode is disabled, it will use NewContext instead. Warning! IT WON'T WORK WITH TEXT HANDLERS. Use NewContextText instead.
func NewContextText ¶
NewContextText creates a new context for the given user simulating that text message was received. It creates a minimal update to handle all possible methods in Context without panics. It can be useful if you want to start a handler without user action (by some external event). Warning! IT WON'T WORK WITH CALLBACK HANDLERS. Use NewContext instead.
func NewContextTextSecure ¶
func NewContextTextSecure(b *Bot, userIDSecure FullUserID, textMsgID int, text string) (Context, error)
NewContextTextSecure creates a new context for the given user from encrypted ID simulating that text message was received. It creates a minimal update to handle all possible methods in Context without panics. It can be useful if you want to start a handler without user action (by some external event). If strict privacy mode is disabled, it will use NewContextText instead. Warning! IT WON'T WORK WITH CALLBACK HANDLERS. Use NewContext instead.
type EncryptionKey ¶
type EncryptionKey struct {
// contains filtered or unexported fields
}
EncryptionKey is a type that represents an AES encryption key for private user ID.
func NewEncryptionKey ¶
func NewEncryptionKey(version *int64) *EncryptionKey
NewEncryptionKey creates a new EncryptionKey.
func NewEncryptionKeyFromString ¶
func NewEncryptionKeyFromString(key string, version *int64) (*EncryptionKey, error)
NewEncryptionKeyFromString creates a new EncryptionKey from a string. Key should be a hex encoded string of 32 bytes.
func (*EncryptionKey) Clear ¶
func (k *EncryptionKey) Clear()
Clear clears the encryption key from memory to prevent reading it from memory dump. Use it after using the key to avoid leaking it.
func (*EncryptionKey) KeyBytes ¶
func (k *EncryptionKey) KeyBytes() []byte
KeyBytes returns the encryption key as a byte slice.
func (*EncryptionKey) String ¶
func (k *EncryptionKey) String() string
String converts EncryptionKey to a string.
func (*EncryptionKey) Version ¶
func (k *EncryptionKey) Version() *int64
Version returns the version of the encryption key.
type FullUserID ¶
type FullUserID struct {
// IDPlain is Telegram user ID in plain format.
// It is empty if privacy mode is strict.
IDPlain *int64 `json:"id_plain,omitempty" bson:"id_plain,omitempty" db:"id_plain,omitempty"`
// IDHMAC is a HMAC of IDPlain. It is used for searching user in DB by ID with plain ID is disabled.
// It is used if privacy mode is strict. It has a hex encoded string.
IDHMAC *string `json:"id_hmac,omitempty" bson:"id_hmac,omitempty" db:"id_hmac,omitempty"`
// IDEnc is an encrypted Telegram user ID. It is used to get readable IDPlain from IDEnc (HMAC is one way function).
// It is used instead of IDPlain when privacy mode is strict. It has a hex encoded string.
IDEnc *string `json:"id_enc,omitempty" bson:"id_enc,omitempty" db:"id_enc,omitempty"`
// HMACKeyVersion is a version of HMAC key for IDHMAC that used for HMAC of IDHMAC.
// It is used if privacy mode is strict.
HMACKeyVersion *int64 `json:"hmac_key_version,omitempty" bson:"hmac_key_version,omitempty" db:"hmac_key_version,omitempty"`
// EncKeyVersion is a version of encryption key for IDEnc that used for encryption and decryption of IDEnc.
// It is used if privacy mode is strict.
EncKeyVersion *int64 `json:"enc_key_version,omitempty" bson:"enc_key_version,omitempty" db:"enc_key_version,omitempty"`
}
FullUserID is a structure that represents secure user ID with encrypted and HMAC parts.
func NewPlainUserID ¶
func NewPlainUserID(id int64) FullUserID
NewPlainUserID creates a new FullUserID with the given ID as a plain text.
func NewPrivateUserID ¶
func NewPrivateUserID(id int64, encKey, hmacKey *EncryptionKey) (FullUserID, error)
NewPrivateUserID creates a new FullUserID with the given ID as an encrypted text. It do not store plain user ID, only encrypted and HMAC parts.
func (FullUserID) ID ¶
func (u FullUserID) ID(encKeys ...*EncryptionKey) (int64, error)
ID returns plain user ID if it is set. Otherwise it tries to decrypt encrypted ID. If encryption keys are provided, it tries to decrypt it with each key and returns plain user ID. If key is not provided, it returns error. If there is an error during decryption, it returns error.
func (FullUserID) IsEmpty ¶
func (u FullUserID) IsEmpty() bool
IsEmpty returns true if all fields are nil.
func (FullUserID) String ¶
func (u FullUserID) String() string
String returns plain user ID as a string if it is set, otherwise returns "[ENCRYPTED]".
type HandlerFunc ¶
HandlerFunc represents a function that is used to handle user actions in bot.
type InitBundle ¶
type InitBundle struct {
// Handler is a handler for [State]. It will be called for initing user.
// It is required.
Handler HandlerFunc
// Data is a callback data, that can be obtained from [Context.Data] inside [HandlerFunc].
// It is optional and shoud be provided if you use [Context.Data] in your [InitBundle.Handler].
Data string
// Text is a text of simulating message, that can be obtained from [Context.Text] inside [HandlerFunc].
// It is optional and shoud be provided if you use [Context.Text] in your [InitBundle.Handler].
Text string
}
InitBundle is a struct for initing users of the bot. You should provide it to Bot.Start method. You should create a mapping of every State to the corresponding HandlerFunc.
type Keyboard ¶
type Keyboard struct {
// contains filtered or unexported fields
}
Keyboard is a ReplyMarkup (keyboard) builder.
func NewKeyboard ¶
NewKeyboard creates new keyboard builder.
func NewKeyboardWithLength ¶
func NewKeyboardWithLength(runeType RuneSizeType, optionalRowLen ...int) *Keyboard
NewKeyboardWithLength creates new keyboard builder with max runes in a row. It creates a new row in Add if number of runes is greater than max runes in row for selected rune type.
func (*Keyboard) Add ¶
Add adds buttons to the current row. It creates a new row in Add if number of buttons is greater than max buttons in row. It creates a new row in Add if number of runes is greater than max runes in row for selected rune type.
func (*Keyboard) AddRow ¶
AddRow adds buttons to the current row. It creates a new row if there is buttons in the current row after Add.
func (*Keyboard) CreateInlineMarkup ¶
func (k *Keyboard) CreateInlineMarkup() *tele.ReplyMarkup
CreateInlineMarkup creates inline keyboard from the current keyboard builder.
func (*Keyboard) CreateReplyMarkup ¶
func (k *Keyboard) CreateReplyMarkup(oneTime bool) *tele.ReplyMarkup
CreateReplyMarkup creates reply keyboard from the current keyboard builder.
func (*Keyboard) StartNewRow ¶
StartNewRow creates a new row.
type KeyboardWithContext ¶
type KeyboardWithContext struct {
*Keyboard
// contains filtered or unexported fields
}
func NewKeyboardWithContext ¶
func NewKeyboardWithContext(ctx Context, optionalRowLen ...int) *KeyboardWithContext
NewKeyboardWithContext creates new keyboard builder with context. With context you can create buttons using keyboard.
func (*KeyboardWithContext) AB ¶
func (k *KeyboardWithContext) AB(name string, callback HandlerFunc, dataList ...string) *Keyboard
AB is a shortcut for AddBtn. It creates a button and adds it to the current row. It creates a new row in Add if number of buttons is greater than max buttons in row. It creates a new row in Add if number of runes is greater than max runes in row for selected rune type.
func (*KeyboardWithContext) ABR ¶
func (k *KeyboardWithContext) ABR(name string, callback HandlerFunc, dataList ...string) *Keyboard
ABR is a shortcut for AddBtnRow. It creates a button and adds it to the current row. It creates a new row if there is buttons in the current row after Add.
func (*KeyboardWithContext) AddBtn ¶
func (k *KeyboardWithContext) AddBtn(name string, callback HandlerFunc, dataList ...string) *Keyboard
Add creates and adds button to the current row. It creates a new row in Add if number of buttons is greater than max buttons in row. It creates a new row in Add if number of runes is greater than max runes in row for selected rune type.
func (*KeyboardWithContext) AddBtnRow ¶
func (k *KeyboardWithContext) AddBtnRow(name string, callback HandlerFunc, dataList ...string) *Keyboard
AddRow creates and adds button to the current row. It creates a new row if there is buttons in the current row after Add.
type KeysProvider ¶
type KeysProvider interface {
// GetEncryptionKey returns the encryption key for the bot.
// It is used to encrypt and decrypt UserID in strict privacy mode.
GetEncryptionKey() *EncryptionKey
// GetHMACKey returns the HMAC key for the bot.
// It is used to HMAC UserID in strict privacy mode.
GetHMACKey() *EncryptionKey
}
KeysProvider is an interface for providing encryption and HMAC keys in strict privacy mode.
type Language ¶
type Language string
const ( LanguageAfrikaans Language = "af" LanguageAlbanian Language = "sq" LanguageAmharic Language = "am" LanguageArabic Language = "ar" LanguageArmenian Language = "hy" LanguageAssamese Language = "as" LanguageAzerbaijani Language = "az" LanguageBashkir Language = "ba" LanguageBasque Language = "eu" LanguageBelarusian Language = "be" LanguageBengali Language = "bn" LanguageBosnian Language = "bs" LanguageBreton Language = "br" LanguageBulgarian Language = "bg" LanguageBurmese Language = "my" LanguageCatalan Language = "ca" LanguageCentralKurdish Language = "ckb" LanguageChinese Language = "zh" LanguageCorsican Language = "co" LanguageCroatian Language = "hr" LanguageCzech Language = "cs" LanguageDanish Language = "da" LanguageDari Language = "prs" LanguageDivehi Language = "dv" LanguageDutch Language = "nl" LanguageEnglish Language = "en" LanguageEstonian Language = "et" LanguageFaroese Language = "fo" LanguageFilipino Language = "fil" LanguageFinnish Language = "fi" LanguageFrench Language = "fr" LanguageFrisian Language = "fy" LanguageGalician Language = "gl" LanguageGeorgian Language = "ka" LanguageGerman Language = "de" LanguageGilbertese Language = "gil" LanguageGreek Language = "el" LanguageGreenlandic Language = "kl" LanguageGujarati Language = "gu" LanguageHausa Language = "ha" LanguageHebrew Language = "he" LanguageHindi Language = "hi" LanguageHungarian Language = "hu" LanguageIcelandic Language = "is" LanguageIgbo Language = "ig" LanguageIndonesian Language = "id" LanguageInuktitut Language = "iu" LanguageIrish Language = "ga" LanguageItalian Language = "it" LanguageJapanese Language = "ja" LanguageKiche Language = "quc" LanguageKannada Language = "kn" LanguageKazakh Language = "kk" LanguageKhmer Language = "km" LanguageKinyarwanda Language = "rw" LanguageKiswahili Language = "sw" LanguageKonkani Language = "kok" LanguageKorean Language = "ko" LanguageKurdish Language = "ku" LanguageKyrgyz Language = "ky" LanguageLao Language = "lo" LanguageLatvian Language = "lv" LanguageLithuanian Language = "lt" LanguageLowerSorbian Language = "dsb" LanguageLuxembourgish Language = "lb" LanguageMacedonian Language = "mk" LanguageMalay Language = "ms" LanguageMalayalam Language = "ml" LanguageMaltese Language = "mt" LanguageMaori Language = "mi" LanguageMapudungun Language = "arn" LanguageMarathi Language = "mr" LanguageMohawk Language = "moh" LanguageMongolian Language = "mn" LanguageMoroccanArabic Language = "ary" LanguageNepali Language = "ne" LanguageNorwegian Language = "no" LanguageNorwegianBokmal Language = "nb" LanguageNorwegianNynorsk Language = "nn" LanguageOccitan Language = "oc" LanguageOdia Language = "or" LanguagePapiamento Language = "pap" LanguagePashto Language = "ps" LanguagePersian Language = "fa" LanguagePolish Language = "pl" LanguagePortuguese Language = "pt" LanguagePunjabi Language = "pa" LanguageQuechua Language = "qu" LanguageRomanian Language = "ro" LanguageRomansh Language = "rm" LanguageRussian Language = "ru" LanguageSamiInari Language = "smn" LanguageSamiLule Language = "smj" LanguageSamiNorthern Language = "se" LanguageSamiSkolt Language = "sms" LanguageSamiSouthern Language = "sma" LanguageSanskrit Language = "sa" LanguageScottishGaelic Language = "gd" LanguageSerbian Language = "sr" LanguageSesotho Language = "st" LanguageSinhala Language = "si" LanguageSlovak Language = "sk" LanguageSlovenian Language = "sl" LanguageSpanish Language = "es" LanguageSwedish Language = "sv" LanguageSwissGerman Language = "gsw" LanguageSyriac Language = "syc" LanguageTajik Language = "tg" LanguageTamazight Language = "tzm" LanguageTamil Language = "ta" LanguageTatar Language = "tt" LanguageTelugu Language = "te" LanguageThai Language = "th" LanguageTibetan Language = "bo" LanguageTswana Language = "tn" LanguageTurkish Language = "tr" LanguageTurkmen Language = "tk" LanguageUkrainian Language = "uk" LanguageUpperSorbian Language = "hsb" LanguageUrdu Language = "ur" LanguageUyghur Language = "ug" LanguageUzbek Language = "uz" LanguageVietnamese Language = "vi" LanguageWelsh Language = "cy" LanguageWolof Language = "wo" LanguageXhosa Language = "xh" LanguageYakut Language = "sah" LanguageYi Language = "ii" LanguageYoruba Language = "yo" LanguageZulu Language = "zu" )
Language constants based on IETF subtags
const (
LanguageDefault Language = "en"
)
func MustLanguage ¶
MustLanguage parses a language code string and returns the corresponding Language constant. It panics if the language code is not recognized. Use this function when you are certain the language code is valid.
func ParseLanguage ¶
ParseLanguage parses a language code string and returns the corresponding Language constant. It accepts language codes in any case (e.g., "en", "EN", "En"). Returns an error if the language code is not recognized.
func ParseLanguageOrDefault ¶
ParseLanguageOrDefault parses a language code string and returns the corresponding Language constant. If the language code is not recognized, it returns the default language.
type LogConfig ¶
type LogConfig struct {
// Enable is a flag that enables logging of bot activity (except updates logging).
// Use default slog to stderr if another logger is not provided using [WithLogger] option.
// Default: true.
// Environment variable: BOTE_ENABLE_LOGGING.
Enable *bool `yaml:"enable" json:"enable" env:"BOTE_LOG_ENABLE"`
// LogUpdates is a flag that enables logging of bot updates.
// Use default slog to stderr if another logger is not provided using [WithUpdateLogger] option.
// Default: true.
// Environment variable: BOTE_LOG_UPDATES.
LogUpdates *bool `yaml:"log_updates" json:"log_updates" env:"BOTE_LOG_UPDATES"`
// Level is the log level. Logger will log messages with level greater than or equal to this level.
// Default: info.
// Possible values:
// - "debug"
// - "info"
// - "warn"
// - "error"
// Environment variable: BOTE_LOG_LEVEL.
Level string `yaml:"level" json:"level" env:"BOTE_LOG_LEVEL"`
// HideUserData is a flag that enables hiding user messages, callback data and pressed buttons from logs.
// Default: false (even in strict privacy because in strict we use HMAC user_id that prevents from identification)
// Environment variable: BOTE_LOG_HIDE_USER_DATA.
HideUserData bool `yaml:"hide_user_data" json:"hide_user_data" env:"BOTE_LOG_HIDE_USER_DATA"`
// DebugIncomingUpdates is a flag that enables logging of incoming updates.
// It is not for production use.
// Default: false.
// Environment variable: BOTE_LOG_DEBUG_INCOMING_UPDATES.
DebugIncomingUpdates bool `yaml:"debug_incoming_updates" json:"debug_incoming_updates" env:"BOTE_LOG_DEBUG_INCOMING_UPDATES"`
}
type Logger ¶
type Logger interface {
Debug(string, ...any)
Info(string, ...any)
Warn(string, ...any)
Error(string, ...any)
}
Logger is an interface for logging messages.
type LongPollingConfig ¶
type LongPollingConfig struct {
// Timeout is the long polling timeout.
// Default: 15 seconds.
// Environment variable: BOTE_LP_TIMEOUT.
Timeout time.Duration `yaml:"timeout" json:"timeout" env:"BOTE_LP_TIMEOUT"`
// Limit is the maximum number of updates to be returned in a single request.
// Default: 0, no limit.
// Environment variable: BOTE_LP_LIMIT.
Limit int `yaml:"limit" json:"limit" env:"BOTE_LP_LIMIT"`
// LastUpdateID is the last update ID to be returned.
// It sets an offset for the starting update to return.
// It is used to continue long polling from the last update ID.
// Default: 0.
// Environment variable: BOTE_LP_LAST_UPDATE_ID.
LastUpdateID int `yaml:"last_update_id" json:"last_update_id" env:"BOTE_LP_LAST_UPDATE_ID"`
// AllowedUpdates is a list of update types the bot wants to receive.
// Empty list means all update types.
// Possible values:
// message
// edited_message
// channel_post
// edited_channel_post
// inline_query
// chosen_inline_result
// callback_query
// shipping_query
// pre_checkout_query
// poll
// poll_answer
//
// Environment variable: BOTE_ALLOWED_UPDATES (comma-separated).
AllowedUpdates []string `yaml:"allowed_updates" json:"allowed_updates" env:"BOTE_ALLOWED_UPDATES" envSeparator:","`
}
LongPollingConfig contains configuration for long polling-based bot operation.
type MessageBuilder ¶
MessageBuilder is a wrapper for strings.Builder with additional methods. You should not copy it. Empty value of MessageBuilder is ready to use.
func NewBuilder ¶
func NewBuilder(cap ...int) *MessageBuilder
NewBuilder creates a new Builder instance.
func (*MessageBuilder) IsEmpty ¶
func (b *MessageBuilder) IsEmpty() bool
IsEmpty returns true if the builder's length is 0.
func (*MessageBuilder) Write ¶
func (b *MessageBuilder) Write(msg ...string)
Write writes a string to the builder. It is an alias for strings.Builder.WriteString.
func (*MessageBuilder) WriteBytes ¶
func (b *MessageBuilder) WriteBytes(data ...[]byte)
WriteBytes writes a byte slice to the builder. It is an alias for strings.Builder.Write.
func (*MessageBuilder) WriteIf ¶
func (b *MessageBuilder) WriteIf(condition bool, writeTrue string, writeFalse ...string)
WriteIf writes either writeTrue or writeFalse depending on the value of first argument.
func (*MessageBuilder) WriteIfF ¶
func (b *MessageBuilder) WriteIfF(condition bool, writeTrue, writeFalse string, args ...any)
WriteIf writes either writeTrue or writeFalse depending on the value of first argument.
func (*MessageBuilder) Writebln ¶
func (b *MessageBuilder) Writebln(msg ...string)
Writebln writes a string to the builder and adds a newline at the beginning and a newline at the end.
func (*MessageBuilder) Writeblnf ¶
func (b *MessageBuilder) Writeblnf(msg string, args ...any)
Writeblnf writes a string to the builder and adds a newline at the beginning and a newline at the end. If args are provided, it uses fmt.Sprintf to format the string.
func (*MessageBuilder) Writebn ¶
func (b *MessageBuilder) Writebn(msg ...string)
Writebn writes a string to the builder and adds a newline at the beginning. It is an alias for strings.Builder.WriteString.
func (*MessageBuilder) Writebnf ¶
func (b *MessageBuilder) Writebnf(msg string, args ...any)
Writebnf writes a string to the builder and adds a newline at the beginning. It is an alias for strings.Builder.WriteString. If args are provided, it uses fmt.Sprintf to format the string.
func (*MessageBuilder) Writef ¶
func (b *MessageBuilder) Writef(msg string, args ...any)
Writef writes a string to the builder. It is an alias for strings.Builder.WriteString. If args are provided, it uses fmt.Sprintf to format the string.
func (*MessageBuilder) Writeln ¶
func (b *MessageBuilder) Writeln(s ...string)
Writeln writes a string to the builder and adds a newline at the end.
func (*MessageBuilder) WritelnIf ¶
func (b *MessageBuilder) WritelnIf(condition bool, writeTrue string, writeFalse ...string)
WritelnIf writes either writeTrue or writeFalse depending on the value of first argument. It is an alias for strings.Builder.WriteString.
func (*MessageBuilder) WritelnIfF ¶
func (b *MessageBuilder) WritelnIfF(condition bool, writeTrue, writeFalse string, args ...any)
WriteIf writes either writeTrue or writeFalse depending on the value of first argument.
func (*MessageBuilder) WritelnIfFf ¶
func (b *MessageBuilder) WritelnIfFf(condition bool, writeTrue string, args ...any)
WriteIf writes either writeTrue or writeFalse depending on the value of first argument.
func (*MessageBuilder) Writelnf ¶
func (b *MessageBuilder) Writelnf(s string, args ...any)
Writeln writes a string to the builder and adds a newline at the end. If args are provided, it uses fmt.Sprintf to format the string.
func (*MessageBuilder) Writelnn ¶
func (b *MessageBuilder) Writelnn(s ...string)
Writelnn writes a string to the builder and adds two newlines at the end.
func (*MessageBuilder) Writelnnf ¶
func (b *MessageBuilder) Writelnnf(s string, args ...any)
Writelnn writes a string to the builder and adds two newlines at the end. If args are provided, it uses fmt.Sprintf to format the string.
type MessageProvider ¶
type MessageProvider interface {
// Messages returns messages for a specific language.
Messages(language Language) Messages
}
MessageProvider is an interface for providing messages based on the user language code.
type Messages ¶
type Messages interface {
// CloseBtn is a message on inline keyboard button that closes the error message.
// Remain it empty if you don't want to show this button.
CloseBtn() string
// GeneralError is a general error message that sends when an unhandled error occurs.
GeneralError() string
// PrepareMessage calls before every Send, SendMain, Edit, EditMain or EditHistory.
// Provide zero msgID in Send and SendMain methods.
// If isHistorical is true, it means that the message is a history message called by EditHistory.
PrepareMessage(msg string, u User, newState State, msgID int, isHistorical bool) string
}
Messages is a collection of messages for a specific language.
type MessagesState ¶
type MessagesState struct {
// Main is the main state of the user, state of the Main message.
Main UserState `bson:"main" json:"main" db:"main"`
// MessageStates contains all states of the user for all messages. It is a map of message_id -> state.
MessageStates map[int]UserState `bson:"message_states" json:"message_states" db:"message_states"`
// MessagesAwaitingText is a unique stack that contains all messages IDs that awaits text.
// Every message can produce text state and they should be handled as LIFO.
MessagesAwaitingText []int `bson:"messages_awaiting_text" json:"messages_awaiting_text" db:"messages_awaiting_text"`
// contains filtered or unexported fields
}
MessagesState contains current user state and state history. State connects to message, every Main and Info message has its own state.
type MetricsConfig ¶
type MetricsConfig struct {
Registry *prometheus.Registry
Namespace string
Subsystem string
ConstLabels prometheus.Labels
}
type MiddlewareFunc ¶
MiddlewareFunc represents a function that called on every bot update.
type MiddlewareFuncTele ¶
MiddlewareFuncTele represents a function that called on every bot update in telebot format.
type Options ¶
type Options struct {
// Config contains bote configuration. It is optional and has default values for all fields.
// You also can set values using environment variables.
Config Config
// UserDB is a storage for users. It uses in-memory storage by default.
// You should implement it in your application if you want to persist users between applicataion restarts.
UserDB UsersStorage
// Msgs is a message provider. It uses default messages by default.
// You should implement it in your application if you want to use custom messages.
Msgs MessageProvider
// Logger is a logger. It uses slog logger by default if EnableLogging == true (by default).
Logger Logger
// UpdateLogger is a logger for updates. It uses Logger and logs in debug level by default.
// It will log updates even if EnableLogging == false.
// You should set LogUpdates == false to disable updates logging.
UpdateLogger UpdateLogger
// Poller is a poller for the bot. It uses default poller by default.
// You should implement it in your application if you want to use custom poller (e.g. for testing).
// Note: If WebhookConfig is provided, this will be ignored and webhook poller will be used.
Poller tele.Poller
// Metrics is a configuration for prometheus metrics.
// It registers metrics in the provided registry.
// It do not register metris if Registry is nil.
Metrics MetricsConfig
// Offline is a flag that enables offline mode.
// It is used to create a bot without network for testing purposes.
Offline bool
// KeysProvider is a provider of encryption and HMAC keys for the bot.
// It is used to provide encryption and HMAC keys for the bot in strict privacy mode.
KeysProvider KeysProvider
// contains filtered or unexported fields
}
Options contains bote additional options.
type PollingMode ¶
type PollingMode string
PollingMode is the polling mode to use.
const ( PollingModeLong PollingMode = "long" PollingModeWebhook PollingMode = "webhook" PollingModeCustom PollingMode = "custom" )
type PrivacyConfig ¶
type PrivacyConfig struct {
// Mode is the privacy mode for the bot.
// Default: "no".
// Possible values:
// - "no" - no privacy mode (all data is stored and logged)
// - "low" - low privacy mode (store UserID + Username)
// - "strict" - strict mode (only encrypted and HMACed UserID is stored)
// Environment variable: BOTE_PRIVACY_MODE.
Mode PrivacyMode `yaml:"mode" json:"mode" env:"BOTE_PRIVACY_MODE"`
// EncryptionKey is the encryption key for the bot.
// It is used to encrypt and decrypt UserID in strict privacy mode.
// Key should be a hex encoded string of 32 bytes.
// Default: nil.
// Environment variable: BOTE_ENCRYPTION_KEY.
EncryptionKey *string `yaml:"encryption_key" json:"encryption_key" env:"BOTE_ENCRYPTION_KEY"`
// EncryptionKeyVersion is the version of the encryption key.
// It is used to identify the version of the encryption key.
// Default: nil.
// Environment variable: BOTE_ENCRYPTION_KEY_VERSION.
EncryptionKeyVersion *int64 `yaml:"enc_key_version" json:"enc_key_version" env:"BOTE_ENCRYPTION_KEY_VERSION"`
// HMACKey is the HMAC key for the bot.
// It is used to HMAC UserID in strict privacy mode.
// Key should be a hex encoded string of 32 bytes.
// Default: nil.
// Environment variable: BOTE_HMAC_KEY.
HMACKey *string `yaml:"hmac_key" json:"hmac_key" env:"BOTE_HMAC_KEY"`
// HMACKeyVersion is the version of the HMAC key.
// It is used to identify the version of the HMAC key.
// Default: nil.
// Environment variable: BOTE_HMAC_KEY_VERSION.
HMACKeyVersion *int64 `yaml:"hmac_key_version" json:"hmac_key_version" env:"BOTE_HMAC_KEY_VERSION"`
}
type PrivacyMode ¶
type PrivacyMode string
PrivacyMode is a privacy mode for the bot. It is used to make compliance with GDPR (privacy by design). Possible values: - "low" - low privacy mode (UserID + Username are stored) - "strict" - strict mode (only encrypted and HMACed UserID is stored)
const ( PrivacyModeLow PrivacyMode = "low" PrivacyModeStrict PrivacyMode = "strict" )
func (PrivacyMode) IsStrict ¶
func (p PrivacyMode) IsStrict() bool
type RuneSizeType ¶
type RuneSizeType string
RuneSizeType sets type of UTF-8 runes in button text. For example, if you use English language, you should use OneBytePerRune. If you use Russian language, you should use TwoBytesPerRune. If you add a lot of emojis of special symbols, you should use FourBytesPerRune.
const ( OneBytePerRune RuneSizeType = "OneBytePerRune" TwoBytesPerRune RuneSizeType = "TwoBytesPerRune" FourBytesPerRune RuneSizeType = "FourBytesPerRune" MaxDataLengthBytes = 64 // 56 )
type State ¶
State is a user state in Telegram bot builded using this package. User changes states when he makes actions, e.g. sends a message, clicks a button, etc. State is connected to message, every Main and Info (history) message has its own state. State is necessary for understanding user behavior and should be used in user init after bot restarting. You should create states as constants in your application and pass it in Send or Edit methods as first argument. States is generally changes in response to user actions inside handlers, but also can be changed in other places in case of any action.
type UpdateLogger ¶
type UpdateLogger interface {
Log(UpdateType, ...any)
}
UpdateLogger is an interface for logging updates.
type UpdateType ¶
type UpdateType string
UpdateType is a type of update that is using in update logging.
const ( MessageUpdate UpdateType = "message" CallbackUpdate UpdateType = "callback" NotPrivateUpdate UpdateType = "not_private" )
func (UpdateType) String ¶
func (t UpdateType) String() string
type User ¶
type User interface {
// ID returns plain Telegram user ID if it is called from [Context.User] (in handler)
ID() int64
// IDFull returns full user ID with encrypted and HMAC parts if they are set.
IDFull() FullUserID
// Username returns Telegram username (without @).
// It is empty if privacy mode is strict.
Username() string
// Language returns Telegram user language code.
Language() Language
// Info returns user info.
// It is empty if privacy mode is strict.
Info() UserInfo
// State returns current state for the given message ID.
State(msgID int) (State, bool)
// StateMain returns state for the Main message.
StateMain() State
// Messages returns all message IDs.
Messages() UserMessages
// IsDisabled returns true if user disabled the bot.
IsDisabled() bool
// String returns user string representation in format '[@username|id]'.
String() string
// Stats returns user stats.
Stats() UserStat
// LastSeenTime returns the time when user interacts with provided message.
// If message ID is not provided, it returns the time when user interacts with bot's any message.
LastSeenTime(optionalMsgID ...int) time.Time
// UpdateLanguage updates user language.
UpdateLanguage(language Language)
// GetValue returns value from user context.
GetValue(key string) (any, bool)
// SetValue sets value in user context (persistent).
SetValue(key string, value any)
// DeleteValue deletes value from user context (persistent).
DeleteValue(key string)
// ClearCache deletes all values from SetValue function.
ClearCache()
}
User is an interface that represents user context in the bot.
type UserInfo ¶
type UserInfo struct {
// Username is Telegram username (without @).
// It is empty if privacy mode is strict.
Username string `bson:"username,omitempty" json:"username,omitempty" db:"username,omitempty"`
// FirstName is Telegram user first name.
// It is empty if privacy mode is enabled.
FirstName string `bson:"first_name,omitempty" json:"first_name,omitempty" db:"first_name,omitempty"`
// LastName is Telegram user last name.
// It is empty if privacy mode is enabled.
LastName string `bson:"last_name,omitempty" json:"last_name,omitempty" db:"last_name,omitempty"`
// IsPremium is true if Telegram user has Telegram Premium.
// It is empty if privacy mode is strict.
IsPremium *bool `bson:"is_premium,omitempty" json:"is_premium,omitempty" db:"is_premium,omitempty"`
}
UserInfo contains user info, that can be obtained from Telegram.
type UserInfoDiff ¶
type UserInfoDiff struct {
FirstName *string `bson:"first_name" json:"first_name" db:"first_name"`
LastName *string `bson:"last_name" json:"last_name" db:"last_name"`
Username *string `bson:"username" json:"username" db:"username"`
IsPremium *bool `bson:"is_premium" json:"is_premium" db:"is_premium"`
}
UserInfoDiff contains changes that should be applied to user info.
type UserMessages ¶
type UserMessages struct {
// Main message is the last and primary one in the chat.
MainID int `bson:"main_id" json:"main_id" db:"main_id"`
// Head message is sent right before main message for making bot more interactive.
HeadID int `bson:"head_id" json:"head_id" db:"head_id"`
// Notification message can be sent in any time. Old notification message will be deleted when new one is sent.
NotificationID int `bson:"notification_id" json:"notification_id" db:"notification_id"`
// Error message can be sent in any time in case of error and deleted automically after next action.
ErrorID int `bson:"error_id" json:"error_id" db:"error_id"`
// History message is the previous main messages. Main message becomes History message after new Main sending.
HistoryIDs []int `bson:"history_ids" json:"history_ids" db:"history_ids"`
// LastActions contains time of last interaction of user with every message.
LastActions map[int]time.Time `bson:"last_actions" json:"last_actions" db:"last_actions"`
}
UserMessages contains IDs of user messages.
func (UserMessages) HasMsgID ¶
func (m UserMessages) HasMsgID(msgID int) bool
type UserMessagesDiff ¶
type UserMessagesDiff struct {
MainID *int `bson:"main_id" json:"main_id" db:"main_id"`
HeadID *int `bson:"head_id" json:"head_id" db:"head_id"`
NotificationID *int `bson:"notification_id" json:"notification_id" db:"notification_id"`
ErrorID *int `bson:"error_id" json:"error_id" db:"error_id"`
HistoryIDs []int `bson:"history_ids" json:"history_ids" db:"history_ids"`
LastActions map[int]time.Time `bson:"last_actions" json:"last_actions" db:"last_actions"`
}
UserMessagesDiff contains changes that should be applied to user messages.
type UserModel ¶
type UserModel struct {
// ID is user ID.
ID FullUserID `bson:"id" json:"id" db:"id"`
// LanguageCode is Telegram user language code.
// It is not encrypted even in strict privacy mode because it has low cardinality and cannot be used for identification.
LanguageCode Language `bson:"language_code" json:"language_code" db:"language_code"`
// ForceLanguageCode is a custom language code for user that can be set by user actions in bot.
// It is not encrypted even in strict privacy mode because it has low cardinality and cannot be used for identification.
ForceLanguageCode Language `bson:"force_language_code" json:"force_language_code" db:"force_language_code"`
// Info contains user info, that can be obtained from Telegram.
// It is empty if privacy mode is strict.
Info UserInfo `bson:"info" json:"info" db:"info"`
// Messages contains IDs of user messages.
Messages UserMessages `bson:"messages" json:"messages" db:"messages"`
// State contains state for every user's message.
State MessagesState `bson:"state" json:"state" db:"state"`
// Stats contains user stats.
Stats UserStat `bson:"stats" json:"stats" db:"stats"`
// IsBot is true if Telegram user is a bot.
IsBot bool `bson:"is_bot" json:"is_bot" db:"is_bot"`
// IsDisabled returns true if user is disabled. Disabled means that user blocks bot.
IsDisabled bool `bson:"is_disabled" json:"is_disabled" db:"is_disabled"`
// Values is a map of user values.
Values map[string]any `bson:"values" json:"values" db:"values"`
}
UserModel is a structure that represents user in DB.
type UserModelDiff ¶
type UserModelDiff struct {
LanguageCode *Language `bson:"language_code" json:"language_code" db:"language_code"`
ForceLanguageCode *Language `bson:"force_language_code" json:"force_language_code" db:"force_language_code"`
Info *UserInfoDiff `bson:"info" json:"info" db:"info"`
Messages *UserMessagesDiff `bson:"messages" json:"messages" db:"messages"`
State *UserStateDiff `bson:"state" json:"state" db:"state"`
Stats *UserStatDiff `bson:"stats" json:"stats" db:"stats"`
IsDisabled *bool `bson:"is_disabled" json:"is_disabled" db:"is_disabled"`
IsBot *bool `bson:"is_bot" json:"is_bot" db:"is_bot"`
Values map[string]any `bson:"values" json:"values" db:"values"`
}
UserModelDiff contains changes that should be applied to user.
type UserStat ¶
type UserStat struct {
// NumberOfStateChangesTotal is the total number of actions user made.
NumberOfStateChangesTotal int `bson:"number_of_state_changes_total" json:"number_of_state_changes_total" db:"number_of_state_changes_total"`
// LastSeenTime is the last time user interacted with the bot.
LastSeenTime time.Time `bson:"last_seen_time" json:"last_seen_time" db:"last_seen_time"`
// CreatedTime is the time when user was created.
CreatedTime time.Time `bson:"created_time" json:"created_time" db:"created_time"`
// DisabledTime is the time when user was disabled.
DisabledTime time.Time `bson:"disabled_time" json:"disabled_time" db:"disabled_time"`
}
type UserStatDiff ¶
type UserStatDiff struct {
NumberOfStateChanges *int `bson:"number_of_state_changes_total" json:"number_of_state_changes_total" db:"number_of_state_changes_total"`
LastSeenTime *time.Time `bson:"last_seen_time" json:"last_seen_time" db:"last_seen_time"`
DisabledTime *time.Time `bson:"disabled_time" json:"disabled_time" db:"disabled_time"`
}
type UserState ¶
type UserState string
UserState is a bote implementation of State interface.
const ( // NoChange is a state that means that user state should not be changed after Send of Edit. NoChange UserState = "" // FirstRequest is a state of a user after first request to bot. FirstRequest UserState = "first_request" // Unknown is a state of a user when hiw real state is not provided after creation. Unknown UserState = "unknown" // Disabled is a state of a disabled user. Disabled UserState = "disabled" )
func ConvertUserState ¶
func NewUserState ¶
func (UserState) NotChanged ¶
type UserStateDiff ¶
type UserStateDiff struct {
Main *UserState `bson:"main" json:"main" db:"main"`
MessageStates map[int]UserState `bson:"message_states" json:"message_states" db:"message_states"`
MessagesAwaitingText []int `bson:"messages_awaiting_text" json:"messages_awaiting_text" db:"messages_awaiting_text"`
}
UserStateDiff contains changes that should be applied to user state.
type UsersStorage ¶
type UsersStorage interface {
// Insert inserts user in storage.
// It is better to NOT modify document before saving to make everything working properly.
Insert(ctx context.Context, userModel UserModel) error
// Find returns user from storage. It returns true as a second argument if user was found without error.
Find(ctx context.Context, id FullUserID) (UserModel, bool, error)
// UpdateAsync updates user model in storage. The idea of that method is that it calls on every user action
// (e.g. for update state), so it should be async to make it faster for user (without IO latency).
// So this method doesn't accept context and doesn't return error because it should be called in async goroutine.
//
// Warning! You can't use simple worker pool, because updates should be ordered. If you don't want to
// make it async, you may use sync operation in this method and handle error using channels, for example.
// You may be intersting in https://github.com/maxbolgarin/mongox for async operations in MongoDB or
// https://github.com/maxbolgarin/gorder for general async queue if you use another db.
//
// If you want stable work of bote package, don't update user model by yourself. Bote will do it for you.
// If you want to expand user model by your additional fields, create an another table/collection in your db.
UpdateAsync(id FullUserID, userModel *UserModelDiff)
}
UsersStorage is a storage for users. You should implement it in your application if you want to persist users between applicataion restarts.
type WebhookConfig ¶
type WebhookConfig struct {
// URL is the webhook endpoint URL that Telegram will use to send updates.
// Must be HTTPS and accessible from the internet.
// Example: "https://yourdomain.com/bot/webhook"
URL string `yaml:"url" json:"url" env:"BOTE_WEBHOOK_URL"`
// Listen is the address to bind the webhook server to.
// Default: ":8443"
// Environment variable: BOTE_WEBHOOK_LISTEN.
Listen string `yaml:"listen" json:"listen" env:"BOTE_WEBHOOK_LISTEN"`
// ReadTimeout is the maximum duration for reading the entire request.
// Default: 30 seconds.
// Environment variable: BOTE_WEBHOOK_READ_TIMEOUT.
ReadTimeout time.Duration `yaml:"read_timeout" json:"read_timeout" env:"BOTE_WEBHOOK_READ_TIMEOUT"`
// IdleTimeout is the maximum amount of time to wait for the next request.
// Default: 120 seconds.
// Environment variable: BOTE_WEBHOOK_IDLE_TIMEOUT.
IdleTimeout time.Duration `yaml:"idle_timeout" json:"idle_timeout" env:"BOTE_WEBHOOK_IDLE_TIMEOUT"`
// MaxConnections is the maximum allowed number of simultaneous HTTPS connections to the webhook.
// Valid range: 1-100. Default: 40.
// Environment variable: BOTE_WEBHOOK_MAX_CONNECTIONS.
MaxConnections int `yaml:"max_connections" json:"max_connections" env:"BOTE_WEBHOOK_MAX_CONNECTIONS"`
// DropPendingUpdates specifies whether to drop pending updates when setting webhook.
// Default: false.
// Environment variable: BOTE_WEBHOOK_DROP_PENDING_UPDATES.
DropPendingUpdates bool `yaml:"drop_pending_updates" json:"drop_pending_updates" env:"BOTE_WEBHOOK_DROP_PENDING_UPDATES"`
// MetricsPath is the path to serve metrics.
// Default: "/metrics".
// Environment variable: BOTE_WEBHOOK_METRICS_PATH.
MetricsPath string `yaml:"metrics_path" json:"metrics_path" env:"BOTE_WEBHOOK_METRICS_PATH"`
// EnableMetrics is a flag that enables metrics serving.
// It uses provided registry to register metrics or a default one if registry is nil.
// Default: false.
// Environment variable: BOTE_WEBHOOK_ENABLE_METRICS.
EnableMetrics bool `yaml:"enable_metrics" json:"enable_metrics" env:"BOTE_WEBHOOK_ENABLE_METRICS"`
// Security contains security configuration.
Security WebhookSecurityConfig `yaml:"security" json:"security"`
// RateLimit contains rate limiting configuration.
RateLimit WebhookRateLimitConfig `yaml:"rate_limit" json:"rate_limit"`
// AllowedUpdates is a list of update types the bot wants to receive.
// Empty list means all update types.
// Possible values:
// message
// edited_message
// channel_post
// edited_channel_post
// inline_query
// chosen_inline_result
// callback_query
// shipping_query
// pre_checkout_query
// poll
// poll_answer
//
// Environment variable: BOTE_ALLOWED_UPDATES (comma-separated).
AllowedUpdates []string `yaml:"allowed_updates" json:"allowed_updates" env:"BOTE_ALLOWED_UPDATES" envSeparator:","`
// contains filtered or unexported fields
}
WebhookConfig contains configuration for webhook-based bot operation.
type WebhookRateLimitConfig ¶
type WebhookRateLimitConfig struct {
// Enabled enables rate limiting. Default: true.
// Environment variable: BOTE_WEBHOOK_RATE_LIMIT_ENABLED.
Enabled *bool `yaml:"enabled" json:"enabled" env:"BOTE_WEBHOOK_RATE_LIMIT_ENABLED"`
// RequestsPerSecond is the maximum requests per second allowed.
// Default: 30 (Telegram's recommended limit).
// Environment variable: BOTE_WEBHOOK_RATE_LIMIT_RPS.
RequestsPerSecond int `yaml:"requests_per_second" json:"requests_per_second" env:"BOTE_WEBHOOK_RATE_LIMIT_RPS"`
// BurstSize is the burst size for rate limiting.
// Default: 10.
// Environment variable: BOTE_WEBHOOK_RATE_LIMIT_BURST.
BurstSize int `yaml:"burst_size" json:"burst_size" env:"BOTE_WEBHOOK_RATE_LIMIT_BURST"`
}
WebhookRateLimitConfig contains rate limiting configuration.
type WebhookSecurityConfig ¶
type WebhookSecurityConfig struct {
// SecretToken is used for webhook request verification.
// Highly recommended for security. Will be generated automatically if not provided.
// Environment variable: BOTE_WEBHOOK_SECRET_TOKEN.
SecretToken string `yaml:"secret_token" json:"secret_token" env:"BOTE_WEBHOOK_SECRET_TOKEN"`
// StartHTTPS indicates if using HTTPS in the current server.
// If true, it will start current server as HTTPS server with provided certificate and key.
// If false, it will start current server as HTTP server.
// Default: false.
// Environment variable: BOTE_WEBHOOK_START_HTTPS.
StartHTTPS bool `yaml:"start_https" json:"start_https" env:"BOTE_WEBHOOK_START_HTTPS"`
// LoadCertInTelegram indicates that current certificate (public part) should be loaded to Telegram.
// It is required for Telegram webhook if you use self-signed certificate.
// If true, will upload certificate to Telegram and providing certificate is required.
// If false, it is expected that you use trusted certificate.
// Default: false.
// Environment variable: BOTE_WEBHOOK_LOAD_CERT_IN_TELEGRAM.
LoadCertInTelegram bool `yaml:"load_cert_in_telegram" json:"load_cert_in_telegram" env:"BOTE_WEBHOOK_LOAD_CERT_IN_TELEGRAM"`
// CertFile is the path to the TLS certificate file.
// Required if StartHTTPS is true or LoadCertInTelegram is true.
// If it is empty it is expected that there is a LB in front of the server that termmintaes TLS.
// HTTPS is strictly required for Telegram webhook.
// Environment variable: BOTE_WEBHOOK_CERT_FILE.
CertFile string `yaml:"cert_file" json:"cert_file" env:"BOTE_WEBHOOK_CERT_FILE"`
// KeyFile is the path to the TLS private key file.
// Required if StartHTTPS is true or LoadCertInTelegram is true.
// If it is empty it is expected that there is a LB in front of the server that termmintaes TLS.
// HTTPS is strictly required for Telegram webhook.
// Environment variable: BOTE_WEBHOOK_KEY_FILE.
KeyFile string `yaml:"key_file" json:"key_file" env:"BOTE_WEBHOOK_KEY_FILE"`
// CheckTLSInRequest indicates that current server should check TLS in request.
// If true, it will check TLS in request and return 400 if it is not TLS.
// It will try to find TLS config in request or https in X-Forwarded-Proto header.
// If false, it will not check TLS in request.
// Default: true.
// Environment variable: BOTE_WEBHOOK_CHECK_TLS_IN_REQUEST.
CheckTLSInRequest *bool `yaml:"check_tls_in_request" json:"check_tls_in_request" env:"BOTE_WEBHOOK_CHECK_TLS_IN_REQUEST"`
// SecurityHeaders is a flag that enables security headers in response.
// Default: true.
// It will set the following headers:
// - X-Frame-Options: DENY
// - X-Content-Type-Options: nosniff
// - X-XSS-Protection: 1; mode=block
// - Referrer-Policy: strict-origin-when-cross-origin
// Environment variable: BOTE_WEBHOOK_SECURITY_HEADERS.
SecurityHeaders *bool `yaml:"security_headers" json:"security_headers" env:"BOTE_WEBHOOK_SECURITY_HEADERS"`
// GenerateSelfSignedCert is a flag that enables generation of self-signed certificate and upload it to Telegram.
// It will generate certificate and key files and upload them to Telegram.
// It will set StartHTTPS to true and LoadCertInTelegram to true.
// Default: false.
// Environment variable: BOTE_WEBHOOK_GENERATE_SELF_SIGNED_CERT.
GenerateSelfSignedCert *bool `yaml:"generate_self_signed_cert" json:"generate_self_signed_cert" env:"BOTE_WEBHOOK_GENERATE_SELF_SIGNED_CERT"`
// AllowedTelegramIPs is a flag that adds Telegram IPs to allowed IPs.
// Default: false.
// Environment variable: BOTE_WEBHOOK_ALLOW_TELEGRAM_IPS.
AllowTelegramIPs *bool `yaml:"allow_telegram_ips" json:"allow_telegram_ips" env:"BOTE_WEBHOOK_ALLOW_TELEGRAM_IPS"`
// AllowedIPs contains allowed IP addresses/CIDR blocks.
// Only requests from these IPs will be accepted.
// Default: [] to allow all IPs.
// Environment variable: BOTE_WEBHOOK_ALLOWED_IPS (comma-separated).
AllowedIPs []string `yaml:"allowed_ips" json:"allowed_ips" env:"BOTE_WEBHOOK_ALLOWED_IPS" envSeparator:","`
}