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"
"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
}
}
}
}

@ -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.

@ -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,
}
}