goboter/telegram/update.go
2020-10-28 15:19:51 +01:00

63 lines
1.4 KiB
Go

package telegram
import (
"context"
"github.com/sirupsen/logrus"
"time"
)
// Serve the Update channel using short polls every <intervall> milliseconds
func (b *Bot) ShortPoll(ctx context.Context, interval int) {
cTicker := time.NewTicker(time.Millisecond * time.Duration(interval)).C
for {
select {
case _, ok := <-ctx.Done():
if ok {
// nope! send on the quit channel is eeeeevil!
}
return
case <-cTicker:
// do an update
updates, err := b.GetUpdates(ctx, b.updateid, 100, 0)
if err != nil {
logrus.WithError(err).Error("cannot get updates")
return // maybe just try to go on?
} else {
for _, u := range updates {
update := u
if update.UpdateId >= b.updateid {
b.updateid = update.UpdateId + 1
}
b.Updates <- update
}
}
}
}
}
// Serve the Update channel using long polling, with a timeout given in seconds
func (b *Bot) LongPoll(ctx context.Context, timeout int64) {
for {
select {
case <-ctx.Done():
return
default:
updates, err := b.GetUpdates(ctx, b.updateid, 100, timeout)
if err != nil {
logrus.WithError(err).Error("cannot get updates")
return // maybe just try to go on?
} else {
for _, u := range updates {
update := u
if update.UpdateId >= b.updateid {
b.updateid = update.UpdateId + 1
}
b.Updates <- update
}
}
}
}
}
// webhook