Skip to content

Commit 2a5cf86

Browse files
committed
Updates channel removed from BotAPI
1 parent 6da34a6 commit 2a5cf86

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

bot.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type BotAPI struct {
2222
Token string `json:"token"`
2323
Debug bool `json:"debug"`
2424
Self User `json:"-"`
25-
Updates chan Update `json:"-"`
2625
Client *http.Client `json:"-"`
2726
}
2827

@@ -395,8 +394,8 @@ func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
395394
}
396395

397396
// UpdatesChan starts a channel for getting updates.
398-
func (bot *BotAPI) UpdatesChan(config UpdateConfig) error {
399-
bot.Updates = make(chan Update, 100)
397+
func (bot *BotAPI) UpdatesChan(config UpdateConfig) (<-chan Update, error) {
398+
updatesChan := make(chan Update, 100)
400399

401400
go func() {
402401
for {
@@ -412,29 +411,29 @@ func (bot *BotAPI) UpdatesChan(config UpdateConfig) error {
412411
for _, update := range updates {
413412
if update.UpdateID >= config.Offset {
414413
config.Offset = update.UpdateID + 1
415-
bot.Updates <- update
414+
updatesChan <- update
416415
}
417416
}
418417
}
419418
}()
420419

421-
return nil
420+
return updatesChan, nil
422421
}
423422

424423
// ListenForWebhook registers a http handler for a webhook.
425-
func (bot *BotAPI) ListenForWebhook(pattern string) http.Handler {
426-
bot.Updates = make(chan Update, 100)
424+
func (bot *BotAPI) ListenForWebhook(pattern string) (<-chan Update, http.Handler) {
425+
updatesChan := make(chan Update, 100)
427426

428427
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
429428
bytes, _ := ioutil.ReadAll(r.Body)
430429

431430
var update Update
432431
json.Unmarshal(bytes, &update)
433432

434-
bot.Updates <- update
433+
updatesChan <- update
435434
})
436435

437436
http.HandleFunc(pattern, handler)
438437

439-
return handler
438+
return updatesChan, handler
440439
}

bot_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func TestGetUserProfilePhotos(t *testing.T) {
352352
func TestListenForWebhook(t *testing.T) {
353353
bot, _ := getBot(t)
354354

355-
handler := bot.ListenForWebhook("/")
355+
_, handler := bot.ListenForWebhook("/")
356356

357357
req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
358358
w := httptest.NewRecorder()
@@ -396,7 +396,7 @@ func TestUpdatesChan(t *testing.T) {
396396

397397
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
398398
ucfg.Timeout = 60
399-
err := bot.UpdatesChan(ucfg)
399+
_, err := bot.UpdatesChan(ucfg)
400400

401401
if err != nil {
402402
t.Fail()
@@ -416,9 +416,9 @@ func ExampleNewBotAPI() {
416416
u := tgbotapi.NewUpdate(0)
417417
u.Timeout = 60
418418

419-
err = bot.UpdatesChan(u)
419+
updates, err := bot.UpdatesChan(u)
420420

421-
for update := range bot.Updates {
421+
for update := range updates {
422422
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
423423

424424
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
@@ -443,10 +443,10 @@ func ExampleNewWebhook() {
443443
log.Fatal(err)
444444
}
445445

446-
bot.ListenForWebhook("/" + bot.Token)
446+
updates, _ := bot.ListenForWebhook("/" + bot.Token)
447447
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
448448

449-
for update := range bot.Updates {
449+
for update := range updates {
450450
log.Printf("%+v\n", update)
451451
}
452452
}

types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"time"
7+
"strings"
78
)
89

910
// APIResponse is a response from the Telegram API with the result stored raw.
@@ -112,11 +113,14 @@ func (m *Message) IsGroup() bool {
112113
return m.From.ID != m.Chat.ID
113114
}
114115

115-
// IsGroup returns if the message was sent to a group.
116116
func (m *Message) IsCommand() bool {
117117
return m.Text != "" && m.Text[0] == '/'
118118
}
119119

120+
func (m *Message) Command() string {
121+
return strings.Split(m.Text, " ")[0]
122+
}
123+
120124
// PhotoSize contains information about photos, including ID and Width and Height.
121125
type PhotoSize struct {
122126
FileID string `json:"file_id"`

0 commit comments

Comments
 (0)