2015-06-26 06:26:24 +02:00
|
|
|
package tgbotapi
|
|
|
|
|
2015-06-29 18:04:48 +02:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
// UpdatesChan starts a channel for getting updates.
|
|
|
|
func (bot *BotAPI) UpdatesChan(config UpdateConfig) error {
|
2015-06-26 06:26:24 +02:00
|
|
|
bot.Updates = make(chan Update, 100)
|
|
|
|
|
|
|
|
go func() {
|
2015-06-26 09:12:09 +02:00
|
|
|
for {
|
|
|
|
updates, err := bot.GetUpdates(config)
|
|
|
|
if err != nil {
|
2015-06-30 05:44:12 +02:00
|
|
|
if bot.Debug {
|
2015-06-29 18:04:48 +02:00
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
log.Println(err)
|
2015-06-30 05:44:12 +02:00
|
|
|
log.Println("Failed to get updates, retrying in 3 seconds...")
|
2015-06-29 18:04:48 +02:00
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
}
|
2015-06-30 05:44:12 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, update := range updates {
|
|
|
|
if update.UpdateID >= config.Offset {
|
|
|
|
config.Offset = update.UpdateID + 1
|
|
|
|
bot.Updates <- update
|
2015-06-26 09:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
return nil
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|