2024-12-18 11:08:21 -04:00

60 lines
1.7 KiB
Go

package api
import (
"context"
"log/slog"
"git.maximotejeda.com/maximo/cedulados-bot/internal/application/auth"
"git.maximotejeda.com/maximo/cedulados-bot/internal/application/command"
"git.maximotejeda.com/maximo/cedulados-bot/internal/application/message"
"git.maximotejeda.com/maximo/cedulados-bot/internal/application/query"
"git.maximotejeda.com/maximo/cedulados-bot/internal/ports"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type api struct {
ctx context.Context
log *slog.Logger
bot *tgbotapi.BotAPI
update *tgbotapi.Update
command ports.Tgb
message ports.Tgb
query ports.Tgb
cedulados ports.CeduladosService
user ports.UserService
}
// NewApi
// Create a new instance for api
func NewApi(ctx context.Context, log *slog.Logger, bot *tgbotapi.BotAPI) *api {
log = log.With("location", "api")
return &api{ctx: ctx, log: log, bot: bot}
}
// Run
// Start bot user interaction process
func (a *api) Run(update *tgbotapi.Update, cedSVC ports.CeduladosService, user ports.UserService) {
au := auth.NewAuth(a.ctx, a.log, a.bot, update, update.SentFrom(), user)
if !(au.Authenticate()) && !update.Message.IsCommand(){
a.log.Error("Authentication failed")
return
}
msg := update.Message
if msg != nil { // message is not nil can be a command or a text message
if msg.IsCommand() {
com := command.Newcommand(a.bot, update, cedSVC, user)
com.Handler()
// is a command
} else if msg.Text != "" {
// is a text message
message := message.NewMessage(a.bot, update, cedSVC, user)
message.Handler()
}
} else if update.CallbackQuery != nil {
// is a cal back
qr := query.NewQuery(a.bot, update, cedSVC, user)
qr.Handler()
}
}