166 lines
4.7 KiB
Go
166 lines
4.7 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"slices"
|
|
"strings"
|
|
|
|
tgbot "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/maximotejeda/us_dop_bot/db"
|
|
edb "github.com/maximotejeda/us_dop_bot/edb"
|
|
"github.com/maximotejeda/us_dop_bot/helpers"
|
|
)
|
|
|
|
// CommandHandler
|
|
// Options for user to create queries on the bot
|
|
func CommandHandler(ctx context.Context, userDB *db.DB, instDB *edb.DB, log *slog.Logger, update tgbot.Update) tgbot.MessageConfig {
|
|
var (
|
|
err error
|
|
msg tgbot.MessageConfig
|
|
usr = db.NewUser(userDB, log)
|
|
)
|
|
command := update.Message.Command()
|
|
chatID := update.Message.Chat.ID
|
|
|
|
msg.ChatID = chatID
|
|
|
|
switch strings.ToLower(command) {
|
|
case "list", "lista":
|
|
msg, err = lista(update, userDB, instDB, log, "bancos")
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
}
|
|
case "listabancos":
|
|
msg, err = lista(update, userDB, instDB, log, "bancos")
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
}
|
|
case "listacajas":
|
|
msg, err = lista(update, userDB, instDB, log, "cajas")
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
}
|
|
|
|
case "listagentes":
|
|
msg, err = lista(update, userDB, instDB, log, "agentes")
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
}
|
|
case "consulta":
|
|
_, err := usr.Get(update.Message.From.ID)
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
}
|
|
btnSTR := map[string]string{}
|
|
for _, inst := range usr.Subs {
|
|
switch inst {
|
|
case "asociacion popular de ahorros y prestamos":
|
|
btnSTR[inst] = "consultar=true&name=apap"
|
|
case "asociacion cibao de ahorros y prestamos":
|
|
btnSTR[inst] = "consultar=true&name=acap"
|
|
case "asociacion la nacional de ahorros y prestamos":
|
|
btnSTR[inst] = "consultar=true&name=anap"
|
|
default:
|
|
btnSTR[inst] = "consultar=true&name=" + inst
|
|
}
|
|
|
|
}
|
|
btnSTR["cancelar ❌"] = "cancelar=true"
|
|
keyboard := helpers.CreateKeyboard(btnSTR)
|
|
msg.ReplyMarkup = keyboard
|
|
msg.Text = "Suscripciones actuales 💰\nPuedes hacer click para Ver cambios en los precios de las suscripcion\n o presionar cancelar"
|
|
|
|
case "status", "info":
|
|
_, err := usr.Get(update.Message.From.ID)
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
}
|
|
btnSTR := map[string]string{}
|
|
for _, inst := range usr.Subs {
|
|
btnSTR[inst] = "unsubs=true&name=" + inst
|
|
}
|
|
btnSTR["cancelar ❌"] = "cancelar=true"
|
|
keyboard := helpers.CreateKeyboard(btnSTR)
|
|
msg.ReplyMarkup = keyboard
|
|
msg.Text = "Suscripciones actuales 💰\n Puedes hacer click en una para eliminar su subscripcion\nPresionar cancelar para omitir."
|
|
|
|
case "reset":
|
|
reset := map[string]string{"Reset": "reset=true", "cancelar ❌": "cancelar=true"}
|
|
keyboard := helpers.CreateKeyboard(reset)
|
|
msg.ReplyMarkup = keyboard
|
|
msg.Text = "Elimina todas las suscripciones del usuario."
|
|
case "help", "start", "ayuda", "h":
|
|
|
|
help := `
|
|
Asistente de cambio US <-> DOP
|
|
🇺🇸 ↔️ 🇩🇴
|
|
Tracker del precio del dolar para RD.
|
|
|
|
Funciona suscribiendo instituciones 💸.
|
|
- Tracker precio del dolar ❇️.
|
|
- Notificacion mensaje automatico 📈.
|
|
- Precio actual ⌚.
|
|
- Historico de precios 📅.
|
|
|
|
Comandos Conocidos por el bot:
|
|
|
|
/help: Muestra este mensaje ❓
|
|
/listabancos: Muestra bancos 🏦
|
|
/listacajas: Muestra asociaciones
|
|
/listagentes: Muestra agentes 📊
|
|
/consulta: Consulta entidad suscrita 🛎️
|
|
/reset: Borra tada suscripcion 🧹
|
|
/status: Estado del usuario 📋
|
|
`
|
|
|
|
msg.Text = help
|
|
default:
|
|
msg.Text = "Commando desconocido intenta con\n/help: to get bot info."
|
|
}
|
|
return msg
|
|
}
|
|
|
|
func lista(update tgbot.Update, user *db.DB, insts *edb.DB, log *slog.Logger, instSTR string) (msg tgbot.MessageConfig, err error) {
|
|
var instList []string
|
|
usr := db.NewUser(user, log)
|
|
msg = tgbot.MessageConfig{}
|
|
msg.ChatID = update.Message.Chat.ID
|
|
_, err = usr.Get(update.Message.From.ID)
|
|
if err != nil {
|
|
log.Error("command-status", "err", err)
|
|
return msg, err
|
|
}
|
|
|
|
// TODO list all institutions
|
|
|
|
switch instSTR {
|
|
case "bancos":
|
|
instList, err = insts.GetBancos()
|
|
case "cajas":
|
|
instList, err = insts.GetCajas()
|
|
case "agentes":
|
|
instList, err = insts.GetAgentes()
|
|
default:
|
|
return msg, fmt.Errorf("tipio de institucion desconocida")
|
|
}
|
|
if err != nil {
|
|
log.Error("[inst-list-query]", "error", err)
|
|
return msg, err
|
|
}
|
|
instMap := map[string]string{}
|
|
for _, i := range instList {
|
|
if slices.Contains[[]string](usr.Subs, i) {
|
|
continue
|
|
}
|
|
instMap[i] = "subs=true&name=" + i
|
|
}
|
|
instMap["cancelar ❌"] = "cancelar=true"
|
|
keyboard := helpers.CreateKeyboard(instMap)
|
|
|
|
msg.Text = "Differentes cajas disponibles para track el precio del cambio\n\n\tasociacion popular\n\n\tasociacion cibao\n\n"
|
|
msg.ReplyMarkup = keyboard
|
|
return msg, nil
|
|
}
|