From 76eb2f0d5f7b09f1ad149d2a95d7306c74895823 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sun, 24 May 2026 02:08:52 +0300 Subject: [PATCH] Deploy: 2026-05-24 02:08:52 --- .../app/Controllers/WhatsAppController.php | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/backend/app/Controllers/WhatsAppController.php b/backend/app/Controllers/WhatsAppController.php index 2003b91..c8ffcaf 100644 --- a/backend/app/Controllers/WhatsAppController.php +++ b/backend/app/Controllers/WhatsAppController.php @@ -479,12 +479,16 @@ class WhatsAppController extends BaseController private function triggerAutoReply(array $session, array $msgData) { try { + error_log("[triggerAutoReply] Started for phone " . $msgData['phone'] . " company " . $session['company_id']); // Hook the Conversation Flow Engine to intercept messages for active or new flows - if (\App\Core\Flows\ConversationFlowEngine::processMessage($session, $msgData)) { + $flowResult = \App\Core\Flows\ConversationFlowEngine::processMessage($session, $msgData); + error_log("[triggerAutoReply] ConversationFlowEngine returned: " . ($flowResult ? 'true' : 'false')); + if ($flowResult) { return; } $rule = \App\Models\ChatbotRule::findActiveForRule($session['company_id'], $session['id']); + error_log("[triggerAutoReply] ChatbotRule found: " . ($rule ? 'yes' : 'no') . ", is_active: " . ($rule['is_active'] ?? 'N/A')); if (!$rule || !$rule['is_active']) { return; } @@ -493,7 +497,9 @@ class WhatsAppController extends BaseController $hasAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']); $hasImage = !empty($msgData['image']) && !empty($msgData['imageMimeType']); + error_log("[triggerAutoReply] Incoming text: '$incomingText', hasAudio: " . ($hasAudio ? 'yes' : 'no') . ", hasImage: " . ($hasImage ? 'yes' : 'no')); if (empty($incomingText) && !$hasAudio && !$hasImage) { + error_log("[triggerAutoReply] Message is completely empty. Returning."); return; } @@ -509,13 +515,13 @@ class WhatsAppController extends BaseController if ($companyId !== 1) { $activeSub = \App\Models\CompanySubscription::findActiveByCompany($companyId); if (!$activeSub) { - error_log("[Chatbot Warning] Company {$companyId} has no active subscription."); + error_log("[triggerAutoReply] Company {$companyId} has no active subscription. Returning."); return; } // Check general request limit if (!\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request')) { - error_log("[Chatbot Warning] Company {$companyId} has exceeded its general request limit."); + error_log("[triggerAutoReply] Company {$companyId} has exceeded its general request limit. Returning."); return; } @@ -537,6 +543,8 @@ class WhatsAppController extends BaseController } } + error_log("[triggerAutoReply] Limit checks passed. Trigger type: " . $rule['trigger_type']); + if ($replyText === null) { if ($rule['trigger_type'] === 'keyword') { if (empty($incomingText)) { @@ -552,12 +560,16 @@ class WhatsAppController extends BaseController } if ($matched) { $replyText = $rule['ai_prompt']; // Under keyword rules, ai_prompt stores the predefined static reply + error_log("[triggerAutoReply] Keyword matched. Static reply set."); } } elseif ($rule['trigger_type'] === 'gemini_ai') { $configuredGeminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : null; $apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredGeminiKey); + + error_log("[triggerAutoReply] Gemini API Key retrieved: " . (!empty($apiKey) ? 'yes' : 'no')); + if (empty($apiKey)) { - error_log("[Chatbot Warning] Gemini API Key is not set globally or for company " . $session['company_id']); + error_log("[triggerAutoReply] Gemini API Key is not set globally or for company " . $session['company_id'] . ". Returning."); return; }