Phase 4: Support LID identity scheme and fix incoming message parsing
This commit is contained in:
@@ -202,10 +202,100 @@ class WhatsAppController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* Placeholder to trigger Gemini AI Auto-Replies or Keyword rules (Phase 5)
|
||||
* Trigger Gemini AI Auto-Replies or Keyword rules (Phase 5)
|
||||
*/
|
||||
private function triggerAutoReply(array $session, array $msgData)
|
||||
{
|
||||
// To be implemented in Phase 5
|
||||
try {
|
||||
$rule = \App\Models\ChatbotRule::findActiveForRule($session['company_id'], $session['id']);
|
||||
if (!$rule || !$rule['is_active']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$incomingText = trim($msgData['body']);
|
||||
if (empty($incomingText)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$replyText = null;
|
||||
|
||||
if ($rule['trigger_type'] === 'keyword') {
|
||||
$keywords = array_filter(array_map('trim', explode(',', $rule['keyword'])));
|
||||
$matched = false;
|
||||
foreach ($keywords as $kw) {
|
||||
if (mb_stripos($incomingText, $kw) !== false) {
|
||||
$matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($matched) {
|
||||
$replyText = $rule['ai_prompt']; // Under keyword rules, ai_prompt stores the predefined static reply
|
||||
}
|
||||
} elseif ($rule['trigger_type'] === 'gemini_ai') {
|
||||
$apiKey = $rule['gemini_api_key'] ?: getenv('GEMINI_API_KEY');
|
||||
if (empty($apiKey)) {
|
||||
error_log("[Chatbot Warning] Gemini API Key is not set globally or for company " . $session['company_id']);
|
||||
return;
|
||||
}
|
||||
$systemPrompt = $rule['ai_prompt'] ?: 'You are a helpful customer support assistant.';
|
||||
|
||||
$replyText = \App\Services\GeminiService::generateResponse($apiKey, $systemPrompt, $incomingText);
|
||||
}
|
||||
|
||||
if (!empty($replyText)) {
|
||||
// Send reply back to the contact via Node.js Gateway
|
||||
$gatewayUrl = getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722';
|
||||
$sendUrl = $gatewayUrl . '/api/messages/send';
|
||||
|
||||
$payload = json_encode([
|
||||
'session_key' => $session['session_key'],
|
||||
'phone' => $msgData['phone'],
|
||||
'message' => $replyText
|
||||
]);
|
||||
|
||||
$ch = curl_init($sendUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'X-Webhook-Secret: ' . getenv('WEBHOOK_SECRET')
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
$status = 'failed';
|
||||
$errorMsg = null;
|
||||
$waMsgId = null;
|
||||
|
||||
if ($httpCode === 200) {
|
||||
$status = 'sent';
|
||||
$resData = json_decode($response, true);
|
||||
$waMsgId = $resData['data']['key']['id'] ?? null;
|
||||
} else {
|
||||
$resData = json_decode($response, true);
|
||||
$errorMsg = $resData['error'] ?? 'HTTP Code ' . $httpCode;
|
||||
error_log("[Chatbot Gateway Error] failed to send auto-reply: " . $errorMsg);
|
||||
}
|
||||
|
||||
// Log the outbound auto-reply message
|
||||
\App\Models\MessageLog::logMessage([
|
||||
'company_id' => $session['company_id'],
|
||||
'session_id' => $session['id'],
|
||||
'contact_phone' => $msgData['phone'],
|
||||
'direction' => 'outbound',
|
||||
'message_type' => 'text',
|
||||
'message_body' => $replyText,
|
||||
'whatsapp_message_id' => $waMsgId,
|
||||
'status' => $status,
|
||||
'error_message' => $errorMsg
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
error_log("[Chatbot Exception] Error: " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user