From e11f74fac4b80e0fd1854f653a3a820647cdca9e Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Tue, 7 May 2024 22:07:53 +0300 Subject: [PATCH] handlers skeleton --- README.md | 2 +- internal/app/app.go | 50 ++++++++++++++++--- internal/{app => config}/config.go | 2 +- .../handler/chat_member_updated_handler.go | 18 +++++++ internal/handler/handler.go | 27 ++++++++++ internal/handler/new_message_handler.go | 18 +++++++ internal/handler/noop.go | 23 +++++++++ 7 files changed, 132 insertions(+), 8 deletions(-) rename internal/{app => config}/config.go (98%) create mode 100644 internal/handler/chat_member_updated_handler.go create mode 100644 internal/handler/handler.go create mode 100644 internal/handler/new_message_handler.go create mode 100644 internal/handler/noop.go diff --git a/README.md b/README.md index 7fa42f7..2a2d1b4 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,4 @@ Vega Poker Bot is a Telegram-based Scrum Poker tool that allows for effective es 1. Add the bot to your group chat. 2. Start a conversation with Vega Poker Bot and configure the group by specifying which members can vote in poker polls. 3. Configure settings as desired, including enabling Redmine integration via API key for pulling task project, Sprint, and tracker details from Redmine. -4. Start the poll at any time using inline command `@vegapokerbot ` +4. Start the poll at any time using command `/poll@vegapokerbot ` diff --git a/internal/app/app.go b/internal/app/app.go index 1efd2c9..48eafe3 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -2,17 +2,20 @@ package app import ( "fmt" + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/config" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db" + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/logger" "github.com/mymmrac/telego" "go.uber.org/zap" ) type App struct { - Logger *zap.SugaredLogger - Telegram *telego.Bot - Config *Config - DB *db.Repositories + Logger *zap.SugaredLogger + Telegram *telego.Bot + Config *config.Config + Repositories *db.Repositories + Handlers map[handler.Type]handler.Handler } func (a *App) Start() error { @@ -34,12 +37,29 @@ func (a *App) Start() error { if err := a.initTelegram(); err != nil { return err } + a.initHandlers() a.Logger.Info("Vega Poker Bot is running") return a.longPoll() } +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 +} + func (a *App) loadConfig() error { - config, err := LoadConfig() + config, err := config.LoadConfig() if err != nil { return err } @@ -62,7 +82,7 @@ func (a *App) initDB() error { if err != nil { return err } - a.DB = db.InitRepositories(conn) + a.Repositories = db.InitRepositories(conn) return nil } @@ -72,6 +92,24 @@ func (a *App) initTelegram() error { return err } +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] +} + func (a *App) longPoll() error { updates, err := a.Telegram.UpdatesViaLongPolling(nil) if err != nil { diff --git a/internal/app/config.go b/internal/config/config.go similarity index 98% rename from internal/app/config.go rename to internal/config/config.go index d480857..668cabf 100644 --- a/internal/app/config.go +++ b/internal/config/config.go @@ -1,4 +1,4 @@ -package app +package config import ( "github.com/cristalhq/aconfig" diff --git a/internal/handler/chat_member_updated_handler.go b/internal/handler/chat_member_updated_handler.go new file mode 100644 index 0000000..f77a528 --- /dev/null +++ b/internal/handler/chat_member_updated_handler.go @@ -0,0 +1,18 @@ +package handler + +import ( + "github.com/mymmrac/telego" +) + +type ChatMemberUpdatedHandler struct { + app App +} + +func NewChatMemberUpdatedHandler(app App) *ChatMemberUpdatedHandler { + return &ChatMemberUpdatedHandler{app: app} +} + +func (h *ChatMemberUpdatedHandler) Handle(telego.Update) error { + // TODO implement me + panic("implement me") +} diff --git a/internal/handler/handler.go b/internal/handler/handler.go new file mode 100644 index 0000000..8792a87 --- /dev/null +++ b/internal/handler/handler.go @@ -0,0 +1,27 @@ +package handler + +import ( + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/config" + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db" + "github.com/mymmrac/telego" + "go.uber.org/zap" +) + +type Type uint8 + +const ( + Noop Type = iota + Message + ChatMemberUpdated +) + +type App interface { + Log() *zap.SugaredLogger + TG() *telego.Bot + Conf() *config.Config + DB() *db.Repositories +} + +type Handler interface { + Handle(update telego.Update) error +} diff --git a/internal/handler/new_message_handler.go b/internal/handler/new_message_handler.go new file mode 100644 index 0000000..933b8c2 --- /dev/null +++ b/internal/handler/new_message_handler.go @@ -0,0 +1,18 @@ +package handler + +import ( + "github.com/mymmrac/telego" +) + +type MessageHandler struct { + app App +} + +func NewMessageHandler(app App) Handler { + return &MessageHandler{app: app} +} + +func (h *MessageHandler) Handle(telego.Update) error { + // TODO implement me + panic("implement me") +} diff --git a/internal/handler/noop.go b/internal/handler/noop.go new file mode 100644 index 0000000..511899e --- /dev/null +++ b/internal/handler/noop.go @@ -0,0 +1,23 @@ +package handler + +import ( + "github.com/mymmrac/telego" + "go.uber.org/zap" +) + +type NoopHandler struct { + logger *zap.SugaredLogger + debug bool +} + +func NewNoopHandler(logger *zap.SugaredLogger, debug bool) *NoopHandler { + return &NoopHandler{logger: logger, debug: debug} +} + +func (h *NoopHandler) Handle(update telego.Update) error { + if h.debug { + h.logger.Debugf("unsupported update: %+v", update) + } + + return nil +}