Deploy: 2026-05-22 03:37:23
This commit is contained in:
@@ -343,12 +343,21 @@ class WhatsAppController extends BaseController
|
||||
$infoContext = $this->fetchUserInfoFromEndpoint($infoEndpoint, $msgData['phone']);
|
||||
}
|
||||
|
||||
// Dynamically fetch Salla order context if connected
|
||||
$sallaContext = "";
|
||||
if (!empty($msgData['phone'])) {
|
||||
$sallaContext = $this->fetchSallaOrderContext($session['company_id'], $msgData['phone'], $incomingText);
|
||||
}
|
||||
|
||||
$systemPrompt = $rule['ai_prompt'] ?: 'You are a helpful customer support assistant.';
|
||||
|
||||
// Append real-time info context to Gemini system prompt
|
||||
if (!empty($infoContext)) {
|
||||
$systemPrompt .= "\n\n" . $infoContext;
|
||||
}
|
||||
if (!empty($sallaContext)) {
|
||||
$systemPrompt .= "\n\n" . $sallaContext;
|
||||
}
|
||||
|
||||
// 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.";
|
||||
@@ -578,4 +587,124 @@ class WhatsAppController extends BaseController
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch order info context from Salla e-commerce platform for the company
|
||||
*/
|
||||
private function fetchSallaOrderContext(int $companyId, string $phone, string $incomingText): string
|
||||
{
|
||||
try {
|
||||
$accessToken = \App\Models\SallaMerchant::getOrRefreshAccessToken($companyId);
|
||||
if (!$accessToken) {
|
||||
return ""; // Salla is not integrated
|
||||
}
|
||||
|
||||
// Standardize customer phone to compare last 9 digits
|
||||
$cleanPhone = preg_replace('/\D/', '', $phone);
|
||||
if (empty($cleanPhone)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 1. Check if user is asking about a specific order ID (e.g. sequence of 5-12 digits)
|
||||
if (preg_match('/\b(\d{5,12})\b/', $incomingText, $matches)) {
|
||||
$orderId = $matches[1];
|
||||
|
||||
$ch = curl_init("https://api.salla.dev/admin/v2/orders/{$orderId}");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Authorization: Bearer ' . $accessToken
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode === 200) {
|
||||
$orderRes = json_decode($response, true);
|
||||
$order = $orderRes['data'] ?? null;
|
||||
if ($order) {
|
||||
$orderCustPhone = preg_replace('/\D/', '', $order['customer']['mobile'] ?? $order['customer']['phone'] ?? '');
|
||||
// Security check: match last 9 digits of WhatsApp phone to order customer phone
|
||||
if (substr($orderCustPhone, -9) === substr($cleanPhone, -9)) {
|
||||
$status = $order['status']['name'] ?? 'غير معروف';
|
||||
$total = $order['amounts']['total']['amount'] ?? $order['total'] ?? '';
|
||||
$currency = $order['amounts']['total']['currency'] ?? 'SAR';
|
||||
$courier = $order['shipment']['courier_name'] ?? '';
|
||||
$tracking = $order['shipment']['tracking_link'] ?? '';
|
||||
$itemsCount = count($order['items'] ?? []);
|
||||
|
||||
$context = "\n\n[تفاصيل طلب سلة المستعلم عنه للعميل:\n";
|
||||
$context .= "- رقم الطلب: {$orderId}\n";
|
||||
$context .= "- حالة الطلب الحالية: {$status}\n";
|
||||
$context .= "- إجمالي الطلب: {$total} {$currency}\n";
|
||||
$context .= "- عدد المنتجات: {$itemsCount}\n";
|
||||
if (!empty($courier)) {
|
||||
$context .= "- شركة الشحن: {$courier}\n";
|
||||
}
|
||||
if (!empty($tracking)) {
|
||||
$context .= "- رابط تتبع الشحنة: {$tracking}\n";
|
||||
}
|
||||
$context .= "الرجاء صياغة رد ودود ومختصر باللغة العربية لإخبار العميل بحالة هذا الطلب بالتحديد]";
|
||||
return $context;
|
||||
} else {
|
||||
// Order ID exists but phone mismatch
|
||||
return "\n\n[تنبيه أمني للذكاء الاصطناعي: العميل سأل عن الطلب رقم {$orderId} ولكن هذا الطلب مسجل برقم هاتف مختلف في سلة. لحماية الخصوصية والأمان، يمنع منعاً باتاً عرض تفاصيل هذا الطلب له. أخبر العميل بلطف أن رقم الهاتف الحالي لا يتطابق مع رقم الهاتف المسجل في تفاصيل هذا الطلب ولا يمكنك كشف تفاصيله]";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fetch list of recent orders for the merchant and search by customer phone number
|
||||
$ch = curl_init("https://api.salla.dev/admin/v2/orders?page=1");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Authorization: Bearer ' . $accessToken
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode === 200) {
|
||||
$ordersRes = json_decode($response, true);
|
||||
$orders = $ordersRes['data'] ?? [];
|
||||
if (is_array($orders)) {
|
||||
foreach ($orders as $order) {
|
||||
$orderCustPhone = preg_replace('/\D/', '', $order['customer']['mobile'] ?? $order['customer']['phone'] ?? '');
|
||||
if (substr($orderCustPhone, -9) === substr($cleanPhone, -9)) {
|
||||
// Found the most recent order for this customer
|
||||
$orderId = $order['id'] ?? $order['reference_id'] ?? '';
|
||||
$status = $order['status']['name'] ?? 'غير معروف';
|
||||
$total = $order['amounts']['total']['amount'] ?? $order['total'] ?? '';
|
||||
$currency = $order['amounts']['total']['currency'] ?? 'SAR';
|
||||
$courier = $order['shipment']['courier_name'] ?? '';
|
||||
$tracking = $order['shipment']['tracking_link'] ?? '';
|
||||
$itemsCount = count($order['items'] ?? []);
|
||||
|
||||
$context = "\n\n[آخر طلب للعميل في متجر سلة:\n";
|
||||
$context .= "- رقم الطلب: {$orderId}\n";
|
||||
$context .= "- حالة الطلب الحالية: {$status}\n";
|
||||
$context .= "- إجمالي الطلب: {$total} {$currency}\n";
|
||||
$context .= "- عدد المنتجات: {$itemsCount}\n";
|
||||
if (!empty($courier)) {
|
||||
$context .= "- شركة الشحن: {$courier}\n";
|
||||
}
|
||||
if (!empty($tracking)) {
|
||||
$context .= "- رابط تتبع الشحنة: {$tracking}\n";
|
||||
}
|
||||
$context .= "الرجاء استخدام هذه التفاصيل للإجابة على استفساره حول حالة طلبه الأخير بدقة وود باللغة العربية]";
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "\n\n[سياق المتجر: العميل متصل بمتجر سلة ولكن لم يتم العثور على أي طلبات سابقة له برقم الهاتف هذا. أخبره بلطف أنه لا توجد طلبات سابقة مسجلة برقم هاتفه الحالي في المتجر، واطلب منه تزويدك برقم الطلب أو البريد الإلكتروني للبحث]";
|
||||
|
||||
} catch (\Exception $e) {
|
||||
error_log("[Fetch Salla Order Exception] " . $e->getMessage());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user