added StopReceivingUpdates to stop updates routine
There was no way to stop the bot from fetching updates, Added StopReceivingUpdates() so we can stop updates when we are done with bot.
This commit is contained in:
parent
690363a5f8
commit
6205cea402
1 changed files with 16 additions and 0 deletions
16
bot.go
16
bot.go
|
@ -28,6 +28,7 @@ type BotAPI struct {
|
|||
|
||||
Self User `json:"-"`
|
||||
Client *http.Client `json:"-"`
|
||||
shutdownChannel chan interface{}
|
||||
}
|
||||
|
||||
// NewBotAPI creates a new BotAPI instance.
|
||||
|
@ -46,6 +47,7 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
|
|||
Token: token,
|
||||
Client: client,
|
||||
Buffer: 100,
|
||||
shutdownChannel: make(chan interface{}),
|
||||
}
|
||||
|
||||
self, err := bot.GetMe()
|
||||
|
@ -480,6 +482,12 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
|
|||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-bot.shutdownChannel:
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
updates, err := bot.GetUpdates(config)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -501,6 +509,14 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
|
|||
return ch, nil
|
||||
}
|
||||
|
||||
// StopReceivingUpdates stops the go routine which receives updates
|
||||
func (bot *BotAPI) StopReceivingUpdates() {
|
||||
if bot.Debug {
|
||||
log.Println("Stopping the update receiver routine...")
|
||||
}
|
||||
close(bot.shutdownChannel)
|
||||
}
|
||||
|
||||
// ListenForWebhook registers a http handler for a webhook.
|
||||
func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
|
||||
ch := make(chan Update, bot.Buffer)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue