diff --git a/services/BotService.php b/services/BotService.php index 0874d64..f800550 100644 --- a/services/BotService.php +++ b/services/BotService.php @@ -976,13 +976,22 @@ class BotService { } } - public function releaseHold($phoneNumber) { + public function releaseHold($phoneNumber, $notify = true) { try { // Clear hold/advisor flags and also ensure in_service is cleared for consistency // Clear hold and reactivate bot $this->db->update('users', ['on_hold' => 0, 'bot_paused_until' => null, 'advisor_requested' => 0, 'in_service' => 0, 'in_service_by' => null, 'in_service_at' => null, 'bot_enabled' => 1], 'phone_number = :phone', ['phone' => $phoneNumber]); - $this->whatsappService->sendTextMessage($phoneNumber, "🔔 Su conversación ha sido retomada por el equipo. Puede usar *MENU* para continuar."); - if (function_exists('writeLog')) writeLog('INFO', "Advisor released hold for $phoneNumber"); + + // Send notification only when requested (avoid duplicate messages after finish) + if ($notify) { + try { + $this->whatsappService->sendTextMessage($phoneNumber, "🔔 Su conversación ha sido retomada por el equipo. Puede usar *MENU* para continuar."); + } catch (Exception $e) { + // ignore send errors + } + } + + if (function_exists('writeLog')) writeLog('INFO', "Advisor released hold for $phoneNumber notify=" . ($notify ? '1' : '0')); } catch (Exception $e) { error_log('releaseHold failed: ' . $e->getMessage()); } @@ -1074,7 +1083,14 @@ class BotService { // Ensure consistent behavior: release hold / clear flags when finishing attend try { - $this->releaseHold($phoneNumber); + // Release hold silently to avoid duplicate notification after finishing attend + $this->releaseHold($phoneNumber, false); + // Re-send main menu/topic so the user sees the menu after the advisor finishes + try { + $this->showMainMenu($phoneNumber); + } catch (Exception $e) { + error_log('finishAttendConversation: showMainMenu failed: ' . $e->getMessage()); + } } catch (Exception $e) { error_log('finishAttendConversation: releaseHold failed: ' . $e->getMessage()); }