|
|
|
@ -9,8 +9,11 @@ import (
|
|
|
|
|
"os"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"go.uber.org/ratelimit"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewBot does try to build a Bot with token `token`, which
|
|
|
|
@ -19,6 +22,9 @@ func NewBot(pref Settings) (*Bot, error) {
|
|
|
|
|
if pref.Updates == 0 {
|
|
|
|
|
pref.Updates = 100
|
|
|
|
|
}
|
|
|
|
|
if pref.PerSeconds == 0 {
|
|
|
|
|
pref.PerSeconds = -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := pref.Client
|
|
|
|
|
if client == nil {
|
|
|
|
@ -49,8 +55,29 @@ func NewBot(pref Settings) (*Bot, error) {
|
|
|
|
|
verbose: pref.Verbose,
|
|
|
|
|
parseMode: pref.ParseMode,
|
|
|
|
|
client: client,
|
|
|
|
|
|
|
|
|
|
ratelimit: false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wait := make(chan struct{})
|
|
|
|
|
if pref.PerSeconds != -1 {
|
|
|
|
|
bot.ratelimit = true
|
|
|
|
|
go func(b *Bot) {
|
|
|
|
|
rl := ratelimit.New(pref.PerSeconds)
|
|
|
|
|
bot.raws = make(chan *sync.Cond, pref.PerBufferSize)
|
|
|
|
|
wait <- struct{}{}
|
|
|
|
|
for raw := range bot.raws {
|
|
|
|
|
rl.Take()
|
|
|
|
|
raw.Broadcast()
|
|
|
|
|
}
|
|
|
|
|
}(bot)
|
|
|
|
|
} else {
|
|
|
|
|
go func() {
|
|
|
|
|
wait <- struct{}{}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
<-wait
|
|
|
|
|
|
|
|
|
|
if pref.Offline {
|
|
|
|
|
bot.Me = &User{}
|
|
|
|
|
} else {
|
|
|
|
@ -82,6 +109,8 @@ type Bot struct {
|
|
|
|
|
stop chan chan struct{}
|
|
|
|
|
client *http.Client
|
|
|
|
|
stopClient chan struct{}
|
|
|
|
|
raws chan *sync.Cond
|
|
|
|
|
ratelimit bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Settings represents a utility struct for passing certain
|
|
|
|
@ -119,6 +148,15 @@ type Settings struct {
|
|
|
|
|
|
|
|
|
|
// Offline allows to create a bot without network for testing purposes.
|
|
|
|
|
Offline bool
|
|
|
|
|
|
|
|
|
|
// PerSeconds limits the number of requests per second executed
|
|
|
|
|
// by the client for Raw function. Default per in seconds is -1.
|
|
|
|
|
// To enable the queue, set the value greater than zero.
|
|
|
|
|
PerSeconds int
|
|
|
|
|
|
|
|
|
|
// PerBufferSize sets the size of the queue that is waiting for the signal to be sent.
|
|
|
|
|
// By default, the buffer is infinite and has a value equal to zero.
|
|
|
|
|
PerBufferSize int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var defaultOnError = func(err error, c Context) {
|
|
|
|
|