From e9ab6e6f0527f3eb10b54b134648f6722e279be9 Mon Sep 17 00:00:00 2001 From: NemoD503 <1historicus@gmail.com> Date: Mon, 21 Oct 2019 01:03:49 +0300 Subject: [PATCH] offset autorefresh --- bot.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bot.go b/bot.go index a996790..5872247 100644 --- a/bot.go +++ b/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