Added Clear() to examples and code simplification
lupoDharkael izhe@hotmail.es
Fri, 16 Dec 2016 02:07:54 +0100
2 files changed,
10 insertions(+),
5 deletions(-)
M
bot.go
→
bot.go
@@ -451,7 +451,6 @@
// GetUpdatesChan starts and returns a channel for getting updates. func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (updatesChannel, error) { ch := make(chan Update, 100) - var updatesCh updatesChannel = ch go func() { for {@@ -473,13 +472,12 @@ }
} }() - return updatesCh, nil + return ch, nil } // ListenForWebhook registers a http handler for a webhook. func (bot *BotAPI) ListenForWebhook(pattern string) updatesChannel { ch := make(chan Update, 100) - var updatesCh updatesChannel = ch http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { bytes, _ := ioutil.ReadAll(r.Body)@@ -490,7 +488,7 @@
ch <- update }) - return updatesCh + return ch } // AnswerInlineQuery sends a response to an inline query.
M
bot_test.go
→
bot_test.go
@@ -1,13 +1,14 @@
package tgbotapi_test import ( - "github.com/go-telegram-bot-api/telegram-bot-api" "io/ioutil" "log" "net/http" "os" "testing" "time" + + "github.com/go-telegram-bot-api/telegram-bot-api" ) const (@@ -483,6 +484,12 @@ u := tgbotapi.NewUpdate(0)
u.Timeout = 60 updates, err := bot.GetUpdatesChan(u) + + //If you need to clear all the updates when bot just started: + //Wait to gather all the new updates (choose the time conveniently) + time.Sleep(time.Millisecond * 500) + //discard all the updates preventing the spam after your server has been down + updates.Clear() for update := range updates { if update.Message == nil {