From 0e9c6935d8bdb2ba03e3724b2b1f182881faaf06 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 13 Mar 2026 19:10:43 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20evitar=20doble=20saludo=20y=20URL=20dupl?= =?UTF-8?q?icada=20en=20mensaje=20de=20t=C3=A9rminos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - sendTermsMessage: solo agrega PDF al final si la URL no está ya en el mensaje - sendTermsMessage: marca welcome_sent_at=now() junto con terms_pending=1 para evitar race condition donde un segundo webhook concurrente cae en isNewUser() y envía la bienvenida mientras los términos ya fueron enviados --- services/BotService.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/services/BotService.php b/services/BotService.php index 1e1616d..effd256 100644 --- a/services/BotService.php +++ b/services/BotService.php @@ -503,7 +503,7 @@ class BotService { if (!$msg) return; - // Adjuntar URL del documento solo si es una URL pública (no localhost) + // Adjuntar URL del documento solo si es una URL pública y no está ya incluida en el mensaje if (!empty($terms['documento_url'])) { $docUrl = $terms['documento_url']; $isLocal = ( @@ -511,7 +511,7 @@ class BotService { strpos($docUrl, '127.0.0.1') !== false || preg_match('/https?:\/\/\d+\.\d+\.\d+\.\d+:\d+/', $docUrl) ); - if (!$isLocal) { + if (!$isLocal && strpos($msg, $docUrl) === false) { $msg .= "\n\n📄 Documento: " . $docUrl; } } @@ -519,10 +519,12 @@ class BotService { $this->whatsappService->sendTextMessage($phoneNumber, $msg); // Marcar usuario como "esperando respuesta de términos" + // También marcamos welcome_sent_at para evitar que una llamada concurrente + // al webhook envíe el mensaje de bienvenida por race condition. try { $this->db->update( 'users', - ['terms_pending' => 1], + ['terms_pending' => 1, 'welcome_sent_at' => date('Y-m-d H:i:s')], 'id = :id', ['id' => $user['id']] );