Extract bot command setup into trySetBotCommands

This commit is contained in:
MHSanaei 2026-04-22 10:47:30 +02:00
parent 9611c9def6
commit 8f30d14716
No known key found for this signature in database
GPG key ID: 7E4060F2FBE5AB7A

View file

@ -249,18 +249,7 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
return err return err
} }
// After bot initialization, set up bot commands with localized descriptions t.trySetBotCommands(bot)
err = bot.SetMyCommands(context.Background(), &telego.SetMyCommandsParams{
Commands: []telego.BotCommand{
{Command: "start", Description: t.I18nBot("tgbot.commands.startDesc")},
{Command: "help", Description: t.I18nBot("tgbot.commands.helpDesc")},
{Command: "status", Description: t.I18nBot("tgbot.commands.statusDesc")},
{Command: "id", Description: t.I18nBot("tgbot.commands.idDesc")},
},
})
if err != nil {
logger.Warning("Failed to set bot commands:", err)
}
// Start receiving Telegram bot messages // Start receiving Telegram bot messages
tgBotMutex.Lock() tgBotMutex.Lock()
@ -274,6 +263,26 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
return nil return nil
} }
func (t *Tgbot) trySetBotCommands(bot *telego.Bot) {
defer func() {
if r := recover(); r != nil {
logger.Warning("Failed to register bot commands (Telegram may be rate-limiting); bot will continue without them:", r)
}
}()
err := bot.SetMyCommands(context.Background(), &telego.SetMyCommandsParams{
Commands: []telego.BotCommand{
{Command: "start", Description: t.I18nBot("tgbot.commands.startDesc")},
{Command: "help", Description: t.I18nBot("tgbot.commands.helpDesc")},
{Command: "status", Description: t.I18nBot("tgbot.commands.statusDesc")},
{Command: "id", Description: t.I18nBot("tgbot.commands.idDesc")},
},
})
if err != nil {
logger.Warning("Failed to set bot commands:", err)
}
}
// createRobustFastHTTPClient creates a fasthttp.Client with proper connection handling // createRobustFastHTTPClient creates a fasthttp.Client with proper connection handling
func (t *Tgbot) createRobustFastHTTPClient(proxyUrl string) *fasthttp.Client { func (t *Tgbot) createRobustFastHTTPClient(proxyUrl string) *fasthttp.Client {
client := &fasthttp.Client{ client := &fasthttp.Client{