diff --git a/backend/marketing_engine/telegram_scraper.php b/backend/marketing_engine/telegram_scraper.php new file mode 100644 index 00000000..b41e4dc9 --- /dev/null +++ b/backend/marketing_engine/telegram_scraper.php @@ -0,0 +1,112 @@ +getAppInfo()->setApiId(2040); +$settings->getAppInfo()->setApiHash('b18441a1ff607e10a989891a5462e627'); + +$MadelineProto = new \danog\MadelineProto\API('telegram_session.madeline', $settings); +// Start MadelineProto (will resume session silently since it's already authenticated) +$MadelineProto->start(); + +// Define targets (Usernames, Group IDs, or Links) +// NOTE: You must be a member of the group/channel if it is private. +$targets = [ + // 'https://t.me/example_group', + // '@example_channel', +]; + +if (empty($targets)) { + echo "[-] No targets defined. Please edit telegram_scraper.php and add your target groups/channels.\n"; + exit; +} + +$allMessages = []; + +foreach ($targets as $target) { + echo "[*] Fetching latest messages from: $target\n"; + try { + // Fetch history (last 50 messages per target) + $messages_Messages = $MadelineProto->messages->getHistory([ + 'peer' => $target, + 'offset_id' => 0, + 'offset_date' => 0, + 'add_offset' => 0, + 'limit' => 50, + 'max_id' => 0, + 'min_id' => 0, + 'hash' => 0, + ]); + + if (isset($messages_Messages['messages'])) { + foreach ($messages_Messages['messages'] as $msg) { + if (isset($msg['message']) && !empty(trim($msg['message']))) { + // Prefix with [TELEGRAM] to inform Gemini + $allMessages[] = "[TELEGRAM]: " . trim($msg['message']); + } + } + } + } catch (\Exception $e) { + echo "[-] Error fetching $target: " . $e->getMessage() . "\n"; + } +} + +if (empty($allMessages)) { + echo "[-] No text messages found.\n"; + exit; +} + +echo "[+] Collected " . count($allMessages) . " messages. Sending to Gemini AI for analysis...\n"; + +$gemini = new SiroGeminiService(); +$reportHtml = $gemini->evaluatePostsForReporting($allMessages); + +if ($reportHtml && strpos($reportHtml, 'لا توجد بيانات مفيدة') === false) { + echo "[+] AI generated a useful report. Saving to DB...\n"; + + $db = new Database(); + $con = $db->connect(); + + $stmt = $con->prepare("INSERT INTO marketing_reports (platform, report_html) VALUES (?, ?)"); + $stmt->execute(['telegram', $reportHtml]); + $reportId = $con->lastInsertId(); + + echo "[+] Report saved with ID: $reportId\n"; + + // Attempt FCM Alert + try { + require_once __DIR__ . '/../core/Services/FcmService.php'; + $fcm = new FcmService(); + $fcm->send( + token: '/topics/admin_alerts', + title: 'New Telegram Intelligence Report 📊', + body: "A new autonomous analysis report for Telegram has been generated.", + data: ['type' => 'marketing_report', 'report_id' => (string)$reportId] + ); + echo "[+] Sent FCM alert to admins.\n"; + } catch (\Exception $e) { + echo "[-] Error sending FCM: " . $e->getMessage() . "\n"; + } + +} else { + echo "[-] AI Report: No useful data in this batch.\n"; +} + +echo "[*] Done.\n"; diff --git a/backend/marketing_engine/telegram_setup.php b/backend/marketing_engine/telegram_setup.php new file mode 100644 index 00000000..7374dae5 --- /dev/null +++ b/backend/marketing_engine/telegram_setup.php @@ -0,0 +1,30 @@ +getAppInfo()->setApiId(2040); +$settings->getAppInfo()->setApiHash('b18441a1ff607e10a989891a5462e627'); + +$MadelineProto = new \danog\MadelineProto\API('telegram_session.madeline', $settings); +$MadelineProto->start(); + +echo "\n✅ Successfully authenticated! Session saved to telegram_session.madeline\n"; +echo "✅ You can now use telegram_scraper.php to fetch messages autonomously.\n";