Update: 2026-08-02 22:51:18

This commit is contained in:
Hamza-Ayed
2026-08-02 22:51:18 +03:00
parent c9af364cbc
commit 2c962bf80a
39 changed files with 627 additions and 191 deletions
+42 -13
View File
@@ -23,7 +23,15 @@ if (!$SECRET_KEY) {
echo json_encode(['status' => 'failure', 'message' => 'Server configuration error: BOT_SECRET_KEY not set in environment']);
exit;
}
$ALLOWED_DEVICES = ['SHAM_CASH_BOT_01', 'PRICE_SCRAPER_BOT_01', '77a9528112ef7c2a'];
// أجهزة البوت المسموح بها — تُضبط عبر BOT_ALLOWED_DEVICES (مفصولة بفواصل)
// حتى لا يتطلب ضم جهاز جديد تعديل الكود ونشره.
$ALLOWED_DEVICES = array_values(array_filter(array_map(
'trim',
explode(',', (string)(getenv('BOT_ALLOWED_DEVICES') ?: ''))
)));
if (!$ALLOWED_DEVICES) {
$ALLOWED_DEVICES = ['SHAM_CASH_BOT_01', 'PRICE_SCRAPER_BOT_01', '77a9528112ef7c2a'];
}
$method = $_SERVER['REQUEST_METHOD'];
@@ -43,9 +51,12 @@ if ($method === 'GET') {
// ---------------------------------------------------------
// GET: Fetch Pending Task for the Bot
// ---------------------------------------------------------
$device_id = filterRequest('device_id');
$ts = filterRequest('ts');
$sig = filterRequest('sig');
// ملاحظة: filterRequest() تقرأ من POST أو JSON body فقط ولا تلمس $_GET
// (backend/core/helpers.php) — والبوت يرسل بارامترات التوقيع في الـ query
// string، لذا تُقرأ هنا من $_GET مباشرة.
$device_id = isset($_GET['device_id']) ? trim((string)$_GET['device_id']) : null;
$ts = isset($_GET['ts']) ? (int)$_GET['ts'] : null;
$sig = isset($_GET['sig']) ? trim((string)$_GET['sig']) : null;
if (!$device_id || !$ts || !$sig) {
jsonError("Missing security parameters");
@@ -64,6 +75,14 @@ if ($method === 'GET') {
// Check Redis Queue for tasks
// Queue Name: queue:bot:tasks (Using Main Redis)
// bootstrap.php يترك $redis = null إذا فشل الاتصال — بدون هذا الفحص
// كان الطلب ينهار بخطأ 500 غامض بدل رسالة واضحة.
if (!$redis) {
http_response_code(503);
echo json_encode(['status' => 'failure', 'message' => 'Task queue unavailable']);
exit;
}
$taskJson = $redis->rpop('queue:bot:tasks');
if ($taskJson) {
@@ -144,18 +163,28 @@ if ($method === 'GET') {
}
// ------------------------------------------------------
// العملة تتبع الدولة بدل تثبيتها على JOD — البوت يعمل على أكثر من سوق
$currencyByCountry = ['JO' => 'JOD', 'SY' => 'SYP', 'EG' => 'EGP'];
$currency = $currencyByCountry[strtoupper($country_code)] ?? 'JOD';
// 1. Save to MySQL
$stmt = $con->prepare("
INSERT INTO scraped_competitor_prices
(task_id, app_name, competitor_name, start_location, end_location, start_lat, start_lng, end_lat, end_lng, price_amount, price_per_km, distance_km, duration_min, currency, country_code)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$start_loc = "Lat: $start_lat, Lng: $start_lng";
$end_loc = "Lat: $end_lat, Lng: $end_lng";
$stmt->execute([$task_id, $app_name, $app_name, $start_loc, $end_loc, (string)$start_lat, (string)$start_lng, (string)$end_lat, (string)$end_lng, $price, $pricePerKm, $distance_km, $duration_min, 'JOD', $country_code]);
try {
$stmt = $con->prepare("
INSERT INTO scraped_competitor_prices
(task_id, app_name, competitor_name, start_location, end_location, start_lat, start_lng, end_lat, end_lng, price_amount, price_per_km, distance_km, duration_min, currency, country_code)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$start_loc = "Lat: $start_lat, Lng: $start_lng";
$end_loc = "Lat: $end_lat, Lng: $end_lng";
$stmt->execute([$task_id, $app_name, $app_name, $start_loc, $end_loc, (string)$start_lat, (string)$start_lng, (string)$end_lat, (string)$end_lng, $price, $pricePerKm, $distance_km, $duration_min, $currency, $country_code]);
} catch (PDOException $e) {
// لا نُسقط الطلب: نسجّل الخطأ ونكمل إلى Redis حتى لا تضيع العيّنة
// من محرّك التسعير بسبب عطل في الكتابة إلى MySQL.
error_log("[Bot Worker] MySQL insert failed for task $task_id: " . $e->getMessage());
}
// 2. Save to Redis (Calculate Price Per KM)
if ($distance_km > 0 && $price > 0) {
if ($redis && $distance_km > 0 && $price > 0) {
// Store in Redis (Main) to be used by Pricing Engine
// Store recent 50 prices for the app
$redis->lpush("competitor:price_history:$app_name", $pricePerKm);