153 lines
4.5 KiB
Go
153 lines
4.5 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
tg "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"
|
|
"github.com/maximotejeda/us_dop_bot/models"
|
|
)
|
|
|
|
// QueryHandler
|
|
// Manage queries to execute user commands
|
|
func QueryHandler(ctx context.Context, dbx *db.DB, inst *edb.DB, log *slog.Logger, query *tg.CallbackQuery) (msg *tg.MessageConfig) {
|
|
tUser := query.From
|
|
user := db.NewUser(dbx, log)
|
|
_, err := user.Get(tUser.ID)
|
|
if err != nil {
|
|
log.Error("callback", "error", err)
|
|
}
|
|
data := query.Data
|
|
|
|
dataList := strings.Split(data, "&")
|
|
|
|
dataMap := map[string]string{}
|
|
for _, val := range dataList {
|
|
subData := strings.Split(val, "=")
|
|
dataMap[subData[0]] = subData[1]
|
|
}
|
|
|
|
switch {
|
|
case dataMap["subs"] != "":
|
|
err := user.Subscribe(dataMap["name"])
|
|
if err != nil {
|
|
log.Error("subs-query", "error", err.Error(), "user", user)
|
|
}
|
|
case dataMap["unsubs"] != "":
|
|
user.Get(tUser.ID)
|
|
err := user.Unsubscribe(dataMap["name"])
|
|
if err != nil {
|
|
log.Error("unsubs-query", "error", err.Error())
|
|
}
|
|
case dataMap["reset"] != "":
|
|
err := user.Reset()
|
|
if err != nil {
|
|
log.Error("[query reset] ", "error", err)
|
|
}
|
|
case dataMap["consultar"] != "":
|
|
name := "&name=" + dataMap["name"]
|
|
queryMap := map[string]string{
|
|
"Actual": "query=true&time=0&unit=now" + name,
|
|
"30 Minutos": "query=true&time=30&unit=minute" + name,
|
|
"1 Hora": "query=true&time=1&unit=hour" + name,
|
|
"6 Horas": "query=true&time=6&unit=hour" + name,
|
|
"12 Horas": "query=true&time=12&unit=hour" + name,
|
|
"1 Dia": "query=true&time=24&unit=hour" + name,
|
|
"1 Semana": "query=true&time=168&unit=hour" + name,
|
|
"2 Semanas": "query=true&time=336&unit=hour" + name,
|
|
"1 Mes": "query=true&time=672&unit=hour" + name,
|
|
}
|
|
keyboard := helpers.CreateKeyboard(queryMap)
|
|
msg = &tg.MessageConfig{}
|
|
msg.ChatID = tUser.ID
|
|
msg.ReplyMarkup = keyboard
|
|
msg.Text = fmt.Sprintf("Intervalos de tiempo disponibles para consulta en %s el cambio del dolar desde hace:", dataMap["name"])
|
|
case dataMap["query"] != "":
|
|
var timeUnit time.Duration
|
|
timeAmntSTR := dataMap["time"]
|
|
timeUnitSTR := dataMap["unit"]
|
|
name := dataMap["name"]
|
|
timeAmnt, err := strconv.Atoi(timeAmntSTR)
|
|
switch name {
|
|
case "apap":
|
|
name = "asociacion popular de ahorros y prestamos"
|
|
case "acap":
|
|
name = "asociacion cibao de ahorros y prestamos"
|
|
case "anap":
|
|
name = "asociacion la nacional de ahorros y prestamos"
|
|
}
|
|
if err != nil {
|
|
log.Error("[query-query] converting amount of time to int", "error", err)
|
|
return msg
|
|
}
|
|
switch timeUnitSTR {
|
|
case "minute":
|
|
timeUnit = time.Minute * time.Duration(timeAmnt)
|
|
instList, err := inst.GetChangeSince(name, timeUnit)
|
|
if err != nil {
|
|
log.Error("[GETLIST] querying the inst database minutes", "error", err)
|
|
return
|
|
}
|
|
queryMap := map[string]string{"clear": "cancel=true"}
|
|
keyboard := helpers.CreateKeyboard(queryMap)
|
|
msg = &tg.MessageConfig{}
|
|
msg.ChatID = tUser.ID
|
|
msg.ReplyMarkup = keyboard
|
|
|
|
msg.Text = instMessage(instList)
|
|
|
|
case "hour":
|
|
timeUnit = time.Hour * time.Duration(timeAmnt)
|
|
instList, err := inst.GetChangeSince(name, timeUnit)
|
|
if err != nil {
|
|
log.Error("[GETLIST] querying the inst database hours", "error", err)
|
|
return
|
|
}
|
|
queryMap := map[string]string{"clear": "cancel=true"}
|
|
keyboard := helpers.CreateKeyboard(queryMap)
|
|
msg = &tg.MessageConfig{}
|
|
msg.ChatID = tUser.ID
|
|
msg.ReplyMarkup = keyboard
|
|
|
|
msg.Text = instMessage(instList)
|
|
case "now":
|
|
instRes, err := inst.GetLastPrice(name)
|
|
if err != nil {
|
|
log.Error("queriing the inst database now", "error", err)
|
|
return
|
|
}
|
|
queryMap := map[string]string{"clear": "cancel=true"}
|
|
keyboard := helpers.CreateKeyboard(queryMap)
|
|
msg = &tg.MessageConfig{}
|
|
msg.ChatID = tUser.ID
|
|
|
|
msg.ReplyMarkup = keyboard
|
|
msg.Text = fmt.Sprintf("%s\nCompra: %.2f\nVenta: %.2f", instRes.Name, instRes.Compra, instRes.Venta)
|
|
|
|
}
|
|
|
|
//log.Info("", "time unit", timeUnit, "timeAmount", timeAmnt)
|
|
|
|
}
|
|
return msg
|
|
}
|
|
|
|
func instMessage(insts []*models.Institucion) string {
|
|
if len(insts) <= 0 {
|
|
return "Sin cambios registrados en este intervalo\nPrueba a ampliar el rango del tiempo deseado."
|
|
}
|
|
name := insts[0].Name
|
|
resultText := fmt.Sprintf("%s\n\n", name)
|
|
for _, i := range insts {
|
|
resultText = resultText + fmt.Sprintf(" %s\n Compra: %.2f Venta: %.2f\n", i.Parsed.Format(time.DateTime), i.Compra, i.Venta)
|
|
}
|
|
return resultText
|
|
}
|