forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoller.go
43 lines (35 loc) · 861 Bytes
/
poller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package telebot
import (
"time"
"github.com/pkg/errors"
)
// Poller is a provider of Updates.
//
// All pollers must implement Poll(), which accepts bot
// pointer and subscription channel and start polling
// asynchronously straight away.
type Poller interface {
// Poll is supposed to take the bot object
// subscription channel and start polling
// for Updates immediately.
Poll(b *Bot, dest chan Update)
}
// LongPoller is a classic LongPoller with timeout.
type LongPoller struct {
Timeout time.Duration
}
// Poll does long polling.
func (p *LongPoller) Poll(b *Bot, dest chan Update) {
var latestUpd int
for {
updates, err := b.getUpdates(latestUpd+1, p.Timeout)
if err != nil {
b.debug(errors.Wrap(err, "getUpdates() failed"))
continue
}
for _, update := range updates {
latestUpd = update.ID
dest <- update
}
}
}