2017-11-20 23:41:39 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-12-11 22:27:09 +00:00
|
|
|
var (
|
|
|
|
ErrCouldNotUpdate = errors.New("getUpdates() failed")
|
|
|
|
)
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
// Poller is a provider of Updates.
|
|
|
|
//
|
|
|
|
// All pollers must implement Poll(), which accepts bot
|
|
|
|
// pointer and subscription channel and start polling
|
2017-11-21 01:50:44 +00:00
|
|
|
// synchronously straight away.
|
2017-11-20 23:41:39 +00:00
|
|
|
type Poller interface {
|
|
|
|
// Poll is supposed to take the bot object
|
|
|
|
// subscription channel and start polling
|
|
|
|
// for Updates immediately.
|
2017-11-27 12:52:16 +00:00
|
|
|
//
|
|
|
|
// Poller must listen for stop constantly and close
|
|
|
|
// it as soon as it's done polling.
|
2017-11-26 03:40:49 +00:00
|
|
|
Poll(b *Bot, updates chan Update, stop chan struct{})
|
2017-11-20 23:41:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:56:09 +00:00
|
|
|
// MiddlewarePoller is a special kind of poller that acts
|
|
|
|
// like a filter for updates. It could be used for spam
|
|
|
|
// handling, banning or whatever.
|
|
|
|
//
|
|
|
|
// For heavy middleware, use increased capacity.
|
|
|
|
//
|
|
|
|
type MiddlewarePoller struct {
|
|
|
|
Capacity int // Default: 1
|
2017-12-10 14:19:00 +00:00
|
|
|
Poller Poller
|
|
|
|
Filter func(*Update) bool
|
2017-11-21 15:56:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 15:58:41 +00:00
|
|
|
// NewMiddlewarePoller wait for it... constructs a new middleware poller.
|
|
|
|
func NewMiddlewarePoller(original Poller, filter func(*Update) bool) *MiddlewarePoller {
|
2017-11-21 15:56:09 +00:00
|
|
|
return &MiddlewarePoller{
|
2017-11-27 15:58:41 +00:00
|
|
|
Poller: original,
|
2017-12-10 14:19:00 +00:00
|
|
|
Filter: filter,
|
2017-11-21 15:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll sieves updates through middleware filter.
|
2017-11-26 03:40:49 +00:00
|
|
|
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
2017-11-21 15:56:09 +00:00
|
|
|
cap := 1
|
|
|
|
if p.Capacity > 1 {
|
|
|
|
cap = p.Capacity
|
|
|
|
}
|
|
|
|
|
|
|
|
middle := make(chan Update, cap)
|
2017-11-27 12:52:16 +00:00
|
|
|
stopPoller := make(chan struct{})
|
2017-11-21 15:56:09 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
go p.Poller.Poll(b, middle, stopPoller)
|
2017-11-21 15:56:09 +00:00
|
|
|
|
2017-11-26 03:40:49 +00:00
|
|
|
for {
|
|
|
|
select {
|
2017-11-27 12:52:16 +00:00
|
|
|
// call to stop
|
2017-11-26 03:40:49 +00:00
|
|
|
case <-stop:
|
2017-11-27 12:52:16 +00:00
|
|
|
stopPoller <- struct{}{}
|
|
|
|
|
|
|
|
// poller is done
|
|
|
|
case <-stopPoller:
|
|
|
|
close(stop)
|
2017-11-26 03:40:49 +00:00
|
|
|
return
|
2017-11-27 12:52:16 +00:00
|
|
|
|
2017-11-26 03:40:49 +00:00
|
|
|
case upd := <-middle:
|
2017-12-10 14:19:00 +00:00
|
|
|
if p.Filter(&upd) {
|
2017-11-26 03:40:49 +00:00
|
|
|
dest <- upd
|
|
|
|
}
|
2017-11-21 15:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
// LongPoller is a classic LongPoller with timeout.
|
|
|
|
type LongPoller struct {
|
|
|
|
Timeout time.Duration
|
2017-11-27 12:52:16 +00:00
|
|
|
|
|
|
|
LastUpdateID int
|
2017-11-20 23:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Poll does long polling.
|
2017-11-26 03:40:49 +00:00
|
|
|
func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
2017-11-27 12:52:16 +00:00
|
|
|
go func(stop chan struct{}) {
|
|
|
|
<-stop
|
|
|
|
close(stop)
|
|
|
|
}(stop)
|
2017-11-20 23:41:39 +00:00
|
|
|
|
|
|
|
for {
|
2017-11-27 12:52:16 +00:00
|
|
|
updates, err := b.getUpdates(p.LastUpdateID+1, p.Timeout)
|
2017-11-20 23:41:39 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if err != nil {
|
2017-12-11 22:27:09 +00:00
|
|
|
b.debug(ErrCouldNotUpdate)
|
2017-11-27 12:52:16 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-11-20 23:41:39 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
for _, update := range updates {
|
|
|
|
p.LastUpdateID = update.ID
|
|
|
|
dest <- update
|
2017-11-20 23:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|