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. // GetUpdatesChan starts and returns a channel for getting updates.
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (updatesChannel, error) { func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (updatesChannel, error) {
ch := make(chan Update, 100) ch := make(chan Update, 100)
var updatesCh updatesChannel = ch
go func() { go func() {
for { 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. // ListenForWebhook registers a http handler for a webhook.
func (bot *BotAPI) ListenForWebhook(pattern string) updatesChannel { func (bot *BotAPI) ListenForWebhook(pattern string) updatesChannel {
ch := make(chan Update, 100) ch := make(chan Update, 100)
var updatesCh updatesChannel = ch
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body) bytes, _ := ioutil.ReadAll(r.Body)
@ -490,7 +488,7 @@ func (bot *BotAPI) ListenForWebhook(pattern string) updatesChannel {
ch <- update ch <- update
}) })
return updatesCh return ch
} }
// AnswerInlineQuery sends a response to an inline query. // AnswerInlineQuery sends a response to an inline query.

View File

@ -1,13 +1,14 @@
package tgbotapi_test package tgbotapi_test
import ( import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/go-telegram-bot-api/telegram-bot-api"
) )
const ( const (
@ -484,6 +485,12 @@ func ExampleNewBotAPI() {
updates, err := bot.GetUpdatesChan(u) 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 { for update := range updates {
if update.Message == nil { if update.Message == nil {
continue continue