Add Interval to UpdateConfig, and method for set it

pull/281/head
youaresofunny 6 years ago
parent e9a86f840c
commit ae54c74c29
  1. 7
      bot.go
  2. 8
      configs.go
  3. 2
      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,13 +493,12 @@ 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) updates, err := bot.GetUpdates(config)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -515,6 +515,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
} }
} }
} }
}
}() }()
return ch, nil return ch, nil

@ -5,6 +5,7 @@ import (
"io" "io"
"net/url" "net/url"
"strconv" "strconv"
"time"
) )
// Telegram constants // Telegram constants
@ -955,6 +956,13 @@ 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"
) )
@ -385,6 +386,7 @@ func NewUpdate(offset int) UpdateConfig {
Offset: offset, Offset: offset,
Limit: 0, Limit: 0,
Timeout: 0, Timeout: 0,
Interval: UpdateDefaultInterval,
} }
} }