Deploy: 2026-05-22 01:13:50

This commit is contained in:
Hamza-Ayed
2026-05-22 01:13:50 +03:00
parent 2ea36c98cd
commit da7d3571e0
3 changed files with 112 additions and 9 deletions

View File

@@ -218,13 +218,14 @@ class WhatsAppController extends BaseController
}
// 2. Log the incoming message in history log
$isAudioMsg = !empty($msgData['audio']) && !empty($msgData['mimeType']);
\App\Models\MessageLog::logMessage([
'company_id' => $session['company_id'],
'session_id' => $session['id'],
'contact_phone' => $msgData['phone'],
'direction' => 'inbound',
'message_type' => 'text',
'message_body' => $msgData['body'],
'message_type' => $isAudioMsg ? 'audio' : 'text',
'message_body' => $isAudioMsg ? ($msgData['body'] ?: '[Voice Note]') : $msgData['body'],
'whatsapp_message_id' => $msgData['id'],
'status' => 'read'
]);
@@ -287,14 +288,19 @@ class WhatsAppController extends BaseController
return;
}
$incomingText = trim($msgData['body']);
if (empty($incomingText)) {
$incomingText = isset($msgData['body']) ? trim($msgData['body']) : '';
$hasAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']);
if (empty($incomingText) && !$hasAudio) {
return;
}
$replyText = null;
if ($rule['trigger_type'] === 'keyword') {
if (empty($incomingText)) {
return;
}
$keywords = array_filter(array_map('trim', explode(',', $rule['keyword'])));
$matched = false;
foreach ($keywords as $kw) {
@@ -317,7 +323,15 @@ class WhatsAppController extends BaseController
// Enforce language matching rule dynamically
$systemPrompt .= "\n\nIMPORTANT LANGUAGE RULE: Detect the language of the incoming message. If the incoming message is in English, you MUST reply in English. If the incoming message is in Arabic, you MUST reply in Arabic. Override any default language instruction to match the user's language.";
$replyText = \App\Services\GeminiService::generateResponse($apiKey, $systemPrompt, $incomingText);
if ($hasAudio) {
$mimeType = $msgData['mimeType'];
if (strpos($mimeType, ';') !== false) {
$mimeType = trim(explode(';', $mimeType)[0]);
}
$replyText = \App\Services\GeminiService::generateResponseFromAudio($apiKey, $systemPrompt, $msgData['audio'], $mimeType);
} else {
$replyText = \App\Services\GeminiService::generateResponse($apiKey, $systemPrompt, $incomingText);
}
}
if (!empty($replyText)) {