move out bot controller logic into separate file

This commit is contained in:
Akulij 2025-03-27 20:44:20 +08:00
parent c81e781247
commit b770b2c5dc
2 changed files with 46 additions and 38 deletions

46
cmd/app/botcontroller.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"log"
"gorm.io/gorm"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/akulij/ticketbot/config"
)
type BotController struct {
cfg config.Config
bot *tgbotapi.BotAPI
db *gorm.DB
updates tgbotapi.UpdatesChannel
}
func GetBotController() BotController {
cfg := config.GetConfig()
log.Printf("Token value: '%v'\n", cfg.BotToken)
log.Printf("Admin password: '%v'\n", cfg.AdminPass)
log.Printf("Admin ID: '%v'\n", *cfg.AdminID)
bot, err := tgbotapi.NewBotAPI(cfg.BotToken)
if err != nil {
log.Panic(err)
}
db, err := GetDB()
if err != nil {
log.Panic(err)
}
bot.Debug = true // set true only while development, should be set to false in production
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
return BotController{cfg: cfg, bot: bot, db: db, updates: updates}
}

View File

@ -10,10 +10,6 @@ import (
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/akulij/ticketbot/config"
"gorm.io/gorm"
)
var adminCommands = map[string]func(BotController, tgbotapi.Update){
@ -25,40 +21,6 @@ var adminCommands = map[string]func(BotController, tgbotapi.Update){
// supposed to be lined with bot
}
type BotController struct {
cfg config.Config
bot *tgbotapi.BotAPI
db *gorm.DB
updates tgbotapi.UpdatesChannel
}
func GetBotController() BotController {
cfg := config.GetConfig()
log.Printf("Token value: '%v'\n", cfg.BotToken)
log.Printf("Admin password: '%v'\n", cfg.AdminPass)
log.Printf("Admin ID: '%v'\n", *cfg.AdminID)
bot, err := tgbotapi.NewBotAPI(cfg.BotToken)
if err != nil {
log.Panic(err)
}
db, err := GetDB()
if err != nil {
log.Panic(err)
}
bot.Debug = true // set true only while development, should be set to false in production
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
return BotController{cfg: cfg, bot: bot, db: db, updates: updates}
}
func main() {
var bc = GetBotController()