Added Clear() to examples and code simplification

bot-api-6.1
lupoDharkael 2016-12-16 02:07:54 +01:00
parent 0a95dda18a
commit c034de512a
2 changed files with 10 additions and 5 deletions

6
bot.go
View File

@ -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.

View File

@ -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