telegram-bot-api
telegram-bot-api copied to clipboard
Deal with useless StopReceivingUpdates method
StopReceivingUpdates
method is useless for real cases. Let's take a code from README, remove non-relevant parts and add StopReceivingUpdates() to it:
package main
import (
"os"
"os/signal"
"syscall"
"log"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
// Stop receiving updates when app receives shutdown signal.
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
go func() {
s := <-shutdown
signal.Stop(shutdown)
log.Printf("Got a %s signal, stop receiving updates, app is closing...", s)
bot.StopReceivingUpdates()
}()
for update := range updates {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
}
}
Calling StopReceivingUpdates()
doesn't stop to receive updates, because it waits <60 seconds to return from bot.GetUpdates
(https://github.com/go-telegram-bot-api/telegram-bot-api/blob/master/bot.go#L494). If bot.GetUpdates()
will return some updates - they will be sent to update channel and you will see logs.
Also, sending SIGINT/SIGTERM to a process doesn't make it to shutdown. Calling StopReceivingUpdates()
just stops to call bot.GetUpdates()
in a loop, but doesn't close an update channel, which is totally useless after StopReceivingUpdates()
call, because the goroutine which could send a message to it was already stopped.
This method should be removed or fixed. Fix must be the following: it should cancel current polling request and close update channel.
Does this help?
https://github.com/go-telegram-bot-api/telegram-bot-api/pull/218