نسخة كاملة من مستودع سيرو عند ecfe7568 لتكون أساس تطبيق «انطلق». نُسخ المتعقَّب في git فقط (12,509 ملفاً / 302 م.ب) بـ git archive، لا `cp -r` — فاستُثنيت تلقائياً مخلفات البناء (build · node_modules · .dart_tool · .gradle · Pods ≈ 10.7 غ.ب) وكل ما يستثنيه .gitignore. هذا الكوميت **بلا أي تعديل عمداً** حتى يكون كل ما يليه فرقاً مقروءاً مقابل سيرو الأصلي. سيرو نفسه لم يُمسّ. ⚠️ لا يبني بعد: `.env` و`lib/env/env.g.dart` غير متعقَّبين في سيرو (وهذا صحيح — أسرار لكل مستأجر). كل تطبيق فلاتر هنا يحتاج .env خاصاً بانطلق ثم توليد env.g.dart عبر build_runner. لا تُنسخ أسرار سيرو. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
158 lines
5.0 KiB
PHP
158 lines
5.0 KiB
PHP
<?php
|
|
// telegram_scraper.php
|
|
if (php_sapi_name() !== 'cli') {
|
|
die("This script must be run from the command line (Terminal).\n");
|
|
}
|
|
|
|
// Load .env for CLI
|
|
$homeDir = '';
|
|
if (preg_match('#^(/home/[^/]+)#', __DIR__, $matches)) {
|
|
$homeDir = $matches[1];
|
|
}
|
|
|
|
$envPaths = [
|
|
__DIR__ . '/../.env',
|
|
__DIR__ . '/../../.env',
|
|
$homeDir ? $homeDir . '/.env' : ''
|
|
];
|
|
foreach ($envPaths as $envPath) {
|
|
if (file_exists($envPath)) {
|
|
$lines = file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if (empty($line) || strpos($line, '#') === 0) continue;
|
|
$parts = explode('=', $line, 2);
|
|
if (count($parts) === 2) {
|
|
$key = trim($parts[0]);
|
|
$val = trim(trim($parts[1]), "\"'");
|
|
putenv("$key=$val");
|
|
$_ENV[$key] = $val;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
require_once __DIR__ . '/../core/Services/SiroGeminiService.php';
|
|
require_once __DIR__ . '/../core/Database/Database.php';
|
|
|
|
if (!file_exists(__DIR__ . '/madeline.php')) {
|
|
die("Error: madeline.php not found. Please run telegram_setup.php first.\n");
|
|
}
|
|
require_once __DIR__ . '/madeline.php';
|
|
|
|
if (!file_exists(__DIR__ . '/telegram_session.madeline')) {
|
|
die("Error: No session found. Please run telegram_setup.php to authenticate.\n");
|
|
}
|
|
|
|
echo "[*] Starting Telegram Scraper...\n";
|
|
|
|
$settings = new \danog\MadelineProto\Settings();
|
|
$settings->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',
|
|
'@YallaGo_Captains',
|
|
'@TaxiFJOR','@zakinntaxi','@zakinncaptains','@zakinaleppo','https://t.me/+rsfxbXEyDCczY2Nk'
|
|
];
|
|
|
|
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.\n";
|
|
echo "--- Sample (First 3 messages) ---\n";
|
|
for($i=0; $i<min(3, count($allMessages)); $i++) {
|
|
echo $allMessages[$i] . "\n";
|
|
}
|
|
echo "---------------------------------\n";
|
|
echo "[*] Sending to Gemini AI for analysis...\n";
|
|
|
|
$gemini = new SiroGeminiService();
|
|
|
|
if (!getenv('GEMINI_API_KEY') && empty($_ENV['GEMINI_API_KEY'])) {
|
|
echo "[-] ERROR: GEMINI_API_KEY is completely missing in CLI environment. Make sure your .env file exists.\n";
|
|
exit;
|
|
}
|
|
|
|
$reportHtml = $gemini->evaluateTelegramForReporting($allMessages);
|
|
|
|
if ($reportHtml && strpos($reportHtml, 'لا توجد بيانات مفيدة') === false) {
|
|
echo "[+] AI generated a useful report. Saving to DB...\n";
|
|
|
|
$con = Database::get('main');
|
|
|
|
$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 or API Error.\n";
|
|
echo "--- Gemini Raw Output ---\n";
|
|
echo var_export($reportHtml, true) . "\n";
|
|
echo "-------------------------\n";
|
|
}
|
|
|
|
echo "[*] Done.\n";
|