Updates channel removed from BotAPI
parent
6da34a6ba5
commit
2a5cf8652d
17
bot.go
17
bot.go
|
@ -22,7 +22,6 @@ type BotAPI struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Debug bool `json:"debug"`
|
Debug bool `json:"debug"`
|
||||||
Self User `json:"-"`
|
Self User `json:"-"`
|
||||||
Updates chan Update `json:"-"`
|
|
||||||
Client *http.Client `json:"-"`
|
Client *http.Client `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -395,8 +394,8 @@ func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdatesChan starts a channel for getting updates.
|
// UpdatesChan starts a channel for getting updates.
|
||||||
func (bot *BotAPI) UpdatesChan(config UpdateConfig) error {
|
func (bot *BotAPI) UpdatesChan(config UpdateConfig) (<-chan Update, error) {
|
||||||
bot.Updates = make(chan Update, 100)
|
updatesChan := make(chan Update, 100)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -412,18 +411,18 @@ func (bot *BotAPI) UpdatesChan(config UpdateConfig) 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
|
||||||
bot.Updates <- update
|
updatesChan <- update
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return updatesChan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenForWebhook registers a http handler for a webhook.
|
// ListenForWebhook registers a http handler for a webhook.
|
||||||
func (bot *BotAPI) ListenForWebhook(pattern string) http.Handler {
|
func (bot *BotAPI) ListenForWebhook(pattern string) (<-chan Update, http.Handler) {
|
||||||
bot.Updates = make(chan Update, 100)
|
updatesChan := make(chan Update, 100)
|
||||||
|
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
bytes, _ := ioutil.ReadAll(r.Body)
|
bytes, _ := ioutil.ReadAll(r.Body)
|
||||||
|
@ -431,10 +430,10 @@ func (bot *BotAPI) ListenForWebhook(pattern string) http.Handler {
|
||||||
var update Update
|
var update Update
|
||||||
json.Unmarshal(bytes, &update)
|
json.Unmarshal(bytes, &update)
|
||||||
|
|
||||||
bot.Updates <- update
|
updatesChan <- update
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc(pattern, handler)
|
http.HandleFunc(pattern, handler)
|
||||||
|
|
||||||
return handler
|
return updatesChan, handler
|
||||||
}
|
}
|
||||||
|
|
12
bot_test.go
12
bot_test.go
|
@ -352,7 +352,7 @@ func TestGetUserProfilePhotos(t *testing.T) {
|
||||||
func TestListenForWebhook(t *testing.T) {
|
func TestListenForWebhook(t *testing.T) {
|
||||||
bot, _ := getBot(t)
|
bot, _ := getBot(t)
|
||||||
|
|
||||||
handler := bot.ListenForWebhook("/")
|
_, handler := bot.ListenForWebhook("/")
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
|
req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
@ -396,7 +396,7 @@ func TestUpdatesChan(t *testing.T) {
|
||||||
|
|
||||||
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
|
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
|
||||||
ucfg.Timeout = 60
|
ucfg.Timeout = 60
|
||||||
err := bot.UpdatesChan(ucfg)
|
_, err := bot.UpdatesChan(ucfg)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
@ -416,9 +416,9 @@ func ExampleNewBotAPI() {
|
||||||
u := tgbotapi.NewUpdate(0)
|
u := tgbotapi.NewUpdate(0)
|
||||||
u.Timeout = 60
|
u.Timeout = 60
|
||||||
|
|
||||||
err = bot.UpdatesChan(u)
|
updates, err := bot.UpdatesChan(u)
|
||||||
|
|
||||||
for update := range bot.Updates {
|
for update := range updates {
|
||||||
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
||||||
|
|
||||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
|
||||||
|
@ -443,10 +443,10 @@ func ExampleNewWebhook() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.ListenForWebhook("/" + bot.Token)
|
updates, _ := bot.ListenForWebhook("/" + bot.Token)
|
||||||
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
|
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
|
||||||
|
|
||||||
for update := range bot.Updates {
|
for update := range updates {
|
||||||
log.Printf("%+v\n", update)
|
log.Printf("%+v\n", update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
types.go
6
types.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// APIResponse is a response from the Telegram API with the result stored raw.
|
// APIResponse is a response from the Telegram API with the result stored raw.
|
||||||
|
@ -112,11 +113,14 @@ func (m *Message) IsGroup() bool {
|
||||||
return m.From.ID != m.Chat.ID
|
return m.From.ID != m.Chat.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsGroup returns if the message was sent to a group.
|
|
||||||
func (m *Message) IsCommand() bool {
|
func (m *Message) IsCommand() bool {
|
||||||
return m.Text != "" && m.Text[0] == '/'
|
return m.Text != "" && m.Text[0] == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Message) Command() string {
|
||||||
|
return strings.Split(m.Text, " ")[0]
|
||||||
|
}
|
||||||
|
|
||||||
// PhotoSize contains information about photos, including ID and Width and Height.
|
// PhotoSize contains information about photos, including ID and Width and Height.
|
||||||
type PhotoSize struct {
|
type PhotoSize struct {
|
||||||
FileID string `json:"file_id"`
|
FileID string `json:"file_id"`
|
||||||
|
|
Loading…
Reference in New Issue