all repos — telegram-bot-api @ 66446d51ffc393a94dc510f458de148ce903f028

Golang bindings for the Telegram Bot API

Added updatesChannel type with Clear() method
lupoDharkael izhe@hotmail.es
Thu, 15 Dec 2016 22:51:58 +0100
commit

66446d51ffc393a94dc510f458de148ce903f028

parent

e2916e08add24b4d520f58626bb4c94d0087eca2

2 files changed, 22 insertions(+), 9 deletions(-)

jump to
M bot.gobot.go

@@ -7,7 +7,6 @@ "bytes"

"encoding/json" "errors" "fmt" - "github.com/technoweenie/multipartstreamer" "io/ioutil" "log" "net/http"

@@ -16,6 +15,8 @@ "os"

"strconv" "strings" "time" + + "github.com/technoweenie/multipartstreamer" ) // BotAPI allows you to interact with the Telegram Bot API.

@@ -448,8 +449,9 @@ return info, err

} // GetUpdatesChan starts and returns a channel for getting updates. -func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (<-chan Update, error) { - updatesChan := make(chan Update, 100) +func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (updatesChannel, error) { + ch := make(chan Update, 100) + var updatesCh updatesChannel = ch go func() { for {

@@ -465,18 +467,19 @@

for _, update := range updates { if update.UpdateID >= config.Offset { config.Offset = update.UpdateID + 1 - updatesChan <- update + ch <- update } } } }() - return updatesChan, nil + return updatesCh, nil } // ListenForWebhook registers a http handler for a webhook. -func (bot *BotAPI) ListenForWebhook(pattern string) <-chan Update { - updatesChan := make(chan Update, 100) +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)

@@ -484,10 +487,10 @@

var update Update json.Unmarshal(bytes, &update) - updatesChan <- update + ch <- update }) - return updatesChan + return updatesCh } // AnswerInlineQuery sends a response to an inline query.
M types.gotypes.go

@@ -37,6 +37,16 @@ ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result"`

CallbackQuery *CallbackQuery `json:"callback_query"` } +//updatesChannel is the channel for getting updates. +type updatesChannel <-chan Update + +//Clear discards all the actual incoming updates +func (ch updatesChannel) Clear() { + for len(ch) != 0 { + <-ch + } +} + // User is a user on Telegram. type User struct { ID int `json:"id"`