mirror of https://github.com/tucnak/telebot
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
686 B
Go
45 lines
686 B
Go
9 years ago
|
package telebot
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Bot struct {
|
||
|
Token string
|
||
|
|
||
|
// Bot as `User` on API level.
|
||
|
Identity User
|
||
|
|
||
|
listeners []Listener
|
||
|
}
|
||
|
|
||
|
type Listener func(*Bot, Message)
|
||
|
|
||
|
func (b *Bot) Listen(interval time.Duration) {
|
||
|
updates := make(chan Update, 1000)
|
||
|
var latest_update int
|
||
|
|
||
|
pulse := time.NewTicker(interval)
|
||
|
go func() {
|
||
|
for range pulse.C {
|
||
|
go api_getUpdates(b.Token,
|
||
|
latest_update+1,
|
||
|
updates)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
for update := range updates {
|
||
|
if update.Id > latest_update {
|
||
|
latest_update = update.Id
|
||
|
}
|
||
|
|
||
|
for _, ear := range b.listeners {
|
||
|
go ear(b, update.Payload)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (b *Bot) AddListener(ear Listener) {
|
||
|
b.listeners = append(b.listeners, ear)
|
||
|
}
|