forked from carrydela/mygoTgChanBot
82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package telegram
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"tgchanbot/internal/config"
|
|
"tgchanbot/internal/storage"
|
|
"tgchanbot/internal/toc"
|
|
|
|
tele "gopkg.in/telebot.v3"
|
|
)
|
|
|
|
type Bot struct {
|
|
bot *tele.Bot
|
|
cfg *config.Config
|
|
storage *storage.Storage
|
|
toc *toc.Manager
|
|
states *StateManager
|
|
}
|
|
|
|
func New(cfg *config.Config, store *storage.Storage) (*Bot, error) {
|
|
pref := tele.Settings{
|
|
Token: cfg.Bot.Token,
|
|
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
|
}
|
|
|
|
b, err := tele.NewBot(pref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bot := &Bot{
|
|
bot: b,
|
|
cfg: cfg,
|
|
storage: store,
|
|
states: NewStateManager(),
|
|
}
|
|
|
|
bot.toc = toc.NewManager(store, b, cfg.Channel.ID, time.Duration(cfg.TOC.DebounceSeconds)*time.Second)
|
|
|
|
bot.setupRoutes()
|
|
|
|
return bot, nil
|
|
}
|
|
|
|
func (b *Bot) setupRoutes() {
|
|
adminOnly := b.bot.Group()
|
|
adminOnly.Use(b.AdminMiddleware())
|
|
|
|
// Category management
|
|
adminOnly.Handle("/cat_add", b.handleCatAdd)
|
|
adminOnly.Handle("/cat_del", b.handleCatDel)
|
|
adminOnly.Handle("/cat_list", b.handleCatList)
|
|
|
|
// Post flow
|
|
adminOnly.Handle("/post", b.handlePost)
|
|
adminOnly.Handle(tele.OnText, b.handleTextOrForwarded)
|
|
|
|
// Entry management
|
|
adminOnly.Handle("/del", b.handleEntryDel)
|
|
adminOnly.Handle("/edit", b.handleEntryEdit)
|
|
adminOnly.Handle("/move", b.handleEntryMove)
|
|
adminOnly.Handle("/list", b.handleEntryList)
|
|
|
|
// TOC
|
|
adminOnly.Handle("/refresh", b.handleRefresh)
|
|
|
|
// Callbacks
|
|
b.bot.Handle(tele.OnCallback, b.handleCallback)
|
|
}
|
|
|
|
func (b *Bot) Start() {
|
|
log.Println("Bot started...")
|
|
b.bot.Start()
|
|
}
|
|
|
|
func (b *Bot) Stop() {
|
|
b.bot.Stop()
|
|
b.toc.Stop()
|
|
}
|