Added updatesChannel type with Clear() method
parent
e2916e08ad
commit
66446d51ff
21
bot.go
21
bot.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/technoweenie/multipartstreamer"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -16,6 +15,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/technoweenie/multipartstreamer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BotAPI allows you to interact with the Telegram Bot API.
|
// BotAPI allows you to interact with the Telegram Bot API.
|
||||||
|
@ -448,8 +449,9 @@ 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) (<-chan Update, error) {
|
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (updatesChannel, error) {
|
||||||
updatesChan := make(chan Update, 100)
|
ch := make(chan Update, 100)
|
||||||
|
var updatesCh updatesChannel = ch
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -465,18 +467,19 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (<-chan Update, error) {
|
||||||
for _, update := range updates {
|
for _, update := range updates {
|
||||||
if update.UpdateID >= config.Offset {
|
if update.UpdateID >= config.Offset {
|
||||||
config.Offset = update.UpdateID + 1
|
config.Offset = update.UpdateID + 1
|
||||||
updatesChan <- update
|
ch <- update
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return updatesChan, nil
|
return updatesCh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenForWebhook registers a http handler for a webhook.
|
// ListenForWebhook registers a http handler for a webhook.
|
||||||
func (bot *BotAPI) ListenForWebhook(pattern string) <-chan Update {
|
func (bot *BotAPI) ListenForWebhook(pattern string) updatesChannel {
|
||||||
updatesChan := 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)
|
||||||
|
@ -484,10 +487,10 @@ func (bot *BotAPI) ListenForWebhook(pattern string) <-chan Update {
|
||||||
var update Update
|
var update Update
|
||||||
json.Unmarshal(bytes, &update)
|
json.Unmarshal(bytes, &update)
|
||||||
|
|
||||||
updatesChan <- update
|
ch <- update
|
||||||
})
|
})
|
||||||
|
|
||||||
return updatesChan
|
return updatesCh
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnswerInlineQuery sends a response to an inline query.
|
// AnswerInlineQuery sends a response to an inline query.
|
||||||
|
|
10
types.go
10
types.go
|
@ -37,6 +37,16 @@ type Update struct {
|
||||||
CallbackQuery *CallbackQuery `json:"callback_query"`
|
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.
|
// User is a user on Telegram.
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
|
|
Loading…
Reference in New Issue