2024-05-07 21:49:09 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-07 22:07:53 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/config"
|
2024-05-07 21:49:09 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db"
|
2024-05-07 22:07:53 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler"
|
2024-05-07 21:49:09 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/logger"
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
2024-05-07 22:07:53 +03:00
|
|
|
Logger *zap.SugaredLogger
|
|
|
|
Telegram *telego.Bot
|
|
|
|
Config *config.Config
|
|
|
|
Repositories *db.Repositories
|
|
|
|
Handlers map[handler.Type]handler.Handler
|
2024-05-07 21:49:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Start() error {
|
|
|
|
if err := a.loadConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.initLogger(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a.Config.Debug {
|
|
|
|
a.Logger.Debugf("loaded configuration: %+v", a.Config)
|
|
|
|
}
|
|
|
|
if err := a.migrateDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.initTelegram(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-07 22:07:53 +03:00
|
|
|
a.initHandlers()
|
2024-05-07 21:49:09 +03:00
|
|
|
a.Logger.Info("Vega Poker Bot is running")
|
|
|
|
return a.longPoll()
|
|
|
|
}
|
|
|
|
|
2024-05-07 22:07:53 +03:00
|
|
|
func (a *App) Log() *zap.SugaredLogger {
|
|
|
|
return a.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) TG() *telego.Bot {
|
|
|
|
return a.Telegram
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Conf() *config.Config {
|
|
|
|
return a.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) DB() *db.Repositories {
|
|
|
|
return a.Repositories
|
|
|
|
}
|
|
|
|
|
2024-05-07 21:49:09 +03:00
|
|
|
func (a *App) loadConfig() error {
|
2024-05-07 22:07:53 +03:00
|
|
|
config, err := config.LoadConfig()
|
2024-05-07 21:49:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Config = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) initLogger() error {
|
|
|
|
var err error
|
|
|
|
a.Logger, err = logger.NewLogger(a.Config.Debug)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) migrateDB() error {
|
|
|
|
return db.Migrate(a.Config.PostgresDSN)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) initDB() error {
|
|
|
|
conn, err := db.Connect(a.Config.PostgresDSN)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-07 22:07:53 +03:00
|
|
|
a.Repositories = db.InitRepositories(conn)
|
2024-05-07 21:49:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) initTelegram() error {
|
|
|
|
var err error
|
|
|
|
a.Telegram, err = telego.NewBot(a.Config.TelegramToken, logger.WrapForTelego(a.Logger, a.Config.Debug))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-07 22:07:53 +03:00
|
|
|
func (a *App) initHandlers() {
|
|
|
|
a.Handlers = map[handler.Type]handler.Handler{
|
|
|
|
handler.Noop: handler.NewNoopHandler(a.Logger, a.Config.Debug),
|
|
|
|
handler.Message: handler.NewMessageHandler(a),
|
|
|
|
handler.ChatMemberUpdated: handler.NewChatMemberUpdatedHandler(a),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) handler(update telego.Update) handler.Handler {
|
|
|
|
if update.Message != nil {
|
|
|
|
return a.Handlers[handler.Message]
|
|
|
|
}
|
|
|
|
if update.ChatMember != nil {
|
|
|
|
return a.Handlers[handler.ChatMemberUpdated]
|
|
|
|
}
|
|
|
|
return a.Handlers[handler.Noop]
|
|
|
|
}
|
|
|
|
|
2024-05-07 21:49:09 +03:00
|
|
|
func (a *App) longPoll() error {
|
|
|
|
updates, err := a.Telegram.UpdatesViaLongPolling(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer a.Telegram.StopLongPolling()
|
|
|
|
for update := range updates {
|
|
|
|
fmt.Printf("Update: %+v\n", update)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|