all repos — telegram-bot-api @ 69bb4a9c33b1861e616862688e1d60ffeb7d0c83

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 == 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
35	return bot.Updates, nil
36}