offset autorefresh

pull/277/head
NemoD503 6 years ago
parent b33efeebc7
commit e9ab6e6f05
  1. 13
      bot.go

@ -19,6 +19,8 @@ import (
"github.com/technoweenie/multipartstreamer"
)
const offsetRefreshDuration = 6 * 24 * time.Hour
// BotAPI allows you to interact with the Telegram Bot API.
type BotAPI struct {
Token string `json:"token"`
@ -492,6 +494,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
ch := make(chan Update, bot.Buffer)
go func() {
lastUpdateTime := time.Now()
for {
select {
case <-bot.shutdownChannel:
@ -508,6 +511,16 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
continue
}
if len(updates) > 0 {
lastUpdateTime = time.Now()
}
if time.Now().Sub(lastUpdateTime) > offsetRefreshDuration {
log.Printf("Offset was rejected from %d to 0", config.Offset)
lastUpdateTime = time.Now()
config.Offset = 0
}
for _, update := range updates {
if update.UpdateID >= config.Offset {
config.Offset = update.UpdateID + 1