From 03f02d2788191b302281f258121dccb5936d3461 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 3 Jul 2026 08:43:20 -0500 Subject: [PATCH] fix(webhook): skip SSE notification and DB notification for canal=turnero messages Turnero messages were triggering the SSE event that conversations.php listens to, causing the contact to flash briefly at the top of the list before disappearing on the next poll (since get_conversations.php filters them out by canal). Co-Authored-By: Claude Sonnet 4.6 --- api/webhook.php | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/api/webhook.php b/api/webhook.php index 2ec6ec5..1feb060 100644 --- a/api/webhook.php +++ b/api/webhook.php @@ -395,30 +395,31 @@ class WhatsAppWebhook { } } - // Crear notificación para UI (nuevo mensaje entrante) - try { - $this->db->insert('notifications', [ + // Notificaciones y SSE solo para el canal principal (no turnero) + if (!$isTurnero) { + try { + $this->db->insert('notifications', [ + 'user_id' => $user['id'], + 'type' => 'incoming_message', + 'message' => substr($messageText, 0, 250), + 'data' => json_encode(['message_id' => $messageId]), + 'is_read' => 0, + 'created_at' => date('Y-m-d H:i:s') + ]); + } catch (Exception $e) { + error_log('Failed to create notification: ' . $e->getMessage()); + } + + $this->pushSSEEvent('new_message', [ 'user_id' => $user['id'], - 'type' => 'incoming_message', + 'phone_number' => $user['phone_number'], + 'name' => $user['name'] ?? ($phoneNumber ?? ''), 'message' => substr($messageText, 0, 250), - 'data' => json_encode(['message_id' => $messageId]), - 'is_read' => 0, - 'created_at' => date('Y-m-d H:i:s') + 'message_type' => $messageType, + 'timestamp' => date('Y-m-d H:i:s') ]); - } catch (Exception $e) { - error_log('Failed to create notification: ' . $e->getMessage()); } - // Empujar evento SSE en tiempo real - $this->pushSSEEvent('new_message', [ - 'user_id' => $user['id'], - 'phone_number' => $user['phone_number'], - 'name' => $user['name'] ?? $from, - 'message' => substr($messageText, 0, 250), - 'message_type' => $messageType, - 'timestamp' => date('Y-m-d H:i:s') - ]); - // Procesar con bot solo si el mensaje llegó al número principal if (!$isTurnero) { try {