Deploy: 2026-05-24 02:29:50
This commit is contained in:
@@ -479,28 +479,13 @@ class WhatsAppController extends BaseController
|
|||||||
private function triggerAutoReply(array $session, array $msgData)
|
private function triggerAutoReply(array $session, array $msgData)
|
||||||
{
|
{
|
||||||
try {
|
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
|
// Hook the Conversation Flow Engine to intercept messages for active or new flows
|
||||||
$flowResult = \App\Core\Flows\ConversationFlowEngine::processMessage($session, $msgData);
|
if (\App\Core\Flows\ConversationFlowEngine::processMessage($session, $msgData)) {
|
||||||
error_log("[triggerAutoReply] ConversationFlowEngine returned: " . ($flowResult ? 'true' : 'false'));
|
|
||||||
if ($flowResult) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rule = \App\Models\ChatbotRule::findActiveForRule($session['company_id'], $session['id']);
|
$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']) {
|
if (!$rule || !$rule['is_active']) {
|
||||||
// Debug: Why is there no rule?
|
|
||||||
$allRules = \App\Core\Database::select("SELECT id, company_id, session_id, is_active FROM chatbot_rules WHERE company_id = ?", [$session['company_id']]);
|
|
||||||
error_log("[triggerAutoReply] Debug: Found " . count($allRules) . " rules for company " . $session['company_id'] . ". Rules: " . json_encode($allRules));
|
|
||||||
|
|
||||||
// Test the exact query
|
|
||||||
$q1 = \App\Core\Database::select("SELECT * FROM chatbot_rules WHERE company_id = ? AND (session_id = ? OR session_id IS NULL) AND is_active = 1 LIMIT 1", [$session['company_id'], $session['id']]);
|
|
||||||
error_log("[triggerAutoReply] Debug Q1 (with session): " . json_encode($q1));
|
|
||||||
|
|
||||||
$q2 = \App\Core\Database::select("SELECT * FROM chatbot_rules WHERE company_id = ? AND is_active = 1 LIMIT 1", [$session['company_id']]);
|
|
||||||
error_log("[triggerAutoReply] Debug Q2 (without session): " . json_encode($q2));
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,9 +493,7 @@ class WhatsAppController extends BaseController
|
|||||||
$hasAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']);
|
$hasAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']);
|
||||||
$hasImage = !empty($msgData['image']) && !empty($msgData['imageMimeType']);
|
$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) {
|
if (empty($incomingText) && !$hasAudio && !$hasImage) {
|
||||||
error_log("[triggerAutoReply] Message is completely empty. Returning.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -526,13 +509,13 @@ class WhatsAppController extends BaseController
|
|||||||
if ($companyId !== 1) {
|
if ($companyId !== 1) {
|
||||||
$activeSub = \App\Models\CompanySubscription::findActiveByCompany($companyId);
|
$activeSub = \App\Models\CompanySubscription::findActiveByCompany($companyId);
|
||||||
if (!$activeSub) {
|
if (!$activeSub) {
|
||||||
error_log("[triggerAutoReply] Company {$companyId} has no active subscription. Returning.");
|
error_log("[Chatbot Warning] Company {$companyId} has no active subscription.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check general request limit
|
// Check general request limit
|
||||||
if (!\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request')) {
|
if (!\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request')) {
|
||||||
error_log("[triggerAutoReply] Company {$companyId} has exceeded its general request limit. Returning.");
|
error_log("[Chatbot Warning] Company {$companyId} has exceeded its general request limit.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -554,8 +537,6 @@ class WhatsAppController extends BaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
error_log("[triggerAutoReply] Limit checks passed. Trigger type: " . $rule['trigger_type']);
|
|
||||||
|
|
||||||
if ($replyText === null) {
|
if ($replyText === null) {
|
||||||
if ($rule['trigger_type'] === 'keyword') {
|
if ($rule['trigger_type'] === 'keyword') {
|
||||||
if (empty($incomingText)) {
|
if (empty($incomingText)) {
|
||||||
@@ -571,16 +552,13 @@ class WhatsAppController extends BaseController
|
|||||||
}
|
}
|
||||||
if ($matched) {
|
if ($matched) {
|
||||||
$replyText = $rule['ai_prompt']; // Under keyword rules, ai_prompt stores the predefined static reply
|
$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') {
|
} elseif ($rule['trigger_type'] === 'gemini_ai') {
|
||||||
$configuredGeminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : null;
|
$configuredGeminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : null;
|
||||||
$apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredGeminiKey);
|
$apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredGeminiKey);
|
||||||
|
|
||||||
error_log("[triggerAutoReply] Gemini API Key retrieved: " . (!empty($apiKey) ? 'yes' : 'no'));
|
|
||||||
|
|
||||||
if (empty($apiKey)) {
|
if (empty($apiKey)) {
|
||||||
error_log("[triggerAutoReply] Gemini API Key is not set globally or for company " . $session['company_id'] . ". Returning.");
|
error_log("[Chatbot Warning] Gemini API Key is not set globally or for company " . $session['company_id']);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user