<?php
// إعدادات البوت
$botToken = '7964478354:AAF6XoFHJvGT0KafeHOc3LMDwDTo1lxNvNA';
$apiUrl = "https://api.telegram.org/bot$botToken/";
$aiApiUrl = 'http://pass-gpt.nowtechai.com/api/v1/pass';
// استقبال التحديث من Telegram
$update = json_decode(file_get_contents("php://input"), true);
if (!$update || !isset($update['message']['text'])) {
exit;
}
$chatId = $update['message']['chat']['id'];
$userMessage = $update['message']['text'];
// تجهيز البيانات لإرسالها إلى API الذكاء الاصطناعي
$payload = json_encode([
"contents" => [
["role" => "system", "content" => "أجب باختصار وباللغة العربية."],
["role" => "user", "content" => $userMessage]
]
]);
$headers = [
"Content-Type: application/json",
"User-Agent: Ktor client",
"Accept: text/event-stream"
];
// إرسال الطلب إلى API باستخدام cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $aiApiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// تحليل الرد المتدفق وتجميع المحتوى
$lines = explode("\n", $response);
$finalReply = "";
foreach ($lines as $line) {
if (strpos($line, "data:") === 0) {
$jsonPart = trim(substr($line, 5));
$parsed = json_decode($jsonPart, true);
if (isset($parsed['status']) && $parsed['status'] === 'stream') {
$finalReply .= $parsed['content'];
}
}
}
$finalReply = trim($finalReply);
if ($finalReply === '') {
$finalReply = "لم يتم العثور على رد من API.";
}
// إرسال الرد للمستخدم
file_get_contents($apiUrl . "sendMessage?chat_id=$chatId&text=" . urlencode($finalReply));
?>