Update: 2026-05-07 03:50:16

This commit is contained in:
Hamza-Ayed
2026-05-07 03:50:16 +03:00
parent 209f721cd6
commit bd7164ed23
9 changed files with 464 additions and 146 deletions

View File

@@ -54,7 +54,7 @@ $systemPrompt = <<<PROMPT
PROMPT;
$payload = [
'model' => 'grok-1', // Update to the correct Grok model when available
'model' => 'grok-beta', // Update to current xAI model name
'messages' => [
['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $text]
@@ -65,37 +65,42 @@ $payload = [
$url = "https://api.x.ai/v1/chat/completions";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
]
]);
try {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
]
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($httpCode !== 200) {
error_log("Grok Error: $response | $error");
json_error('فشل في تحليل الأمر بواسطة Grok', 500);
if ($httpCode !== 200) {
error_log("Grok Error: $response | $error");
json_error('فشل في تحليل الأمر بواسطة Grok. تأكد من صحة مفتاح API وصلاحية الحساب.', 500);
}
$respData = json_decode($response, true);
if (!isset($respData['choices'][0]['message']['content'])) {
json_error('رد غير متوقع من Grok AI', 500);
}
$jsonText = $respData['choices'][0]['message']['content'];
$parsed = json_decode($jsonText, true);
if (!$parsed) {
json_error('فشل في تحليل الرد كـ JSON', 500);
}
json_success($parsed, 'تم تحليل الأمر بواسطة Grok');
} catch (\Throwable $e) {
error_log("Voice Intent Error: " . $e->getMessage());
json_error('حدث خطأ فني أثناء تحليل الأمر صوتياً.', 500);
}
$respData = json_decode($response, true);
if (!isset($respData['choices'][0]['message']['content'])) {
json_error('رد غير متوقع من Grok AI', 500);
}
$jsonText = $respData['choices'][0]['message']['content'];
$parsed = json_decode($jsonText, true);
if (!$parsed) {
json_error('فشل في تحليل الرد كـ JSON', 500);
}
json_success($parsed, 'تم تحليل الأمر بواسطة Grok');