2017-11-20 23:41:39 +00:00
|
|
|
package telebot
|
|
|
|
|
2022-10-05 21:45:27 +00:00
|
|
|
import "time"
|
2017-12-11 22:27:09 +00:00
|
|
|
|
2024-03-01 19:25:37 +00:00
|
|
|
var AllowedUpdates = []string{
|
|
|
|
"message",
|
|
|
|
"edited_message",
|
|
|
|
"channel_post",
|
|
|
|
"edited_channel_post",
|
|
|
|
"message_reaction",
|
|
|
|
"message_reaction_count",
|
|
|
|
"inline_query",
|
|
|
|
"chosen_inline_result",
|
|
|
|
"callback_query",
|
|
|
|
"shipping_query",
|
|
|
|
"pre_checkout_query",
|
|
|
|
"poll",
|
|
|
|
"poll_answer",
|
|
|
|
"my_chat_member",
|
|
|
|
"chat_member",
|
|
|
|
"chat_join_request",
|
|
|
|
"chat_boost",
|
|
|
|
"removed_chat_boost",
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-05 21:45:27 +00:00
|
|
|
// LongPoller is a classic LongPoller with timeout.
|
|
|
|
type LongPoller struct {
|
|
|
|
Limit int
|
|
|
|
Timeout time.Duration
|
|
|
|
LastUpdateID int
|
|
|
|
|
|
|
|
// AllowedUpdates contains the update types
|
|
|
|
// you want your bot to receive.
|
|
|
|
//
|
|
|
|
// Possible values:
|
|
|
|
// message
|
|
|
|
// edited_message
|
|
|
|
// channel_post
|
|
|
|
// edited_channel_post
|
|
|
|
// inline_query
|
|
|
|
// chosen_inline_result
|
|
|
|
// callback_query
|
|
|
|
// shipping_query
|
|
|
|
// pre_checkout_query
|
|
|
|
// poll
|
|
|
|
// poll_answer
|
|
|
|
//
|
|
|
|
AllowedUpdates []string `yaml:"allowed_updates"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll does long polling.
|
|
|
|
func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
updates, err := b.getUpdates(p.LastUpdateID+1, p.Limit, p.Timeout, p.AllowedUpdates)
|
|
|
|
if err != nil {
|
|
|
|
b.debug(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, update := range updates {
|
|
|
|
p.LastUpdateID = update.ID
|
|
|
|
dest <- update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:16:51 +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
|
|
|
|
Poller Poller
|
|
|
|
Filter func(*Update) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMiddlewarePoller wait for it... constructs a new middleware poller.
|
|
|
|
func NewMiddlewarePoller(original Poller, filter func(*Update) bool) *MiddlewarePoller {
|
|
|
|
return &MiddlewarePoller{
|
|
|
|
Poller: original,
|
|
|
|
Filter: filter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll sieves updates through middleware filter.
|
|
|
|
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
|
|
|
if p.Capacity < 1 {
|
|
|
|
p.Capacity = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
middle := make(chan Update, p.Capacity)
|
|
|
|
stopPoller := make(chan struct{})
|
2022-02-04 17:53:28 +00:00
|
|
|
stopConfirm := make(chan struct{})
|
2022-01-22 22:16:51 +00:00
|
|
|
|
2022-02-04 17:53:28 +00:00
|
|
|
go func() {
|
|
|
|
p.Poller.Poll(b, middle, stopPoller)
|
|
|
|
close(stopConfirm)
|
|
|
|
}()
|
2022-01-22 22:16:51 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
close(stopPoller)
|
2022-02-04 17:53:28 +00:00
|
|
|
<-stopConfirm
|
2022-01-22 22:16:51 +00:00
|
|
|
return
|
|
|
|
case upd := <-middle:
|
|
|
|
if p.Filter(&upd) {
|
|
|
|
dest <- upd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|