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
+62
View File
@@ -84,3 +84,65 @@ func (ts *TelegramService) SendMessageWithToken(chatID interface{}, message, bot
svc := &TelegramService{BotToken: botToken}
return svc.SendMessage(chatID, message)
}
// RemitenteReciente es alguien que le escribió al bot recientemente, útil para
// descubrir su chat_id sin tener que pedírselo por otro medio.
type RemitenteReciente struct {
ChatID int64
Nombre string
Mensaje string
}
// UpdatesRecientesDelBot consulta getUpdates y devuelve, más reciente primero,
// los remitentes que le han escrito al bot (hasta los últimos 100 updates que
// Telegram todavía tenga en cola).
func UpdatesRecientesDelBot(botToken string) ([]RemitenteReciente, error) {
if botToken == "" {
return nil, fmt.Errorf("bot token vacío")
}
resp, err := http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/getUpdates?limit=100", botToken)) //nolint:noctx
if err != nil {
return nil, fmt.Errorf("no se pudo consultar Telegram: %w", err)
}
defer resp.Body.Close()
var result struct {
OK bool `json:"ok"`
Result []struct {
Message struct {
From struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
} `json:"from"`
Chat struct {
ID int64 `json:"id"`
} `json:"chat"`
Text string `json:"text"`
} `json:"message"`
} `json:"result"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil || !result.OK {
return nil, fmt.Errorf("Telegram no devolvió updates válidos")
}
out := make([]RemitenteReciente, 0, len(result.Result))
for i := len(result.Result) - 1; i >= 0; i-- {
m := result.Result[i].Message
if m.Chat.ID == 0 {
continue
}
nombre := m.From.FirstName
if m.From.LastName != "" {
nombre += " " + m.From.LastName
}
if m.From.Username != "" {
nombre += " (@" + m.From.Username + ")"
}
if nombre == "" {
nombre = fmt.Sprintf("Chat %d", m.Chat.ID)
}
out = append(out, RemitenteReciente{ChatID: m.Chat.ID, Nombre: nombre, Mensaje: m.Text})
}
return out, nil
}