diff --git a/bot.go b/bot.go index b88213d..18db8e7 100644 --- a/bot.go +++ b/bot.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "io/ioutil" + _ "log" "net/http" "net/url" "os" @@ -492,26 +493,26 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { ch := make(chan Update, bot.Buffer) go func() { + ticker := time.NewTicker(config.Interval) for { select { case <-bot.shutdownChannel: return - default: - } - - updates, err := bot.GetUpdates(config) - if err != nil { - log.Println(err) - log.Println("Failed to get updates, retrying in 3 seconds...") - time.Sleep(time.Second * 3) - - continue - } + case <-ticker.C: + updates, err := bot.GetUpdates(config) + if err != nil { + log.Println(err) + log.Println("Failed to get updates, retrying in 3 seconds...") + time.Sleep(time.Second * 3) + + continue + } - for _, update := range updates { - if update.UpdateID >= config.Offset { - config.Offset = update.UpdateID + 1 - ch <- update + for _, update := range updates { + if update.UpdateID >= config.Offset { + config.Offset = update.UpdateID + 1 + ch <- update + } } } } diff --git a/configs.go b/configs.go index bdf00b6..1b13931 100644 --- a/configs.go +++ b/configs.go @@ -5,6 +5,7 @@ import ( "io" "net/url" "strconv" + "time" ) // Telegram constants @@ -952,9 +953,16 @@ type FileConfig struct { // UpdateConfig contains information about a GetUpdates request. type UpdateConfig struct { - Offset int - Limit int - Timeout int + Offset int + Limit int + Timeout int + Interval time.Duration +} + +const UpdateDefaultInterval = time.Second + +func (uc *UpdateConfig) SetInterval(t time.Duration) { + uc.Interval = t } // WebhookConfig contains information about a SetWebhook request. diff --git a/helpers.go b/helpers.go index 8dd227a..0dc6306 100644 --- a/helpers.go +++ b/helpers.go @@ -1,6 +1,7 @@ package got import ( + _ "log" "net/url" ) @@ -382,9 +383,10 @@ func NewUserProfilePhotos(userID int) UserProfilePhotosConfig { // You likely want to set this to the last Update ID plus 1. func NewUpdate(offset int) UpdateConfig { return UpdateConfig{ - Offset: offset, - Limit: 0, - Timeout: 0, + Offset: offset, + Limit: 0, + Timeout: 0, + Interval: UpdateDefaultInterval, } }