From c034de512afbaa7ef63c2ac1cf6e55721cafff20 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Fri, 16 Dec 2016 02:07:54 +0100 Subject: [PATCH] Added Clear() to examples and code simplification --- bot.go | 6 ++---- bot_test.go | 9 ++++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bot.go b/bot.go index 373fa41..0f086fa 100644 --- a/bot.go +++ b/bot.go @@ -451,7 +451,6 @@ func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) { // 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 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (updatesChannel, error) { } }() - 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 @@ func (bot *BotAPI) ListenForWebhook(pattern string) updatesChannel { ch <- update }) - return updatesCh + return ch } // AnswerInlineQuery sends a response to an inline query. diff --git a/bot_test.go b/bot_test.go index 3ae19b4..40010d3 100644 --- a/bot_test.go +++ b/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 ( @@ -484,6 +485,12 @@ func ExampleNewBotAPI() { 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 { continue