diff --git a/backend/core/Services/SiroGeminiService.php b/backend/core/Services/SiroGeminiService.php index 3f2fdd76..019a6a19 100644 --- a/backend/core/Services/SiroGeminiService.php +++ b/backend/core/Services/SiroGeminiService.php @@ -133,7 +133,7 @@ class SiroGeminiService { */ public function analyzeCompetitorFormulas( array $formulas, - string $model = 'gemini-1.5-flash' + string $model = 'gemini-flash-lite-latest' ): ?array { if (!$this->apiKey) return null; @@ -249,5 +249,58 @@ class SiroGeminiService { error_log("[SiroGeminiService] Exception: " . $e->getMessage()); return null; } + public function evaluatePostsForReporting(array $posts, string $model = 'gemini-flash-lite-latest'): ?string { + if (!$this->apiKey || empty($posts)) return null; + + $postsJson = json_encode($posts, JSON_UNESCAPED_UNICODE); + + $prompt = " + أنت محلل بيانات استخباراتية للسوق لتطبيق 'سيرو' لنقل الركاب. + إليك مجموعة من المنشورات والتعليقات التي جمعها الروبوت الخاص بنا من مجموعات فيسبوك اليوم: + $postsJson + + المطلوب منك: + 1. قراءة جميع هذه المنشورات واستخراج أي شكاوى، أسئلة، أو نقاشات تتعلق بـ (تطبيقات النقل الذكي، أسعار المحروقات، باقات الإنترنت، مشاكل السيارات). + 2. تلخيص أهم هذه النقاشات في تقرير قصير ومفيد (News Report). + 3. تجاهل المنشورات العشوائية أو الشخصية التي لا تفيد السوق. + + قم بصياغة تقريرك بتنسيق HTML مرتب وجاهز للعرض (بدون علامات ```html)، واستخدم
لا توجد بيانات مفيدة في هذه الدفعة.
+ "; + + $url = $this->baseUrl . "{$model}:generateContent?key={$this->apiKey}"; + + $postData = [ + 'contents' => [ + [ + 'parts' => [ + ['text' => $prompt] + ] + ] + ], + 'generationConfig' => [ + 'temperature' => 0.5, + 'maxOutputTokens' => 1500 + ] + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); + curl_setopt($ch, CURLOPT_TIMEOUT, 60); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($httpCode === 200) { + $data = json_decode($response, true); + $text = $data['candidates'][0]['content']['parts'][0]['text'] ?? ''; + return trim(preg_replace('/```html|```/', '', $text)); + } + + return null; } } diff --git a/backend/marketing_engine/cron_insert_task.php b/backend/marketing_engine/cron_insert_task.php new file mode 100644 index 00000000..1ba9e9fb --- /dev/null +++ b/backend/marketing_engine/cron_insert_task.php @@ -0,0 +1,9 @@ +prepare("INSERT INTO marketing_tasks (platform, type, status) VALUES ('facebook', 'autonomous_scroll_and_reply', 'pending')"); +$stmt->execute(); + +echo "Task inserted successfully at " . date('Y-m-d H:i:s') . "\n"; diff --git a/backend/marketing_engine/index.php b/backend/marketing_engine/index.php index 206cfce1..374f16f3 100644 --- a/backend/marketing_engine/index.php +++ b/backend/marketing_engine/index.php @@ -6,29 +6,26 @@ require_once __DIR__ . '/../core/bootstrap.php'; require_once __DIR__ . '/../functions.php'; +require_once __DIR__ . '/../core/Services/SiroGeminiService.php'; header('Content-Type: application/json'); // Simple bot authentication $headers = getallheaders(); $botToken = $headers['X-Bot-Token'] ?? ''; -if ($botToken !== 'YOUR_SECRET_BOT_TOKEN') { - // http_response_code(401); - // exit(json_encode(['status' => 'error', 'message' => 'Unauthorized'])); -} +// if ($botToken !== 'YOUR_SECRET_BOT_TOKEN') { +// http_response_code(401); +// exit(json_encode(['status' => 'error', 'message' => 'Unauthorized'])); +// } $action = $_GET['action'] ?? ''; -$platform = $_GET['platform'] ?? 'tiktok'; // Default to tiktok for new platform testing +$platform = $_GET['platform'] ?? 'facebook'; try { - // In a real scenario, you'd use a dedicated database. - // Here we use the main DB but with marketing_ prefixed tables. $con = Database::get('main'); switch ($action) { case 'get_task': - // 1. First, check if the bot needs a task - // We look for pending tasks for this platform $stmt = $con->prepare(" SELECT id, type, target_url, content_text, media_url FROM marketing_tasks @@ -39,10 +36,6 @@ try { $task = $stmt->fetch(PDO::FETCH_ASSOC); if ($task) { - // If the task requires a media file (like upload_video) - // The Android app will need to download it first using the media_url - - // Mark as in progress $update = $con->prepare("UPDATE marketing_tasks SET status = 'in_progress' WHERE id = ?"); $update->execute([$task['id']]); @@ -78,6 +71,34 @@ try { } break; + case 'evaluate_posts': + $input = json_decode(file_get_contents('php://input'), true); + $posts = $input['posts'] ?? []; + + if (empty($posts)) { + echo json_encode(['status' => 'success', 'data' => null]); + break; + } + + $gemini = new SiroGeminiService(); + $reportHtml = $gemini->evaluatePostsForReporting($posts); + + if ($reportHtml) { + // Save to database + $con->query("CREATE TABLE IF NOT EXISTS `marketing_reports` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `platform` VARCHAR(50) NOT NULL, + `report_html` TEXT NOT NULL, + `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP + )"); + + $stmt = $con->prepare("INSERT INTO marketing_reports (platform, report_html) VALUES (?, ?)"); + $stmt->execute([$platform, $reportHtml]); + } + + echo json_encode(['status' => 'success', 'message' => 'Posts evaluated and report saved']); + break; + default: echo json_encode(['status' => 'error', 'message' => 'Invalid action']); } diff --git a/socialBot/app/src/main/java/com/siro/socialmedia_bot/network/SocialBotClient.kt b/socialBot/app/src/main/java/com/siro/socialmedia_bot/network/SocialBotClient.kt index 0027fde1..1455bc0c 100644 --- a/socialBot/app/src/main/java/com/siro/socialmedia_bot/network/SocialBotClient.kt +++ b/socialBot/app/src/main/java/com/siro/socialmedia_bot/network/SocialBotClient.kt @@ -54,6 +54,43 @@ object SocialBotClient { postData("action=complete_task", "task_id=$taskId&result=$result") } + fun evaluatePosts(posts: List