ADD message broadcast on changes
This commit is contained in:
parent
93da47c688
commit
eb3055e7aa
62
broadcast/broadcast.go
Normal file
62
broadcast/broadcast.go
Normal file
@ -0,0 +1,62 @@
|
||||
package broadcast
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
tb "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/maximotejeda/us_dop_bot/db"
|
||||
message "github.com/maximotejeda/us_dop_bot/edb"
|
||||
"github.com/maximotejeda/us_dop_bot/helpers"
|
||||
)
|
||||
|
||||
func SendList(ctx context.Context, userDB *db.DB, log *slog.Logger, data []byte) []tb.MessageConfig {
|
||||
user := db.NewUser(userDB, log)
|
||||
// convert data to map
|
||||
m := message.Message{}
|
||||
listMsg := []tb.MessageConfig{}
|
||||
err := json.Unmarshal(data, &m)
|
||||
if err != nil {
|
||||
log.Error("unmarshaling data", "error", err)
|
||||
}
|
||||
if m.Message == "change registered" {
|
||||
|
||||
userList, err := user.GetAll(m.Data.After.Name)
|
||||
if err != nil {
|
||||
log.Error("querying DB data", "error", err)
|
||||
return nil
|
||||
}
|
||||
cancelBTN := map[string]string{}
|
||||
cancelBTN["Eliminar ❌"] = "cancelar=true"
|
||||
keyboard := helpers.CreateKeyboard(cancelBTN)
|
||||
log.Info("printing change", "user list", userList, "name", m.Data.After.Name)
|
||||
compraCHG := comparer(m.Data.Before.Compra, m.Data.After.Compra)
|
||||
ventaCHG := comparer(m.Data.Before.Venta, m.Data.After.Venta)
|
||||
text := fmt.Sprintf("Cambio Registrado:\n\nInstitucion: %s\n\t Compra: %.2f %s %.2f\n\t Venta: %.2f %s %.2f\n\n\t %s", m.Data.After.Name, m.Data.Before.Compra, compraCHG, m.Data.After.Compra, m.Data.Before.Venta, ventaCHG, m.Data.After.Venta, m.Data.After.Parsed.Format(time.DateTime))
|
||||
|
||||
for _, user := range userList {
|
||||
if user.TguID != 0 {
|
||||
msg := tb.NewMessage(int64(user.TguID), text)
|
||||
msg.ReplyMarkup = keyboard
|
||||
listMsg = append(listMsg, msg)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
return listMsg
|
||||
}
|
||||
|
||||
func comparer(before, after float64) string {
|
||||
if before > after {
|
||||
return "⬇️"
|
||||
} else if before < after {
|
||||
return "⬆️"
|
||||
} else {
|
||||
return "🟰"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user