Add Interval to UpdateConfig, and method for set it

pull/281/head
youaresofunny 6 years ago
parent e9a86f840c
commit ae54c74c29
  1. 31
      bot.go
  2. 14
      configs.go
  3. 8
      helpers.go

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
_ "log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -492,26 +493,26 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
ch := make(chan Update, bot.Buffer) ch := make(chan Update, bot.Buffer)
go func() { go func() {
ticker := time.NewTicker(config.Interval)
for { for {
select { select {
case <-bot.shutdownChannel: case <-bot.shutdownChannel:
return return
default: case <-ticker.C:
} updates, err := bot.GetUpdates(config)
if err != nil {
updates, err := bot.GetUpdates(config) log.Println(err)
if err != nil { log.Println("Failed to get updates, retrying in 3 seconds...")
log.Println(err) time.Sleep(time.Second * 3)
log.Println("Failed to get updates, retrying in 3 seconds...")
time.Sleep(time.Second * 3) continue
}
continue
}
for _, update := range updates { for _, update := range updates {
if update.UpdateID >= config.Offset { if update.UpdateID >= config.Offset {
config.Offset = update.UpdateID + 1 config.Offset = update.UpdateID + 1
ch <- update ch <- update
}
} }
} }
} }

@ -5,6 +5,7 @@ import (
"io" "io"
"net/url" "net/url"
"strconv" "strconv"
"time"
) )
// Telegram constants // Telegram constants
@ -952,9 +953,16 @@ type FileConfig struct {
// UpdateConfig contains information about a GetUpdates request. // UpdateConfig contains information about a GetUpdates request.
type UpdateConfig struct { type UpdateConfig struct {
Offset int Offset int
Limit int Limit int
Timeout 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. // WebhookConfig contains information about a SetWebhook request.

@ -1,6 +1,7 @@
package got package got
import ( import (
_ "log"
"net/url" "net/url"
) )
@ -382,9 +383,10 @@ func NewUserProfilePhotos(userID int) UserProfilePhotosConfig {
// You likely want to set this to the last Update ID plus 1. // You likely want to set this to the last Update ID plus 1.
func NewUpdate(offset int) UpdateConfig { func NewUpdate(offset int) UpdateConfig {
return UpdateConfig{ return UpdateConfig{
Offset: offset, Offset: offset,
Limit: 0, Limit: 0,
Timeout: 0, Timeout: 0,
Interval: UpdateDefaultInterval,
} }
} }