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 == true {
17 panic(err)
18 } else {
19 log.Println(err)
20 log.Println("Fail to GetUpdates,Retry in 3 Seconds...")
21 time.Sleep(time.Second * 3)
22 }
23 } else {
24 for _, update := range updates {
25 if update.UpdateID >= config.Offset {
26 config.Offset = update.UpdateID + 1
27 bot.Updates <- update
28 }
29 }
30 }
31 }
32 }()
33
34 return bot.Updates, nil
35}