Update: 2026-08-08 17:35:50

This commit is contained in:
Hamza-Ayed
2026-08-08 17:35:50 +03:00
parent 8c9f729ecd
commit e63f990aea
18 changed files with 261 additions and 75 deletions
+40 -1
View File
@@ -371,9 +371,48 @@ class SiroGeminiService {
return null;
}
$data = json_decode($response, true);
$text = $data['candidates'][0]['content']['parts'][0]['text'] ?? '';
return trim(preg_replace('/```html|```/', '', $text));
}
/**
* Answers a driver support inquiry acting as Siro's smart assistant.
*/
public function answerSupportInquiry(
string $question,
array $driverContext,
string $model = 'gemini-flash-lite-latest'
): ?string {
if (!$this->apiKey) return null;
$contextJson = json_encode($driverContext, JSON_UNESCAPED_UNICODE);
$prompt = "
أنت مساعد الدعم الفني الذكي لتطبيق 'سيرو' (Siro) لنقل الركاب.
وظيفتك مساعدة السائقين (الكباتن) والإجابة على استفساراتهم باحترافية، ودقة، وبلهجة ودية ومهذبة جداً.
بيانات السائق الحالية (سياق مهم للإجابة على سؤاله):
$contextJson
سؤال السائق:
\"$question\"
المطلوب منك:
1. الإجابة مباشرة على سؤال السائق بناءً على السياق المعطى إذا كان يخص رحلاته أو رصيده المذكور في السياق.
2. إذا سأل عن إجراءات عامة، أجب كخبير في سيرو.
3. اجعل الإجابة ودية وواضحة جداً وقصيرة.
4. الرد يكون باللغة العربية حصراً.
5. لا تذكر تفاصيل تقنية (مثل json أو database)، بل تحدث كإنسان يقدم الدعم.
";
$prompt .= "\n\nالرجاء إرجاع الإجابة بصيغة JSON فقط كالتالي:\n{\n \"reply\": \"نص الإجابة هنا\"\n}";
$response = $this->callGemini($prompt, $model);
if ($response && isset($response['reply'])) {
return $response['reply'];
}
return null;
}
}
}
+109
View File
@@ -0,0 +1,109 @@
<?php
require_once __DIR__ . '/../../connect.php';
require_once __DIR__ . '/../../core/Services/SiroGeminiService.php';
require_once __DIR__ . '/../../encrypt_decrypt.php';
// Variables from connect.php (JWT token)
global $user_id, $role, $con;
if (!$user_id) {
jsonError("Unauthorized");
exit;
}
$driverID = $user_id;
$helpQuestion = filterRequest("helpQuestion");
if (empty($helpQuestion)) {
jsonError("Missing parameters");
exit;
}
try {
global $encryptionHelper;
// 1. Fetch driver info (Decrypting names and phone)
$stmt = $con->prepare("SELECT `first_name`, `last_name`, `phone` FROM `driver` WHERE `id` = :id");
$stmt->bindParam(':id', $driverID);
$stmt->execute();
$driverRow = $stmt->fetch(PDO::FETCH_ASSOC);
$driverInfo = [];
if ($driverRow) {
$driverInfo = [
'first_name' => $driverRow['first_name'] ? $encryptionHelper->decryptData($driverRow['first_name']) : '',
'last_name' => $driverRow['last_name'] ? $encryptionHelper->decryptData($driverRow['last_name']) : '',
'phone' => $driverRow['phone'] ? $encryptionHelper->decryptData($driverRow['phone']) : '',
'user_type' => $role
];
}
// 2. Fetch wallet balance using S2S
$walletServer = getenv('PAYMENT_SERVER_URL') ?: 'https://wallet.siromove.com';
$walletUrl = "$walletServer/v2/main/ride/driverWallet/get_s2s_wallet_dashboard.php";
$ch = curl_init($walletUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(["driverID" => $driverID]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'X-S2S-Api-Key: ' . getenv('S2S_SHARED_KEY')
]
]);
$s2sRes = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$totalWallet = 0.0;
if ($httpCode === 200 && $s2sRes) {
$resDecoded = json_decode($s2sRes, true);
if ($resDecoded && isset($resDecoded['status']) && $resDecoded['status'] === 'success') {
$totalWallet = (float)($resDecoded['message']['totalWallet'] ?? 0.0);
}
}
$walletInfo = ['balance' => $totalWallet];
// 3. Fetch recent trips (Correct table is `ride` and correct columns)
$recentTrips = [];
try {
$stmt3 = $con->prepare("SELECT `id`, `date`, `time`, `status`, `price`, `paymentMethod` FROM `ride` WHERE `driver_id` = :id ORDER BY `id` DESC LIMIT 3");
$stmt3->bindParam(':id', $driverID);
$stmt3->execute();
$recentTrips = $stmt3->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (PDOException $e) {
// Ignored
}
$contextData = [
'driver_info' => $driverInfo,
'wallet_balance' => $walletInfo,
'recent_trips' => $recentTrips,
];
// 4. Call Gemini
$geminiService = new SiroGeminiService();
$aiReply = $geminiService->answerSupportInquiry($helpQuestion, $contextData);
if (!$aiReply) {
$aiReply = "مرحباً كابتن، استلمنا استفسارك ولكن المساعد الذكي غير متاح حالياً. سيقوم فريق الدعم بمراجعة رسالتك قريباً.";
}
// 5. Insert into helpCenter
$sql = "INSERT INTO `helpCenter` (`driverID`, `helpQuestion`, `replay`) VALUES (:driverID, :helpQuestion, :replay)";
$stmt4 = $con->prepare($sql);
$stmt4->bindParam(':driverID', $driverID);
$stmt4->bindParam(':helpQuestion', $helpQuestion);
$stmt4->bindParam(':replay', $aiReply);
$stmt4->execute();
if ($stmt4->rowCount() > 0) {
jsonSuccess(null, "Help question saved and answered");
} else {
jsonError("Failed to save help question");
}
} catch (Exception $e) {
jsonError("An error occurred while processing your request.");
}
?>