all repos — telegram-bot-api @ 2483f043976a025de9c97f53dfcaa4894c090b8b

Golang bindings for the Telegram Bot API

updates.go (view raw)

 1package tgbotapi
 2
 3import (
 4	"log"
 5	"time"
 6)
 7
 8// UpdatesChan returns a chan that is called whenever a new message is gotten.
 9func (bot *BotAPI) UpdatesChan(config UpdateConfig) (chan Update, error) {
10	bot.Updates = make(chan Update, 100)
11
12	go func() {
13		for {
14			updates, err := bot.GetUpdates(config)
15			if err != nil {
16				if bot.Debug {
17					panic(err)
18				} else {
19					log.Println(err)
20					log.Println("Failed to get updates, retrying in 3 seconds...")
21					time.Sleep(time.Second * 3)
22				}
23
24				continue
25			}
26
27			for _, update := range updates {
28				if update.UpdateID >= config.Offset {
29					config.Offset = update.UpdateID + 1
30					bot.Updates <- update
31				}
32			}
33		}
34	}()
35
36	return bot.Updates, nil
37}