PaulSonOfLars / gotgbot

Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
MIT License
509 stars 114 forks source link

Token handling #176

Closed Borzoff closed 3 months ago

Borzoff commented 3 months ago

Description of the problem / feature request

Hello, how to handle the moment when the token stops working while Polling is already running

Feature requests

I want to handle the moment when the token revoke

Bugs


func startBot(token string, wg *sync.WaitGroup) {
    defer wg.Done()

    bot, err := gotgbot.NewBot(token, nil)
    if err != nil {
        if strings.Contains(err.Error(), "Unauthorized") {
            log.Printf("Invalid token for bot: %s", token)
            return
        }
        log.Printf("Failed to create a bot with the token %s: %v", token, err)
        return
    }
    dispatcher := ext.NewDispatcher(&ext.DispatcherOpts{
        // If an error is returned by a handler, log it and continue going.
        Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
            log.Println("an error occurred while handling update:", err.Error())
            return ext.DispatcherActionNoop
        },
        MaxRoutines: ext.DefaultMaxRoutines,
    })

    dispatcher.AddHandler(handlers.NewCommand("start", handler.Start))

    updater := ext.NewUpdater(dispatcher, nil)
    err = updater.StartPolling(bot, &ext.PollingOpts{
        DropPendingUpdates: true,
        GetUpdatesOpts: &gotgbot.GetUpdatesOpts{
            Timeout: 9,
            RequestOpts: &gotgbot.RequestOpts{
                Timeout: time.Second * 10,
            },
        },
    })
    if err != nil {
        log.Printf("Не удалось запустить polling для бота с токеном %s: %v", token, err)
    }
    log.Printf("%s bot launched", bot.User.Username)
    updater.Idle()
}

System information:

Borzoff commented 3 months ago

Found one of the options: Pass the UnhandledErrFunc parameter to Updater and process it there