feat: telegram chat ID en usuarios (UI + notificacion al asignado)

This commit is contained in:
Lizandro Guarnizo
2026-07-07 00:26:31 -05:00
parent 6a3793a63b
commit 62397f2f8c
2 changed files with 47 additions and 7 deletions
+24
View File
@@ -297,6 +297,11 @@ func NotificarTareaAsignada(t *models.Tarea) {
escapeTelegramHTML(t.Prioridad),
escapeTelegramHTML(t.Descripcion))
sendTelegramAdmin(msg)
// También enviar al chat del usuario asignado si tiene TelegramChatID
if t.Asignado != nil && t.Asignado.TelegramChatID != "" {
go sendTelegramToUser(t.Asignado.TelegramChatID, msg)
}
}
}
@@ -412,3 +417,22 @@ func sendTelegramAdmin(mensaje string) {
_ = models.CreateTelegramLog(logEntry)
}
}
// sendTelegramToUser envía un mensaje al chat de un usuario usando el primer bot config activo.
func sendTelegramToUser(chatID string, mensaje string) {
configs, err := models.GetAllTelegramConfigs()
if err != nil || len(configs) == 0 {
log.Printf("[Notif] No hay configs telegram para enviar a usuario %s", chatID)
return
}
for _, cfg := range configs {
if !cfg.Activo {
continue
}
ts := &TelegramService{BotToken: cfg.BotToken}
if err := ts.SendMessage(chatID, mensaje); err != nil {
log.Printf("[Notif] Error enviando telegram a %s: %v", chatID, err)
}
return
}
}
+23 -7
View File
@@ -120,6 +120,7 @@
Estado = user.estado;
Email = user.email;
roleName = user.Role.ID;
TelegramChatID = user.telegram_chat_id || '';
" title="Editar">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke-width="1.5" stroke="currentColor" class="w-5">
@@ -218,12 +219,18 @@
<!-- Rol -->
<label class="block mt-4 mb-2" for="editRole">Rol</label>
<select id="editRole" x-model="roleName" class="border border-gray-300 rounded w-full p-2" required>
<option value="" disabled selected>Selecciona un rol</option> <!-- Opción predeterminada -->
<option value="" disabled selected>Selecciona un rol</option>
<template x-for="role in roles" :key="role.ID">
<option :value="role.ID" x-text="role.name"></option>
</template>
</select>
<!-- Telegram -->
<label class="block mt-4 mb-2" for="editTelegram">Chat ID de Telegram</label>
<input id="editTelegram" x-model="TelegramChatID" placeholder="Ej: 123456789"
class="border border-gray-300 rounded w-full p-2 font-mono">
<p class="text-[10px] text-slate-400 mt-0.5">Para recibir notificaciones de tareas por Telegram.</p>
<div class="mt-4 flex justify-between">
<button type="submit" class="bg-[#8eb02f] text-white px-4 py-2 rounded">Guardar Cambios</button>
<button type="button" class="bg-gray-600 text-white px-4 py-2 rounded"
@@ -257,6 +264,10 @@
<option :value="role.ID" x-text="role.name"></option>
</template>
</select>
<label class="block mt-4 mb-2" for="addTelegram">Chat ID de Telegram</label>
<input id="addTelegram" x-model="newTelegramChatID" placeholder="Ej: 123456789"
class="border border-gray-300 rounded w-full p-2 font-mono">
<p class="text-[10px] text-slate-400 mt-0.5">Para recibir notificaciones de tareas por Telegram.</p>
<div class="mt-4 flex justify-between">
<button type="submit" class="bg-[#8eb02f] text-white px-4 py-2 rounded">Guardar </button>
<button type="button" class="bg-gray-600 text-white px-4 py-2 rounded"
@@ -310,6 +321,7 @@
newEmail: '',
newRole: '',
newNombreUsuario: '',
newTelegramChatID: '',
Name: '',
NombreUsuario: '',
@@ -317,6 +329,7 @@
NewEstado: '',
Email: '',
roleName: '',
TelegramChatID: '',
password: '',
userId: '',
@@ -433,7 +446,8 @@
email: this.newEmail,
role_id: parseInt(this.newRole),
estado: true,
tipo: 'sistema'
tipo: 'sistema',
telegram_chat_id: this.newTelegramChatID
})
})
.then(response => response.json())
@@ -442,6 +456,7 @@
this.newNombreUsuario = '';
this.newEmail = '';
this.newRole = '';
this.newTelegramChatID = '';
this.closeModal();
this.loadRegister();
})
@@ -457,19 +472,20 @@
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: this.Name, // Usar 'Name' en lugar de 'First_name'
nombre_usuario: this.NombreUsuario, // Usar 'Nombre_usuario' en lugar de 'Last_name'
email: this.Email, // Usar 'Email'
role_id: parseInt(this.roleName) // Usar 'roleName'
name: this.Name,
nombre_usuario: this.NombreUsuario,
email: this.Email,
role_id: parseInt(this.roleName),
telegram_chat_id: this.TelegramChatID
})
})
.then(response => response.json())
.then(() => {
// Limpiar campos después de la actualización
this.Name = '';
this.NombreUsuario = '';
this.Email = '';
this.roleName = '';
this.TelegramChatID = '';
this.closeModal();
this.loadRegister();
})