KickerBot/kicker/kicker.go

46 lines
788 B
Go
Raw Normal View History

2021-12-03 14:30:10 +03:00
package kicker
2021-12-03 12:19:55 +03:00
import (
"errors"
"log"
"time"
tb "gopkg.in/tucnak/telebot.v3"
)
type Handler struct {
Endpoint interface{}
Handler tb.HandlerFunc
}
2021-12-03 14:35:07 +03:00
// Базовая структура для бота
2021-12-03 14:30:10 +03:00
type Kicker struct {
2021-12-03 12:19:55 +03:00
Bot *tb.Bot
Token string
}
2021-12-03 14:35:07 +03:00
// Initialize bot with token
2021-12-03 14:30:10 +03:00
func (b *Kicker) Init() error {
2021-12-03 12:19:55 +03:00
bot, err := tb.NewBot(tb.Settings{
Token: b.Token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Print(err)
return err
}
b.Bot = bot
return nil
}
// Add handler methods to the bot
2021-12-03 14:30:10 +03:00
func (b *Kicker) AddHandlers(handlers []Handler) error {
2021-12-03 12:19:55 +03:00
if len(handlers) != 0 {
for i := range handlers {
b.Bot.Handle(handlers[i].Endpoint, handlers[i].Handler)
}
return nil
}
return errors.New("no handlers are declared")
}