From eae38cff2e2de9efc83065fdd0143fafc010d185 Mon Sep 17 00:00:00 2001 From: "brobert (aider)" Date: Sun, 23 Mar 2025 22:02:38 +0100 Subject: [PATCH] feat: implement proper user mentions using full JIDs in messages --- src/bot/commands/task.ts | 4 ++-- src/bot/utils/messaging.ts | 17 +++++++++++++---- src/services/notificationService.ts | 2 +- src/utils/userUtils.ts | 4 ++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/bot/commands/task.ts b/src/bot/commands/task.ts index 6680d10..7ed2fc2 100644 --- a/src/bot/commands/task.ts +++ b/src/bot/commands/task.ts @@ -28,8 +28,8 @@ export function handleTaskCommand(body: string, sender: string, groupId: string, try { assignedUser = normalizeUserIdentifier(assignedUserMatch[0]); // Check if assigned user is in the community - if (!linkedGroups.has(`${assignedUser.replace('@', '')}@s.whatsapp.net`)) { - sendMessage(sender, `El usuario ${assignedUser} no está en la comunidad`); + if (!linkedGroups.has(assignedUser)) { + sendMessage(sender, `El usuario ${formatUserMention(assignedUser)} no está en la comunidad`); return; } } catch (error) { diff --git a/src/bot/utils/messaging.ts b/src/bot/utils/messaging.ts index 7ead9f0..a12408e 100644 --- a/src/bot/utils/messaging.ts +++ b/src/bot/utils/messaging.ts @@ -11,13 +11,22 @@ if (!API_URL || !INSTANCE_NAME || !API_KEY) { throw new Error('Required environment variables are not defined.'); } -// Send a message -export async function sendMessage(phone: string, message: string) { +// Send a message with optional mentions +export async function sendMessage(phone: string, message: string, mentions: string[] = []) { try { - await axios.post(`${API_URL}/message/sendText/${INSTANCE_NAME}`, { + const payload: any = { number: phone, text: message, - }, { + }; + + // Add mentions if provided + if (mentions.length > 0) { + payload.contextInfo = { + mentionedJid: mentions + }; + } + + await axios.post(`${API_URL}/message/sendText/${INSTANCE_NAME}`, payload, { headers: { 'Content-Type': 'application/json', apikey: API_KEY, diff --git a/src/services/notificationService.ts b/src/services/notificationService.ts index 3379a75..aca537d 100644 --- a/src/services/notificationService.ts +++ b/src/services/notificationService.ts @@ -21,7 +21,7 @@ export function notifyTaskCreation(task: Task) { const assigneeMessage = `📝 ${creatorMention} te ha asignado una nueva tarea:\n` + `ID: ${task.id}\nDescripción: ${task.description}` + (task.dueDate ? `\nFecha límite: ${task.dueDate}` : ''); - sendMessage(task.assignedTo, assigneeMessage); + sendMessage(task.assignedTo, assigneeMessage, [task.assignedTo]); } } diff --git a/src/utils/userUtils.ts b/src/utils/userUtils.ts index 9e20f46..6daed06 100644 --- a/src/utils/userUtils.ts +++ b/src/utils/userUtils.ts @@ -1,7 +1,7 @@ export function normalizeUserIdentifier(input: string): string { // Handle JID format (12345678@s.whatsapp.net) if (input.includes('@s.whatsapp.net')) { - return `@${input.split('@')[0]}`; + return input; // Return full JID } // Remove @ prefix if present @@ -15,7 +15,7 @@ export function normalizeUserIdentifier(input: string): string { throw new Error('Número de teléfono inválido'); } - return `@${phoneNumber}`; + return `${phoneNumber}@s.whatsapp.net`; } export function extractUserFromJid(jid: string): string {