Deploy: 2026-05-22 21:52:51
This commit is contained in:
@@ -16,6 +16,7 @@ class ConversationFlowEngine
|
||||
*/
|
||||
private static array $flows = [
|
||||
'test_flow' => TestFlow::class,
|
||||
'driver_registration_flow' => DriverRegistrationFlow::class,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -24,6 +25,9 @@ class ConversationFlowEngine
|
||||
private static array $startTriggers = [
|
||||
'test' => 'test_flow',
|
||||
'اختبار' => 'test_flow',
|
||||
'سجل' => 'driver_registration_flow',
|
||||
'تسجيل' => 'driver_registration_flow',
|
||||
'register' => 'driver_registration_flow',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -39,6 +43,20 @@ class ConversationFlowEngine
|
||||
$companyId = $session['company_id'];
|
||||
$text = isset($msgData['body']) ? trim($msgData['body']) : '';
|
||||
|
||||
// If incoming message is audio, transcribe it via Gemini
|
||||
$isAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']);
|
||||
if ($isAudio) {
|
||||
$rule = \App\Models\ChatbotRule::findActiveForRule($companyId);
|
||||
$apiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : getenv('GEMINI_API_KEY');
|
||||
if (!empty($apiKey)) {
|
||||
$transcription = \App\Services\GeminiService::transcribeAudio($apiKey, $msgData['audio'], $msgData['mimeType']);
|
||||
if ($transcription) {
|
||||
$text = $transcription;
|
||||
$msgData['body'] = $transcription;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Lookup existing active flow
|
||||
$state = ConversationState::findActive($companyId, $phone);
|
||||
|
||||
@@ -84,6 +102,7 @@ class ConversationFlowEngine
|
||||
$flowInstance = new $flowClass();
|
||||
|
||||
try {
|
||||
$context['company_id'] = $companyId;
|
||||
$result = $flowInstance->handleStep($currentStep, $msgData, $context);
|
||||
|
||||
if ($result->isFinished()) {
|
||||
@@ -104,8 +123,8 @@ class ConversationFlowEngine
|
||||
}
|
||||
|
||||
// 5. Send reply if one is provided
|
||||
if ($result->getReplyText() !== '') {
|
||||
self::sendReply($session, $phone, $result->getReplyText());
|
||||
if ($result->getReplyText() !== '' || $result->getMediaUrl() !== null) {
|
||||
self::sendReply($session, $phone, $result->getReplyText(), $result->getMediaUrl());
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -118,8 +137,14 @@ class ConversationFlowEngine
|
||||
/**
|
||||
* Send outbound message reply via the Baileys Gateway
|
||||
*/
|
||||
private static function sendReply(array $session, string $phone, string $message): void
|
||||
{
|
||||
public static function sendReply(
|
||||
array $session,
|
||||
string $phone,
|
||||
string $message,
|
||||
?string $mediaUrl = null,
|
||||
?string $audioBase64 = null,
|
||||
?string $mimetype = null
|
||||
): void {
|
||||
$gatewayUrl = rtrim(getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722', '/');
|
||||
if (substr($gatewayUrl, -4) === '/api') {
|
||||
$sendUrl = $gatewayUrl . '/messages/send';
|
||||
@@ -127,11 +152,25 @@ class ConversationFlowEngine
|
||||
$sendUrl = $gatewayUrl . '/api/messages/send';
|
||||
}
|
||||
|
||||
$payload = json_encode([
|
||||
$payloadData = [
|
||||
'session_key' => $session['session_key'],
|
||||
'phone' => $phone,
|
||||
'message' => $message
|
||||
]);
|
||||
'phone' => $phone
|
||||
];
|
||||
|
||||
if ($message !== '') {
|
||||
$payloadData['message'] = $message;
|
||||
}
|
||||
if ($mediaUrl !== null) {
|
||||
$payloadData['media_url'] = $mediaUrl;
|
||||
}
|
||||
if ($audioBase64 !== null) {
|
||||
$payloadData['audio'] = $audioBase64;
|
||||
}
|
||||
if ($mimetype !== null) {
|
||||
$payloadData['mimetype'] = $mimetype;
|
||||
}
|
||||
|
||||
$payload = json_encode($payloadData);
|
||||
|
||||
$ch = curl_init($sendUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
@@ -161,14 +200,17 @@ class ConversationFlowEngine
|
||||
error_log("[Flow Engine Gateway Error] Failed to send: " . $errorMsg);
|
||||
}
|
||||
|
||||
$msgType = ($audioBase64 !== null || $mediaUrl !== null) ? 'audio' : 'text';
|
||||
|
||||
// Log the outbound auto-reply message
|
||||
MessageLog::logMessage([
|
||||
'company_id' => $session['company_id'],
|
||||
'session_id' => $session['id'],
|
||||
'contact_phone' => $phone,
|
||||
'direction' => 'outbound',
|
||||
'message_type' => 'text',
|
||||
'message_type' => $msgType,
|
||||
'message_body' => $message,
|
||||
'media_url' => $mediaUrl,
|
||||
'whatsapp_message_id' => $waMsgId,
|
||||
'status' => $status,
|
||||
'error_message' => $errorMsg
|
||||
|
||||
483
backend/app/Core/Flows/DriverRegistrationFlow.php
Normal file
483
backend/app/Core/Flows/DriverRegistrationFlow.php
Normal file
@@ -0,0 +1,483 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core\Flows;
|
||||
|
||||
use App\Services\GeminiService;
|
||||
use App\Models\DriverOcrData;
|
||||
use App\Models\ChatbotRule;
|
||||
use App\Models\DriverReminder;
|
||||
|
||||
/**
|
||||
* DriverRegistrationFlow
|
||||
* Handles step-by-step driver and vehicle registration using Gemini OCR.
|
||||
*/
|
||||
class DriverRegistrationFlow extends BaseFlow
|
||||
{
|
||||
private array $prompts = [
|
||||
"id_front" => <<<EOT
|
||||
You are an OCR expert for Syrian national ID cards (green card).
|
||||
|
||||
### TASK
|
||||
Analyse the **front side** of the ID and return **raw JSON only** with exactly these keys:
|
||||
|
||||
{
|
||||
"full_name": "", // الاسم الثلاثي أو الرباعي
|
||||
"national_number": "", // الرقم الوطني (LATIN digits only)
|
||||
"dob": "YYYY-MM-DD", // تاريخ الميلاد
|
||||
"address": "" // العنوان
|
||||
}
|
||||
|
||||
### RULES
|
||||
* Read the red number on the bottom of the card.
|
||||
* Convert any Eastern-Arabic digits (٠١٢٣٤٥٦٧٨٩) to Western-Arabic digits (0-9).
|
||||
* `national_number` must contain **Latin digits only, no spaces or other characters**.
|
||||
* If a field is missing, set it to **null**.
|
||||
* Convert the birth date to ISO `YYYY-MM-DD`.
|
||||
* Return valid JSON only — no extra keys, no markdown.
|
||||
EOT,
|
||||
"id_back" => <<<EOT
|
||||
أنت خبير OCR مختص ببطاقات الهوية السورية (الوجه الخلفي).
|
||||
|
||||
### المطلوب
|
||||
حلّل صورة الوجه الخلفي للهوية السورية وأعد **JSON صِرف** يحتوي المفاتيح التالية فقط:
|
||||
|
||||
{
|
||||
"governorate": "", // المحافظة (مثال: دمشق)
|
||||
"address": "", // العنوان التفصيلي (حيّ، بلدة …)
|
||||
"gender": "", //Male or Female
|
||||
"issue_date": "YYYY-MM-DD"// تاريخ الإصدار بصيغة ISO
|
||||
}
|
||||
|
||||
### القواعد
|
||||
1. حوّل أي أرقام عربية شرقية (٠١٢٣٤٥٦٧٨٩) إلى أرقام لاتينية (0-9).
|
||||
2. أعدّ تاريخ الإصدار بالتقويم الميلادي بصيغة `YYYY-MM-DD`.
|
||||
3. استخدم أحرف لاتينية كبيرة لزمرة الدم مع رمز `+` أو `-` فقط.
|
||||
4. إذا كان أحد الحقول غير موجود مطلقًا، أعد قيمته **null**.
|
||||
5. لا تُرجع أي مفاتيح إضافية أو شروح أو Markdown — JSON صالح فقط.
|
||||
EOT,
|
||||
"driving_license_front" => <<<EOT
|
||||
You are an OCR expert for Syrian documents.
|
||||
|
||||
### TASK
|
||||
Analyse the **front side of a Syrian driving licence** and return **clean JSON only** with the following keys (no extra keys, no markdown):
|
||||
|
||||
{
|
||||
"name_arabic": "", // الاسم الثلاثي أو الرباعي بالعربية
|
||||
"birth_place": "", // المحافظة أو المنطقة المكتوبة بعد كلمة الولادة
|
||||
"birth_year": "", // سنة الميلاد فقط (أربعة أرقام)
|
||||
"national_number": "",
|
||||
"civil_registry": "", // سطر "القيد" (مثال: سهوة 3)
|
||||
"blood_type": "" // زمرة الدم بالشكل: A+ , A- , B+ , B- , AB+ , AB- , O+ , O-
|
||||
}
|
||||
|
||||
### RULES
|
||||
* إذا كانت القيمة مفقودة تمامًا اكتب **null**.
|
||||
* لا تُغيّر ترتيب المفاتيح.
|
||||
* لا تُرسل أى شرح أو أسطر إضافية – JSON خالص فقط.
|
||||
EOT,
|
||||
"driving_license_back" => <<<EOT
|
||||
You are an OCR expert for Syrian driving licences.
|
||||
|
||||
### TASK
|
||||
Analyse the **back side** of a Syrian driving licence and return **raw JSON only** with exactly these keys:
|
||||
|
||||
{
|
||||
"issue_date": "YYYY-MM-DD", // تاريخ المنح
|
||||
"expiry_date": "YYYY-MM-DD", // صالحة لغاية
|
||||
"license_number": "", // رقم الإجازة
|
||||
"license_category": "" // D1, D2, D3 … (as printed after "UNIVERSAL DRIVING LICENCE")
|
||||
}
|
||||
|
||||
### RULES
|
||||
* If a value is totally absent, set it to **null**.
|
||||
* Convert all dates to ISO `YYYY-MM-DD` (Gregorian).
|
||||
* Do **NOT** add extra keys, comments, or markdown — return valid JSON only.
|
||||
EOT,
|
||||
"vehicle_license_front" => <<<EOT
|
||||
You are an OCR expert specialized in analyzing Syrian vehicle registration cards (الرخصة البرتقالية).
|
||||
|
||||
Your task is to extract structured data from the **front side** of the Syrian orange vehicle card and return **raw JSON only** with the following exact fields:
|
||||
|
||||
{
|
||||
"car_plate": "", // رقم المركبة الكامل مع اسم المحافظة، مأخوذ من الجهة اليسرى في السطر الأول (مثال: "155186 درعا")
|
||||
"owner": "", // اسم المالك الكامل
|
||||
"vin": "", // رقم الهيكل
|
||||
"color": "", // اللون بالعربية أو الإنجليزية (مثال: "أبيض" أو "White")
|
||||
"color_hex": "", // كود اللون بصيغة Hex (مثال: "#FFFFFF") أو #27332F إن تعذّر
|
||||
"issue_date": "YYYY-MM-DD", // تاريخ المنح بصيغة ISO
|
||||
"inspection_date": "YYYY-MM-DD" // تاريخ الفحص القادم بصيغة ISO
|
||||
}
|
||||
|
||||
### Instructions & Rules:
|
||||
|
||||
1. Do **not** extract the "رمز المركبة" (on the right side of the first line) — use only the **left side** of the first line for `car_plate`.
|
||||
2. Convert any Arabic dates (like `2024/05/13`) into ISO format `YYYY-MM-DD`.
|
||||
3. If any value is missing or unreadable, return `null` for it.
|
||||
4. Maintain Arabic encoding (e.g., owner name, city name, color).
|
||||
5. Never guess — extract only what's visually found on the card.
|
||||
6. Never include any explanation or extra output — return the JSON only.
|
||||
|
||||
Example of valid `car_plate`:
|
||||
- "155186 درعا"
|
||||
- "45291 دمشق"
|
||||
- "122334 حمص"
|
||||
EOT,
|
||||
"vehicle_license_back" => <<<EOT
|
||||
You are an OCR expert for Syrian vehicle registration cards (orange card).
|
||||
|
||||
### TASK
|
||||
Analyse the **back side** of the card and return **raw JSON only** with exactly these keys (no more, no less):
|
||||
|
||||
{
|
||||
"make": "", // الصانع (Hyundai …)
|
||||
"model": "", // الطراز (H1 …)
|
||||
"year": "", // سنة الصنع بالأرقام اللاتينية (e.g. "2019")
|
||||
"fuel": "", // نوع الوقود (بنزين، ديزل …) أو بالإنجليزية (Petrol, Diesel,electric)
|
||||
"chassis": "" // رقم الهيكل (VIN)
|
||||
}
|
||||
|
||||
### RULES
|
||||
* Convert any Eastern-Arabic digits (٠١٢٣٤٥٦٧٨٩) to Western digits (0-9).
|
||||
* Normalise color names to standard English if possible, then map to a common Hex code
|
||||
• "أبيض / White" → **#FFFFFF**
|
||||
• "أسود / Black" → **#000000**
|
||||
• "أحمر / Red" → **#FF0000**
|
||||
• "أزرق / Blue" → **#0000FF**
|
||||
• … (use the closest basic colour); if no match, set **color_hex = null**.
|
||||
* If any field is unreadable or absent, set its value to **null**.
|
||||
* Do **NOT** include extra keys, comments, or markdown — output valid JSON only.
|
||||
EOT
|
||||
];
|
||||
|
||||
public function handleStep(string $step, array $messageData, array &$context): FlowResult
|
||||
{
|
||||
$text = isset($messageData['body']) ? trim($messageData['body']) : '';
|
||||
$phone = $messageData['phone'];
|
||||
$companyId = $context['company_id'] ?? 1;
|
||||
|
||||
// If currently postponed and user sends a message, resume the flow
|
||||
if ($step === 'postponed') {
|
||||
// Cancel active reminder
|
||||
$activeReminder = DriverReminder::findActive($companyId, $phone);
|
||||
if ($activeReminder) {
|
||||
DriverReminder::update($activeReminder['id'], ['status' => 'cancelled']);
|
||||
}
|
||||
// Restore previous step
|
||||
$step = $context['previous_step'] ?? 'ask_name';
|
||||
}
|
||||
|
||||
// Check if user requests postponement/delay (only if already started and not finished)
|
||||
if ($step !== 'start' && $step !== 'finished' && !empty($text)) {
|
||||
$rule = ChatbotRule::findActiveForRule($companyId);
|
||||
$apiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : getenv('GEMINI_API_KEY');
|
||||
if (!empty($apiKey)) {
|
||||
$postponeData = $this->detectPostponement($text, $apiKey);
|
||||
if ($postponeData !== null) {
|
||||
$hours = $postponeData['hours'];
|
||||
$postponeCount = ($context['postpone_count'] ?? 0) + 1;
|
||||
|
||||
if ($postponeCount > 3) {
|
||||
return new FlowResult(
|
||||
"عذراً كابتن، لقد تجاوزت الحد الأقصى لمرات التأجيل (3 مرات). تم إلغاء طلب التسجيل الحالي. يمكنك البدء من جديد عندما تكون جاهزاً بكتابة 'تسجيل'.",
|
||||
"finished",
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
$context['postpone_count'] = $postponeCount;
|
||||
$context['previous_step'] = $step;
|
||||
|
||||
// Schedule reminder
|
||||
$scheduledAt = date('Y-m-d H:i:s', strtotime("+{$hours} hours"));
|
||||
DriverReminder::saveReminder([
|
||||
'company_id' => $companyId,
|
||||
'phone' => $phone,
|
||||
'scheduled_at' => $scheduledAt,
|
||||
'postpone_count' => $postponeCount,
|
||||
'status' => 'pending'
|
||||
]);
|
||||
|
||||
return new FlowResult(
|
||||
"حاضر كابتن، قمت بتأجيل التسجيل. سأقوم بتذكيرك بعد {$hours} ساعة لإكمال خطوات التسجيل. بالتوفيق!",
|
||||
"postponed"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch ($step) {
|
||||
case 'start':
|
||||
return new FlowResult(
|
||||
"أهلاً بك كابتن في خدمة تسجيل كباتن تطبيق انطلق 🚖.\nيرجى إرسال اسمك الثلاثي الكامل للبدء:",
|
||||
"ask_name"
|
||||
);
|
||||
|
||||
case 'ask_name':
|
||||
if (empty($text)) {
|
||||
return new FlowResult("يرجى إدخال اسمك الثلاثي الكامل للاستمرار:", "ask_name");
|
||||
}
|
||||
$context['name'] = $text;
|
||||
return new FlowResult(
|
||||
"شكراً كابتن {$text}.\nالآن يرجى إرسال صورة **الوجه الأمامي للهوية الشخصية** (تأكد من أن الصورة واضحة والإضاءة جيدة):",
|
||||
"id_front"
|
||||
);
|
||||
|
||||
case 'id_front':
|
||||
return $this->processOcrStep(
|
||||
$step,
|
||||
$messageData,
|
||||
$context,
|
||||
"id_front_sy",
|
||||
"national_number",
|
||||
"عذراً كابتن، لم أتمكن من قراءة الرقم الوطني من الهوية بوضوح. يرجى إرسال صورة أخرى للوجه الأمامي للهوية الشخصية تكون أكثر وضوحاً:",
|
||||
"تم استخراج الرقم الوطني بنجاح ✅.\nالآن، يرجى إرسال صورة **الوجه الخلفي للهوية الشخصية**:",
|
||||
"id_back"
|
||||
);
|
||||
|
||||
case 'id_back':
|
||||
return $this->processOcrStep(
|
||||
$step,
|
||||
$messageData,
|
||||
$context,
|
||||
"id_back_sy",
|
||||
"gender",
|
||||
"عذراً كابتن، لم أتمكن من قراءة بيانات الوجه الخلفي للهوية بوضوح. يرجى إرسال صورة أخرى للوجه الخلفي للهوية الشخصية:",
|
||||
"تم استخراج البيانات بنجاح ✅.\nيرجى إرسال صورة **الوجه الأمامي لرخصة القيادة**:",
|
||||
"driving_license_front"
|
||||
);
|
||||
|
||||
case 'driving_license_front':
|
||||
return $this->processOcrStep(
|
||||
$step,
|
||||
$messageData,
|
||||
$context,
|
||||
"driving_license_sy_front",
|
||||
"national_number",
|
||||
"عذراً كابتن، لم أتمكن من قراءة رخصة القيادة بوضوح. يرجى إرسال صورة أخرى واضحة للوجه الأمامي لرخصة القيادة:",
|
||||
"تم استخراج بيانات رخصة القيادة بنجاح ✅.\nيرجى إرسال صورة **الوجه الخلفي لرخصة القيادة**:",
|
||||
"driving_license_back"
|
||||
);
|
||||
|
||||
case 'driving_license_back':
|
||||
return $this->processOcrStep(
|
||||
$step,
|
||||
$messageData,
|
||||
$context,
|
||||
"driving_license_sy_back",
|
||||
"license_number",
|
||||
"عذراً كابتن، لم أتمكن من قراءة الوجه الخلفي لرخصة القيادة بوضوح. يرجى إعادة إرسال الصورة بشكل أكثر وضوحاً:",
|
||||
"تم استخراج البيانات بنجاح ✅.\nيرجى إرسال صورة **الوجه الأمامي لرخصة السيارة (الرخصة البرتقالية)**:",
|
||||
"vehicle_license_front"
|
||||
);
|
||||
|
||||
case 'vehicle_license_front':
|
||||
return $this->processOcrStep(
|
||||
$step,
|
||||
$messageData,
|
||||
$context,
|
||||
"vehicle_license_sy_front",
|
||||
"car_plate",
|
||||
"عذراً كابتن، لم أتمكن من قراءة رقم لوحة السيارة بوضوح. يرجى إرسال صورة واضحة للوجه الأمامي لرخصة السيارة:",
|
||||
"تم استخراج رقم اللوحة بنجاح ✅.\nيرجى إرسال صورة **الوجه الخلفي لرخصة السيارة (الرخصة البرتقالية)**:",
|
||||
"vehicle_license_back"
|
||||
);
|
||||
|
||||
case 'vehicle_license_back':
|
||||
return $this->processOcrStep(
|
||||
$step,
|
||||
$messageData,
|
||||
$context,
|
||||
"vehicle_license_sy_back",
|
||||
"chassis",
|
||||
"عذراً كابتن، لم أتمكن من قراءة مواصفات السيارة بوضوح. يرجى إرسال صورة واضحة للوجه الخلفي لرخصة السيارة:",
|
||||
"تم استخراج مواصفات السيارة بنجاح ✅.\nيرجى إرسال صورة **وثيقة غير محكوم (لا حكم عليه)**:",
|
||||
"criminal_record"
|
||||
);
|
||||
|
||||
case 'criminal_record':
|
||||
if (empty($messageData['image']) || empty($messageData['imageMimeType'])) {
|
||||
return new FlowResult("الرجاء إرسال صورة وثيقة غير محكوم (لا حكم عليه) للاستمرار:", "criminal_record");
|
||||
}
|
||||
|
||||
// Save non-OCR criminal record image
|
||||
$imageUrl = $this->saveIncomingImage($step, $phone, $messageData);
|
||||
if (!$imageUrl) {
|
||||
return new FlowResult("عذراً، فشل حفظ الصورة. الرجاء إعادة إرسال صورة الوثيقة:", "criminal_record");
|
||||
}
|
||||
|
||||
// Securely save registration data to local database
|
||||
try {
|
||||
DriverOcrData::saveSecure([
|
||||
'company_id' => $companyId,
|
||||
'phone' => $phone,
|
||||
'name' => $context['name'],
|
||||
'id_front_url' => $context['id_front_url'] ?? null,
|
||||
'id_front_ocr' => $context['id_front_ocr'] ?? null,
|
||||
'id_back_url' => $context['id_back_url'] ?? null,
|
||||
'id_back_ocr' => $context['id_back_ocr'] ?? null,
|
||||
'driving_license_front_url' => $context['driving_license_front_url'] ?? null,
|
||||
'driving_license_front_ocr' => $context['driving_license_front_ocr'] ?? null,
|
||||
'driving_license_back_url' => $context['driving_license_back_url'] ?? null,
|
||||
'driving_license_back_ocr' => $context['driving_license_back_ocr'] ?? null,
|
||||
'vehicle_license_front_url' => $context['vehicle_license_front_url'] ?? null,
|
||||
'vehicle_license_front_ocr' => $context['vehicle_license_front_ocr'] ?? null,
|
||||
'vehicle_license_back_url' => $context['vehicle_license_back_url'] ?? null,
|
||||
'vehicle_license_back_ocr' => $context['vehicle_license_back_ocr'] ?? null,
|
||||
'criminal_record_url' => $imageUrl,
|
||||
'status' => 'ocr_completed'
|
||||
]);
|
||||
} catch (\Exception $dbEx) {
|
||||
error_log("[Registration Flow Error] DB Write Failed: " . $dbEx->getMessage());
|
||||
return new FlowResult("عذراً، حدث خطأ أثناء حفظ طلبك في قاعدة البيانات. يرجى المحاولة مرة أخرى لاحقاً.", "criminal_record");
|
||||
}
|
||||
|
||||
return new FlowResult(
|
||||
"شكراً لك كابتن، لقد تم استلام كافة المستندات والتحقق منها بنجاح. سيقوم فريق خدمة عملاء انطلق بمراجعة طلبك وتفعيل حسابك بأسرع وقت ممكن. يومك سعيد! 🚖",
|
||||
"finished",
|
||||
true
|
||||
);
|
||||
|
||||
default:
|
||||
return new FlowResult("خطأ في تحديد خطوة المسار.", "finished", true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process document upload and OCR extraction step
|
||||
*/
|
||||
private function processOcrStep(
|
||||
string $step,
|
||||
array $messageData,
|
||||
array &$context,
|
||||
string $promptKey,
|
||||
string $requiredJsonKey,
|
||||
string $failMessage,
|
||||
string $successMessage,
|
||||
string $nextStep
|
||||
): FlowResult {
|
||||
if (empty($messageData['image']) || empty($messageData['imageMimeType'])) {
|
||||
return new FlowResult("الرجاء إرسال الصورة المطلوبة للمتابعة:", $step);
|
||||
}
|
||||
|
||||
$imageUrl = $this->saveIncomingImage($step, $messageData['phone'], $messageData);
|
||||
if (!$imageUrl) {
|
||||
return new FlowResult("عذراً، فشل حفظ الصورة. الرجاء إعادة المحاولة وإرسال الصورة:", $step);
|
||||
}
|
||||
|
||||
$companyId = $context['company_id'] ?? 1;
|
||||
$rule = ChatbotRule::findActiveForRule($companyId);
|
||||
$apiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : getenv('GEMINI_API_KEY');
|
||||
|
||||
if (empty($apiKey)) {
|
||||
error_log("[DriverRegistrationFlow] Gemini API key not configured.");
|
||||
return new FlowResult("عذراً، عطل فني في خادم معالجة الصور بالذكاء الاصطناعي. يرجى المحاولة لاحقاً.", $step);
|
||||
}
|
||||
|
||||
$prompt = $this->prompts[$step] ?? '';
|
||||
$rawOcr = GeminiService::generateOcrFromImage($apiKey, $prompt, $messageData['image'], $messageData['imageMimeType']);
|
||||
|
||||
if (!$rawOcr) {
|
||||
error_log("[DriverRegistrationFlow] OCR response empty or model request failed.");
|
||||
return new FlowResult($failMessage, $step);
|
||||
}
|
||||
|
||||
$ocrData = json_decode($rawOcr, true);
|
||||
if (!$ocrData || (empty($ocrData[$requiredJsonKey]) && !array_key_exists($requiredJsonKey, $ocrData))) {
|
||||
error_log("[DriverRegistrationFlow] Missing required key '$requiredJsonKey' in OCR response: " . $rawOcr);
|
||||
return new FlowResult($failMessage, $step);
|
||||
}
|
||||
|
||||
// Save URL and OCR JSON string in the conversation context
|
||||
$context[$step . '_url'] = $imageUrl;
|
||||
$context[$step . '_ocr'] = $ocrData;
|
||||
|
||||
return new FlowResult($successMessage, $nextStep);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode base64 image data and save it to the public directory
|
||||
*/
|
||||
private function saveIncomingImage(string $step, string $phone, array $messageData): ?string
|
||||
{
|
||||
try {
|
||||
$extension = 'jpg';
|
||||
if (strpos($messageData['imageMimeType'], 'png') !== false) {
|
||||
$extension = 'png';
|
||||
}
|
||||
|
||||
$uniqueName = 'driver_' . $step . '_' . md5($phone . time()) . '.' . $extension;
|
||||
$uploadDir = __DIR__ . '/../../../../public/uploads/documents/';
|
||||
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0755, true);
|
||||
}
|
||||
|
||||
$uploadPath = $uploadDir . $uniqueName;
|
||||
$imgData = base64_decode($messageData['image']);
|
||||
|
||||
if (file_put_contents($uploadPath, $imgData) === false) {
|
||||
error_log("[DriverRegistrationFlow] Failed to write image file: " . $uploadPath);
|
||||
return null;
|
||||
}
|
||||
|
||||
return '/uploads/documents/' . $uniqueName;
|
||||
} catch (\Exception $e) {
|
||||
error_log("[DriverRegistrationFlow Exception] Failed to save image: " . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if user wants to postpone, and return hours_delay if so.
|
||||
*/
|
||||
private function detectPostponement(string $text, string $apiKey): ?array
|
||||
{
|
||||
if (empty($text)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Quick heuristic check: if the message is too long, or clearly doesn't contain postponement keywords, skip to save API costs
|
||||
$keywords = ['بعدين', 'بكرا', 'بكرة', 'بعد', 'شوي', 'ثانية', 'مشغول', 'المسا', 'الليل', 'تأجيل', 'وقت ثاني', 'تعبان', 'بعدين برسل', 'بعدين ببعت', 'ببعثهم بعدين', 'ببعتهم بعدين', 'بعدين بكمل'];
|
||||
$hasKeyword = false;
|
||||
foreach ($keywords as $kw) {
|
||||
if (mb_strpos($text, $kw) !== false) {
|
||||
$hasKeyword = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasKeyword && mb_strlen($text) > 100) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$systemPrompt = "You are an assistant that detects if a user wants to postpone, delay, or complete a registration flow later. Analyze the Arabic message.";
|
||||
$userMessage = <<<EOT
|
||||
Analyze the following Arabic message (often in Syrian dialect) to determine if the user wants to postpone/delay sending documents or complete the registration later.
|
||||
|
||||
Message: "{$text}"
|
||||
|
||||
Respond with ONLY a valid JSON object matching this schema:
|
||||
{
|
||||
"wants_postpone": true/false,
|
||||
"hours_delay": 12
|
||||
}
|
||||
Do not include any markdown, code blocks, or explanations.
|
||||
EOT;
|
||||
|
||||
$response = GeminiService::generateResponse($apiKey, $systemPrompt, $userMessage);
|
||||
if (empty($response)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Clean markdown block if present
|
||||
$response = trim(preg_replace('/```json|```/', '', $response));
|
||||
$data = json_decode($response, true);
|
||||
if (isset($data['wants_postpone']) && $data['wants_postpone'] === true) {
|
||||
return [
|
||||
'hours' => isset($data['hours_delay']) ? (int)$data['hours_delay'] : 12
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
144
backend/app/Models/DriverOcrData.php
Normal file
144
backend/app/Models/DriverOcrData.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Core\Security;
|
||||
|
||||
/**
|
||||
* DriverOcrData Model
|
||||
* Manages collected driver documents and OCR results with binary field encryption.
|
||||
*/
|
||||
class DriverOcrData extends BaseModel
|
||||
{
|
||||
protected static string $table = 'driver_ocr_data';
|
||||
|
||||
/**
|
||||
* Find decrypted record for a company and contact phone
|
||||
*/
|
||||
public static function findByPhone(int $companyId, string $phone): ?array
|
||||
{
|
||||
self::ensureTableExists();
|
||||
$hash = Security::blindIndex($phone);
|
||||
$record = Database::selectOne(
|
||||
"SELECT * FROM " . static::$table . " WHERE company_id = ? AND (phone_hash = ? OR phone = ?) LIMIT 1",
|
||||
[$companyId, $hash, $phone]
|
||||
);
|
||||
return self::decryptRecord($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save or update driver registration OCR state securely
|
||||
*/
|
||||
public static function saveSecure(array $data): string
|
||||
{
|
||||
self::ensureTableExists();
|
||||
|
||||
$data = self::encryptRecord($data);
|
||||
|
||||
$existing = Database::selectOne(
|
||||
"SELECT id FROM " . static::$table . " WHERE company_id = ? AND phone_hash = ? LIMIT 1",
|
||||
[$data['company_id'], $data['phone_hash']]
|
||||
);
|
||||
|
||||
if ($existing) {
|
||||
self::update($existing['id'], $data);
|
||||
return $existing['id'];
|
||||
} else {
|
||||
return self::create($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to encrypt sensitive fields in a record (simplified to plain text)
|
||||
*/
|
||||
public static function encryptRecord(array $data): array
|
||||
{
|
||||
if (!empty($data['phone'])) {
|
||||
$data['phone_hash'] = Security::blindIndex($data['phone']);
|
||||
}
|
||||
|
||||
$ocrFields = [
|
||||
'id_front_ocr', 'id_back_ocr',
|
||||
'driving_license_front_ocr', 'driving_license_back_ocr',
|
||||
'vehicle_license_front_ocr', 'vehicle_license_back_ocr'
|
||||
];
|
||||
|
||||
foreach ($ocrFields as $field) {
|
||||
if (isset($data[$field])) {
|
||||
if (is_array($data[$field])) {
|
||||
$jsonStr = json_encode($data[$field], JSON_UNESCAPED_UNICODE);
|
||||
} else {
|
||||
$jsonStr = $data[$field];
|
||||
}
|
||||
$data[$field] = $jsonStr ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to decrypt sensitive fields in a record (simplified to plain text)
|
||||
*/
|
||||
public static function decryptRecord(?array $record): ?array
|
||||
{
|
||||
if (!$record) return null;
|
||||
|
||||
$ocrFields = [
|
||||
'id_front_ocr', 'id_back_ocr',
|
||||
'driving_license_front_ocr', 'driving_license_back_ocr',
|
||||
'vehicle_license_front_ocr', 'vehicle_license_back_ocr'
|
||||
];
|
||||
|
||||
foreach ($ocrFields as $field) {
|
||||
if (!empty($record[$field])) {
|
||||
$record[$field] = json_decode($record[$field], true);
|
||||
}
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the driver_ocr_data table exists dynamically
|
||||
*/
|
||||
public static function ensureTableExists(): void
|
||||
{
|
||||
static $checked = false;
|
||||
if ($checked) return;
|
||||
|
||||
try {
|
||||
Database::execute("
|
||||
CREATE TABLE IF NOT EXISTS `driver_ocr_data` (
|
||||
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||
`company_id` INT NOT NULL,
|
||||
`phone` VARCHAR(512) NOT NULL,
|
||||
`phone_hash` VARCHAR(64) NOT NULL,
|
||||
`name` VARCHAR(512) NOT NULL,
|
||||
`id_front_url` VARCHAR(512) DEFAULT NULL,
|
||||
`id_front_ocr` TEXT DEFAULT NULL,
|
||||
`id_back_url` VARCHAR(512) DEFAULT NULL,
|
||||
`id_back_ocr` TEXT DEFAULT NULL,
|
||||
`driving_license_front_url` VARCHAR(512) DEFAULT NULL,
|
||||
`driving_license_front_ocr` TEXT DEFAULT NULL,
|
||||
`driving_license_back_url` VARCHAR(512) DEFAULT NULL,
|
||||
`driving_license_back_ocr` TEXT DEFAULT NULL,
|
||||
`vehicle_license_front_url` VARCHAR(512) DEFAULT NULL,
|
||||
`vehicle_license_front_ocr` TEXT DEFAULT NULL,
|
||||
`vehicle_license_back_url` VARCHAR(512) DEFAULT NULL,
|
||||
`vehicle_license_back_ocr` TEXT DEFAULT NULL,
|
||||
`criminal_record_url` VARCHAR(512) DEFAULT NULL,
|
||||
`status` VARCHAR(50) NOT NULL DEFAULT 'pending',
|
||||
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
UNIQUE KEY `unique_company_driver_phone` (`company_id`, `phone_hash`),
|
||||
FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
");
|
||||
$checked = true;
|
||||
} catch (\Exception $e) {
|
||||
error_log("Failed to ensure driver_ocr_data table: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
74
backend/app/Models/DriverReminder.php
Normal file
74
backend/app/Models/DriverReminder.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
|
||||
/**
|
||||
* DriverReminder Model
|
||||
* Manages driver registration reminders for follow-up notifications.
|
||||
*/
|
||||
class DriverReminder extends BaseModel
|
||||
{
|
||||
protected static string $table = 'driver_registration_reminders';
|
||||
|
||||
/**
|
||||
* Find active reminder by company and contact phone
|
||||
*/
|
||||
public static function findActive(int $companyId, string $phone): ?array
|
||||
{
|
||||
self::ensureTableExists();
|
||||
return Database::selectOne(
|
||||
"SELECT * FROM " . static::$table . " WHERE company_id = ? AND phone = ? AND status = 'pending' LIMIT 1",
|
||||
[$companyId, $phone]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save or update driver reminder state
|
||||
*/
|
||||
public static function saveReminder(array $data): string
|
||||
{
|
||||
self::ensureTableExists();
|
||||
|
||||
$existing = Database::selectOne(
|
||||
"SELECT id FROM " . static::$table . " WHERE company_id = ? AND phone = ? AND status = 'pending' LIMIT 1",
|
||||
[$data['company_id'], $data['phone']]
|
||||
);
|
||||
|
||||
if ($existing) {
|
||||
self::update($existing['id'], $data);
|
||||
return $existing['id'];
|
||||
} else {
|
||||
return self::create($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the driver_registration_reminders table exists dynamically
|
||||
*/
|
||||
public static function ensureTableExists(): void
|
||||
{
|
||||
static $checked = false;
|
||||
if ($checked) return;
|
||||
|
||||
try {
|
||||
Database::execute("
|
||||
CREATE TABLE IF NOT EXISTS `driver_registration_reminders` (
|
||||
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||
`company_id` INT NOT NULL,
|
||||
`phone` VARCHAR(50) NOT NULL,
|
||||
`scheduled_at` DATETIME NOT NULL,
|
||||
`postpone_count` INT DEFAULT 0,
|
||||
`status` VARCHAR(20) DEFAULT 'pending',
|
||||
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
");
|
||||
$checked = true;
|
||||
} catch (\Exception $e) {
|
||||
error_log("Failed to ensure driver_registration_reminders table: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,6 +155,59 @@ class GeminiService
|
||||
return $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transcribe incoming audio voice note to text using gemini-2.0-flash-lite
|
||||
*/
|
||||
public static function transcribeAudio(string $apiKey, string $audioBase64, string $mimeType): ?string
|
||||
{
|
||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=' . $apiKey;
|
||||
|
||||
// Clean mimeType if it contains codec info
|
||||
if (strpos($mimeType, ';') !== false) {
|
||||
$mimeType = trim(explode(';', $mimeType)[0]);
|
||||
}
|
||||
|
||||
$payload = json_encode([
|
||||
'contents' => [
|
||||
[
|
||||
'role' => 'user',
|
||||
'parts' => [
|
||||
[
|
||||
'inlineData' => [
|
||||
'mimeType' => $mimeType,
|
||||
'data' => $audioBase64
|
||||
]
|
||||
],
|
||||
[
|
||||
'text' => "Transcribe the following audio message to Arabic text. Output only the transcription, no translation, no commentary, no markdown, and no code blocks."
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$ch = curl_init($url);
|
||||
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'
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
error_log("[Gemini Audio Transcription Error] HTTP " . $httpCode . " | Response: " . $response);
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
return trim($data['candidates'][0]['content']['parts'][0]['text'] ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Call Gemini API with image inline data and system instruction to generate a response text
|
||||
*/
|
||||
@@ -213,6 +266,64 @@ class GeminiService
|
||||
return $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call Gemini API with image inline data and custom prompt to extract structured OCR data
|
||||
*/
|
||||
public static function generateOcrFromImage(string $apiKey, string $prompt, string $imageBase64, string $mimeType): ?string
|
||||
{
|
||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=' . $apiKey;
|
||||
|
||||
// Clean mimeType if it contains codec info
|
||||
if (strpos($mimeType, ';') !== false) {
|
||||
$mimeType = trim(explode(';', $mimeType)[0]);
|
||||
}
|
||||
|
||||
$payload = json_encode([
|
||||
'contents' => [
|
||||
[
|
||||
'role' => 'user',
|
||||
'parts' => [
|
||||
[
|
||||
'text' => $prompt
|
||||
],
|
||||
[
|
||||
'inlineData' => [
|
||||
'mimeType' => $mimeType,
|
||||
'data' => $imageBase64
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$ch = curl_init($url);
|
||||
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'
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 35); // 35 seconds timeout for image analysis
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
error_log("[Gemini OCR Image Response Error] HTTP " . $httpCode . " | Response: " . $response);
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
$textRaw = $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
|
||||
if ($textRaw) {
|
||||
// Clean markdown block if present
|
||||
$textRaw = trim(preg_replace('/```json|```/', '', $textRaw));
|
||||
}
|
||||
return $textRaw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call ElevenLabs API to generate a native audio response from text
|
||||
*/
|
||||
|
||||
@@ -1073,6 +1073,7 @@
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; flex-wrap: wrap; gap: 0.5rem;">
|
||||
<label class="form-label" x-text="chatbotSettings.trigger_type === 'gemini_ai' ? 'System Instruction Prompt' : 'Predefined Auto-Reply Message'" style="margin-bottom: 0;"></label>
|
||||
<div x-show="chatbotSettings.trigger_type === 'gemini_ai'" style="display: flex; gap: 0.35rem; flex-wrap: wrap; align-items: center;">
|
||||
<button type="button" class="btn btn-secondary" style="font-size: 0.75rem; padding: 0.25rem 0.5rem; width: auto;" @click="loadPromptTemplate('intaleq')">قالب خدمة عملاء انطلق (سوري)</button>
|
||||
<button type="button" class="btn btn-secondary" style="font-size: 0.75rem; padding: 0.25rem 0.5rem; width: auto;" @click="loadPromptTemplate('nabeh')">قالب تطبيق نبيه (سوري)</button>
|
||||
<button type="button" class="btn btn-secondary" style="font-size: 0.75rem; padding: 0.25rem 0.5rem; width: auto;" @click="loadPromptTemplate('store')">قالب متجر إلكتروني</button>
|
||||
<button type="button" class="btn btn-secondary" style="font-size: 0.75rem; padding: 0.25rem 0.5rem; width: auto;" @click="loadPromptTemplate('general')">قالب عام (إنجليزي)</button>
|
||||
@@ -2006,7 +2007,22 @@
|
||||
},
|
||||
|
||||
loadPromptTemplate(type) {
|
||||
if (type === 'nabeh') {
|
||||
if (type === 'intaleq') {
|
||||
this.chatbotSettings.ai_prompt = `أنت روبوت خدمة العملاء الخاص بشركة "انطلق" (Intaleq). مهمتك هي مساعدة المستخدمين والإجابة على استفساراتهم حول خدماتنا بلهجة سورية ودودة ومهنية للغاية.
|
||||
|
||||
التزم بالقواعد والتفاصيل التالية بدقة عند الرد:
|
||||
1. اسم الشركة: انطلق.
|
||||
2. ساعات العمل: يومياً من الساعة 11 صباحاً حتى 5 مساءً.
|
||||
3. طرق الدفع المتاحة: شام كاش، سيريتل، إم تي إن، وبطاقات البنك.
|
||||
4. طريقة التسجيل: يمكن للمستخدمين تحميل التطبيق من متجر جوجل بلاي، آبل ستور، أو عبر رابط مباشر. عملية التسجيل سهلة وسريعة.
|
||||
5. مميزات التطبيق:
|
||||
- عمولة التطبيق هي 10% فقط (وهي الأقل في السوق).
|
||||
- يوجد ذكاء اصطناعي متطور لتحليل المشاكل وحلها.
|
||||
- ضمان حقوق السائق والركاب بشكل كامل.
|
||||
- خدمة الطلب من أي مكان وبكل سهولة.
|
||||
6. تحدث دائماً باللهجة السورية اللطيفة والترحيبية والودية عند التحدث باللغة العربية (مثال: "ياهلا بك"، "كيف بقدر أساعدك اليوم؟").
|
||||
7. كوني مختصرة ومباشرة ومفيدة في إجاباتكِ وتجنبي الإطالة غير الضرورية.`;
|
||||
} else if (type === 'nabeh') {
|
||||
this.chatbotSettings.ai_prompt = `أنتِ "سارة"، موظفة خدمة العملاء الافتراضية الذكية والودودة لتطبيق "نبيه" (Nabeh).
|
||||
مهمتكِ هي مساعدة المستخدمين والإجابة على استفساراتهم بلطف وأدب بالمسائل التقنية والتجارية المتعلقة بالتطبيق.
|
||||
|
||||
|
||||
@@ -148,5 +148,237 @@ $router->post('/api/external/verify-payment', function ($request, $response) {
|
||||
});
|
||||
|
||||
|
||||
// REST API for Entaleq Driver OCR Details (GET)
|
||||
$router->get('/api/external/driver-ocr', function ($request, $response) {
|
||||
$apiKey = getenv('ENTALEQ_API_KEY');
|
||||
$incomingKey = $request->getHeader('x-api-key') ?? '';
|
||||
|
||||
if (empty($apiKey) || $incomingKey !== $apiKey) {
|
||||
$response->status(401)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Unauthorized'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$phone = $request->get('phone') ?? '';
|
||||
$companyId = $request->get('company_id') ?? '';
|
||||
|
||||
if (empty($phone)) {
|
||||
$response->status(400)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Missing phone parameter'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
\App\Models\DriverOcrData::ensureTableExists();
|
||||
$hash = \App\Core\Security::blindIndex($phone);
|
||||
|
||||
if ($companyId) {
|
||||
$records = \App\Core\Database::select(
|
||||
"SELECT * FROM driver_ocr_data WHERE company_id = ? AND phone_hash = ?",
|
||||
[$companyId, $hash]
|
||||
);
|
||||
} else {
|
||||
$records = \App\Core\Database::select(
|
||||
"SELECT * FROM driver_ocr_data WHERE phone_hash = ?",
|
||||
[$hash]
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($records)) {
|
||||
$response->status(404)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'No driver records found for this phone number'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$decryptedRecords = [];
|
||||
$host = ($request->getHeader('x-forwarded-proto') ?: 'http') . '://' . ($request->getHeader('host') ?: 'localhost');
|
||||
|
||||
foreach ($records as $record) {
|
||||
$decrypted = \App\Models\DriverOcrData::decryptRecord($record);
|
||||
if ($decrypted) {
|
||||
// Include absolute paths for all document URLs
|
||||
$urlFields = [
|
||||
'id_front_url', 'id_back_url',
|
||||
'driving_license_front_url', 'driving_license_back_url',
|
||||
'vehicle_license_front_url', 'vehicle_license_back_url',
|
||||
'criminal_record_url'
|
||||
];
|
||||
foreach ($urlFields as $field) {
|
||||
if (!empty($decrypted[$field])) {
|
||||
$decrypted[$field . '_absolute'] = $host . $decrypted[$field];
|
||||
} else {
|
||||
$decrypted[$field . '_absolute'] = null;
|
||||
}
|
||||
}
|
||||
$decryptedRecords[] = $decrypted;
|
||||
}
|
||||
}
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $decryptedRecords
|
||||
]);
|
||||
});
|
||||
|
||||
// REST API to Mark Driver Registered (POST)
|
||||
$router->post('/api/external/register-driver', function ($request, $response) {
|
||||
$apiKey = getenv('ENTALEQ_API_KEY');
|
||||
$incomingKey = $request->getHeader('x-api-key') ?? '';
|
||||
|
||||
if (empty($apiKey) || $incomingKey !== $apiKey) {
|
||||
$response->status(401)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Unauthorized'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$phone = $request->get('phone') ?? '';
|
||||
$companyId = $request->get('company_id') ?? '';
|
||||
|
||||
if (empty($phone)) {
|
||||
$response->status(400)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Missing phone parameter'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
\App\Models\DriverOcrData::ensureTableExists();
|
||||
$hash = \App\Core\Security::blindIndex($phone);
|
||||
|
||||
if ($companyId) {
|
||||
$existing = \App\Core\Database::selectOne(
|
||||
"SELECT id FROM driver_ocr_data WHERE company_id = ? AND phone_hash = ? LIMIT 1",
|
||||
[$companyId, $hash]
|
||||
);
|
||||
} else {
|
||||
$existing = \App\Core\Database::selectOne(
|
||||
"SELECT id FROM driver_ocr_data WHERE phone_hash = ? LIMIT 1",
|
||||
[$hash]
|
||||
);
|
||||
}
|
||||
|
||||
if (!$existing) {
|
||||
$response->status(404)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Driver record not found'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
\App\Core\Database::execute(
|
||||
"UPDATE driver_ocr_data SET status = 'registered' WHERE id = ?",
|
||||
[$existing['id']]
|
||||
);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Driver status successfully updated to registered'
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
// Cron job endpoint to send pending driver registration reminders
|
||||
$router->get('/api/external/cron/send-reminders', function ($request, $response) {
|
||||
$apiKey = getenv('ENTALEQ_API_KEY');
|
||||
$incomingKey = $request->getHeader('x-api-key') ?? '';
|
||||
|
||||
if (empty($apiKey) || $incomingKey !== $apiKey) {
|
||||
$response->status(401)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Unauthorized'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
\App\Models\DriverReminder::ensureTableExists();
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$reminders = \App\Core\Database::select(
|
||||
"SELECT * FROM driver_registration_reminders WHERE status = 'pending' AND scheduled_at <= ?",
|
||||
[$now]
|
||||
);
|
||||
|
||||
$processed = 0;
|
||||
foreach ($reminders as $reminder) {
|
||||
$companyId = $reminder['company_id'];
|
||||
$phone = $reminder['phone'];
|
||||
|
||||
// Get company chatbot rule details
|
||||
$rule = \App\Models\ChatbotRule::findActiveForRule($companyId);
|
||||
$geminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : getenv('GEMINI_API_KEY');
|
||||
$elApiKey = ($rule && !empty($rule['elevenlabs_api_key'])) ? $rule['elevenlabs_api_key'] : getenv('ELEVENLABS_API_KEY');
|
||||
$elVoiceId = ($rule && !empty($rule['elevenlabs_voice_id'])) ? $rule['elevenlabs_voice_id'] : getenv('ELEVENLABS_VOICE_ID');
|
||||
|
||||
// Fetch company WhatsApp session
|
||||
$session = \App\Models\WhatsAppSession::findByCompany($companyId);
|
||||
if (!$session || $session['status'] !== 'connected') {
|
||||
error_log("Cron Reminder: WhatsApp session not connected for company {$companyId}");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Try to get driver name from active conversation flow state
|
||||
$driverName = '';
|
||||
$state = \App\Models\ConversationState::findActive($companyId, $phone);
|
||||
if ($state) {
|
||||
$ctx = json_decode($state['context_data'] ?: '{}', true);
|
||||
$driverName = $ctx['name'] ?? '';
|
||||
}
|
||||
|
||||
$nameStr = $driverName ? " كابتن " . $driverName : " كابتن";
|
||||
$reminderMsg = "أهلاً بك{$nameStr}، حابين نذكرك تكمل خطوات تسجيلك لتنضم لعائلة انطلق 🚖. بقية الأوراق كتير مهمة لنفعل حسابك ونبدأ سوا. بانتظار إرسالها!";
|
||||
|
||||
// Generate Audio voice note if key is present
|
||||
$audioData = null;
|
||||
if (!empty($geminiKey)) {
|
||||
$audioData = \App\Services\GeminiService::generateAudioResponse(
|
||||
$geminiKey,
|
||||
"أنت روبوت خدمة العملاء لشركة انطلق، تتحدث باللهجة السورية الودودة.",
|
||||
$reminderMsg,
|
||||
'Puck',
|
||||
$elApiKey ?: null,
|
||||
$elVoiceId ?: null
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Send the text reminder
|
||||
\App\Core\Flows\ConversationFlowEngine::sendReply($session, $phone, $reminderMsg);
|
||||
|
||||
// 2. Send the voice note reminder if generated successfully
|
||||
if ($audioData && !empty($audioData['audio'])) {
|
||||
\App\Core\Flows\ConversationFlowEngine::sendReply(
|
||||
$session,
|
||||
$phone,
|
||||
'',
|
||||
null,
|
||||
$audioData['audio'],
|
||||
$audioData['mimeType'] ?? 'audio/mp4'
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Mark the reminder as sent
|
||||
\App\Models\DriverReminder::update($reminder['id'], [
|
||||
'status' => 'sent'
|
||||
]);
|
||||
$processed++;
|
||||
} catch (\Exception $ex) {
|
||||
error_log("Failed to process reminder ID {$reminder['id']}: " . $ex->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'processed_count' => $processed,
|
||||
'total_pending_due' => count($reminders)
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
// 4. Dispatch the request
|
||||
$router->dispatch($request, $response);
|
||||
|
||||
139
backend/public/test_simulation.php
Normal file
139
backend/public/test_simulation.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* Driver Registration & Postponement Flow Simulation Script
|
||||
* Run this on the server: php backend/public/test_simulation.php
|
||||
*/
|
||||
|
||||
require_once dirname(__DIR__) . '/app/bootstrap.php';
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Models\ConversationState;
|
||||
use App\Models\DriverReminder;
|
||||
use App\Core\Flows\ConversationFlowEngine;
|
||||
use App\Models\WhatsAppSession;
|
||||
|
||||
echo "=== Starting Driver Registration & Reminder Flow Simulation ===\n\n";
|
||||
|
||||
$companyId = 1;
|
||||
$phone = "963999999999"; // Test Phone Number
|
||||
|
||||
// Setup mock session
|
||||
$session = WhatsAppSession::findOrCreate($companyId);
|
||||
// Ensure session is marked connected for simulation to go through sendReply
|
||||
WhatsAppSession::updateState($session['id'], [
|
||||
'phone' => $phone,
|
||||
'status' => 'connected'
|
||||
]);
|
||||
// Refresh session data
|
||||
$session = WhatsAppSession::findByCompany($companyId);
|
||||
|
||||
echo "1. Cleaning up existing test states/reminders...\n";
|
||||
Database::execute("DELETE FROM conversation_states WHERE contact_phone = ?", [$phone]);
|
||||
Database::execute("DELETE FROM driver_registration_reminders WHERE phone = ?", [$phone]);
|
||||
Database::execute("DELETE FROM driver_ocr_data WHERE phone = ?", [$phone]);
|
||||
|
||||
echo "2. Initializing conversation to 'id_front' state...\n";
|
||||
ConversationState::saveState([
|
||||
'company_id' => $companyId,
|
||||
'contact_phone' => $phone,
|
||||
'flow_name' => 'driver_registration_flow',
|
||||
'current_step' => 'id_front',
|
||||
'context_data' => json_encode(['name' => 'جميل الشام'], JSON_UNESCAPED_UNICODE),
|
||||
'expires_at' => date('Y-m-d H:i:s', strtotime('+1 hour'))
|
||||
]);
|
||||
|
||||
echo "3. Simulating user postponement request: 'بكرة المسا ببعتلك الهوية الشخصية'...\n";
|
||||
$msgData = [
|
||||
'phone' => $phone,
|
||||
'body' => 'بكرة المسا ببعتلك الهوية الشخصية'
|
||||
];
|
||||
|
||||
$handled = ConversationFlowEngine::processMessage($session, $msgData);
|
||||
echo " Message processed by flow engine: " . ($handled ? "YES" : "NO") . "\n";
|
||||
|
||||
// Fetch the new state and scheduled reminders
|
||||
$state = ConversationState::findActive($companyId, $phone);
|
||||
echo " New Flow Step: " . ($state ? $state['current_step'] : 'None (finished)') . "\n";
|
||||
echo " Context: " . ($state ? $state['context_data'] : 'None') . "\n";
|
||||
|
||||
$reminders = Database::select("SELECT * FROM driver_registration_reminders WHERE phone = ?", [$phone]);
|
||||
echo " Scheduled Reminders Count: " . count($reminders) . "\n";
|
||||
if (!empty($reminders)) {
|
||||
foreach ($reminders as $r) {
|
||||
echo " - ID: {$r['id']}, Scheduled At: {$r['scheduled_at']}, Postpone Count: {$r['postpone_count']}, Status: {$r['status']}\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n4. Testing Cron Scheduler (Simulate reminder execution by setting scheduled_at to the past)...\n";
|
||||
if (!empty($reminders)) {
|
||||
Database::execute("UPDATE driver_registration_reminders SET scheduled_at = ? WHERE id = ?", [
|
||||
date('Y-m-d H:i:s', strtotime('-1 minute')),
|
||||
$reminders[0]['id']
|
||||
]);
|
||||
|
||||
echo " Triggering cron process via internal endpoint request simulation...\n";
|
||||
// We call the main cron logic directly by querying and executing the reminder
|
||||
$dueReminders = Database::select(
|
||||
"SELECT * FROM driver_registration_reminders WHERE status = 'pending' AND scheduled_at <= ?",
|
||||
[date('Y-m-d H:i:s')]
|
||||
);
|
||||
|
||||
echo " Due Reminders found: " . count($dueReminders) . "\n";
|
||||
foreach ($dueReminders as $reminder) {
|
||||
$rule = \App\Models\ChatbotRule::findActiveForRule($companyId);
|
||||
$geminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : getenv('GEMINI_API_KEY');
|
||||
$elApiKey = ($rule && !empty($rule['elevenlabs_api_key'])) ? $rule['elevenlabs_api_key'] : getenv('ELEVENLABS_API_KEY');
|
||||
$elVoiceId = ($rule && !empty($rule['elevenlabs_voice_id'])) ? $rule['elevenlabs_voice_id'] : getenv('ELEVENLABS_VOICE_ID');
|
||||
|
||||
$driverName = 'جميل الشام';
|
||||
$nameStr = $driverName ? " كابتن " . $driverName : " كابتن";
|
||||
$reminderMsg = "أهلاً بك{$nameStr}، حابين نذكرك تكمل خطوات تسجيلك لتنضم لعائلة انطلق 🚖. بقية الأوراق كتير مهمة لنفعل حسابك ونبدأ سوا. بانتظار إرسالها!";
|
||||
|
||||
echo " Generating reminder Audio via TTS...\n";
|
||||
$audioData = null;
|
||||
if (!empty($geminiKey)) {
|
||||
$audioData = \App\Services\GeminiService::generateAudioResponse(
|
||||
$geminiKey,
|
||||
"أنت روبوت خدمة العملاء لشركة انطلق، تتحدث باللهجة السورية الودودة.",
|
||||
$reminderMsg,
|
||||
'Puck',
|
||||
$elApiKey ?: null,
|
||||
$elVoiceId ?: null
|
||||
);
|
||||
}
|
||||
|
||||
echo " Sending outbound simulated message...\n";
|
||||
ConversationFlowEngine::sendReply($session, $phone, $reminderMsg);
|
||||
|
||||
if ($audioData && !empty($audioData['audio'])) {
|
||||
echo " Audio voice note generated successfully (Length: " . strlen($audioData['audio']) . " bytes). Sending...\n";
|
||||
ConversationFlowEngine::sendReply(
|
||||
$session,
|
||||
$phone,
|
||||
'',
|
||||
null,
|
||||
$audioData['audio'],
|
||||
$audioData['mimeType'] ?? 'audio/mp4'
|
||||
);
|
||||
} else {
|
||||
echo " Audio voice note generation skipped or failed (Gemini Key may be missing or empty).\n";
|
||||
}
|
||||
|
||||
DriverReminder::update($reminder['id'], ['status' => 'sent']);
|
||||
echo " Reminder ID {$reminder['id']} status set to 'sent'.\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n5. Simulating user resuming registration (sending message 'هلا يا كابتن')...\n";
|
||||
$msgData = [
|
||||
'phone' => $phone,
|
||||
'body' => 'هلا يا كابتن'
|
||||
];
|
||||
|
||||
$handled = ConversationFlowEngine::processMessage($session, $msgData);
|
||||
echo " Resumed Message handled: " . ($handled ? "YES" : "NO") . "\n";
|
||||
|
||||
$state = ConversationState::findActive($companyId, $phone);
|
||||
echo " Resumed Flow Step: " . ($state ? $state['current_step'] : 'None') . "\n";
|
||||
|
||||
echo "\n=== Simulation Finished ===\n";
|
||||
Reference in New Issue
Block a user