handlers skeleton

This commit is contained in:
Pavel 2024-05-07 22:07:53 +03:00
parent 37da2aea5f
commit e11f74fac4
7 changed files with 132 additions and 8 deletions

View File

@ -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. 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. 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. 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 <task>` 4. Start the poll at any time using command `/poll@vegapokerbot <task>`

View File

@ -2,17 +2,20 @@ package app
import ( import (
"fmt" "fmt"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/config"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/logger" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/logger"
"github.com/mymmrac/telego" "github.com/mymmrac/telego"
"go.uber.org/zap" "go.uber.org/zap"
) )
type App struct { type App struct {
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
Telegram *telego.Bot Telegram *telego.Bot
Config *Config Config *config.Config
DB *db.Repositories Repositories *db.Repositories
Handlers map[handler.Type]handler.Handler
} }
func (a *App) Start() error { func (a *App) Start() error {
@ -34,12 +37,29 @@ func (a *App) Start() error {
if err := a.initTelegram(); err != nil { if err := a.initTelegram(); err != nil {
return err return err
} }
a.initHandlers()
a.Logger.Info("Vega Poker Bot is running") a.Logger.Info("Vega Poker Bot is running")
return a.longPoll() 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 { func (a *App) loadConfig() error {
config, err := LoadConfig() config, err := config.LoadConfig()
if err != nil { if err != nil {
return err return err
} }
@ -62,7 +82,7 @@ func (a *App) initDB() error {
if err != nil { if err != nil {
return err return err
} }
a.DB = db.InitRepositories(conn) a.Repositories = db.InitRepositories(conn)
return nil return nil
} }
@ -72,6 +92,24 @@ func (a *App) initTelegram() error {
return err 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 { func (a *App) longPoll() error {
updates, err := a.Telegram.UpdatesViaLongPolling(nil) updates, err := a.Telegram.UpdatesViaLongPolling(nil)
if err != nil { if err != nil {

View File

@ -1,4 +1,4 @@
package app package config
import ( import (
"github.com/cristalhq/aconfig" "github.com/cristalhq/aconfig"

View File

@ -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")
}

View File

@ -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
}

View File

@ -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")
}

23
internal/handler/noop.go Normal file
View File

@ -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
}