feat: pantalla para administrar chats autorizados del agente de Telegram

Antes la whitelist de telegram_agent_auth solo se podía tocar con curl/Postman
contra /api/v2/agent/auth. Se agrega /app/agente/chats-autorizados con un
CRUD simple, más un botón "buscar mensajes recientes" (UpdatesRecientesDelBot,
vía getUpdates) para descubrir el chat_id de alguien que le acaba de escribir
al bot sin tener que pedírselo por otro medio. Entrada nueva en el menú lateral
del módulo Automatización IA.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Lizandro GD
2026-08-03 02:37:40 +00:00
co-authored by Claude Sonnet 5
parent 7b0e4b9b5c
commit 5607836261
6 changed files with 311 additions and 0 deletions
@@ -170,6 +170,51 @@ func sendAgentReply(botToken string, chatID int64, text string) error {
// ─── CRUD de chats autorizados ────────────────────────────────────────────────
// AgentAuthView renderiza la pantalla de administración de chats autorizados.
func AgentAuthView(c *fiber.Ctx) error {
return c.Render("automatizacion/chats_autorizados", fiber.Map{
"user": c.Locals("user"),
"modules": c.Locals("userModules"),
}, "layouts/main")
}
// AgentAuthRecientes consulta los últimos mensajes recibidos por el bot del
// agente (getUpdates) y devuelve los remitentes distintos que todavía no están
// autorizados, para que el admin pueda autorizarlos con un clic sin tener que
// buscar el chat_id a mano.
func AgentAuthRecientes(c *fiber.Ctx) error {
tgCfg, err := models.GetAgentTelegramConfig()
if err != nil || tgCfg.BotToken == "" {
return c.Status(400).JSON(fiber.Map{"error": "No hay un bot de Telegram configurado para el agente"})
}
autorizados, _ := models.GetAllAgentAuth()
yaAutorizado := map[int64]bool{}
for _, a := range autorizados {
yaAutorizado[a.ChatID] = true
}
remitentes, err := services.UpdatesRecientesDelBot(tgCfg.BotToken)
if err != nil {
return c.Status(502).JSON(fiber.Map{"error": err.Error()})
}
type item struct {
ChatID int64 `json:"chat_id"`
Nombre string `json:"nombre"`
Mensaje string `json:"mensaje"`
}
seen := map[int64]bool{}
out := make([]item, 0, len(remitentes))
for _, r := range remitentes {
if seen[r.ChatID] || yaAutorizado[r.ChatID] {
continue
}
seen[r.ChatID] = true
out = append(out, item{ChatID: r.ChatID, Nombre: r.Nombre, Mensaje: r.Mensaje})
}
return c.JSON(fiber.Map{"items": out})
}
func AgentAuthList(c *fiber.Ctx) error {
items, err := models.GetAllAgentAuth()
if err != nil {