This commit is contained in:
2021-12-03 12:19:55 +03:00
commit 9744337c99
7 changed files with 136 additions and 0 deletions

43
bot/bot.go Normal file
View File

@ -0,0 +1,43 @@
package bot
import (
"errors"
"log"
"time"
tb "gopkg.in/tucnak/telebot.v3"
)
type Handler struct {
Endpoint interface{}
Handler tb.HandlerFunc
}
type Bot struct {
Bot *tb.Bot
Token string
}
func (b *Bot) Init() error {
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
func (b *Bot) AddHandlers(handlers []Handler) error {
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")
}

16
bot/handlers.go Normal file
View File

@ -0,0 +1,16 @@
package bot
import (
tb "gopkg.in/tucnak/telebot.v3"
)
var HandlersV1 = []Handler{
{
Endpoint: tb.OnText,
Handler: func(c tb.Context) error {
m := c.Message()
c.Bot().Send(m.Sender, m.Text)
return nil
},
},
}