Update: 2026-07-05 17:25:01

This commit is contained in:
Hamza-Ayed
2026-07-05 17:25:01 +03:00
parent 2593558c9f
commit b89eab0651
2 changed files with 142 additions and 0 deletions
@@ -0,0 +1,112 @@
<?php
// telegram_scraper.php
if (php_sapi_name() !== 'cli') {
die("This script must be run from the command line (Terminal).\n");
}
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',
];
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";
@@ -0,0 +1,30 @@
<?php
if (php_sapi_name() !== 'cli') {
die("This script must be run from the command line (Terminal).\n");
}
echo "=================================================\n";
echo " Siro Telegram MTProto Setup (MadelineProto) \n";
echo "=================================================\n\n";
if (!file_exists('madeline.php')) {
echo "[*] Downloading MadelineProto Library... (This may take a moment)\n";
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
echo "[+] Download complete.\n";
}
require_once 'madeline.php';
echo "[*] Initializing Telegram Client...\n";
echo "[!] You will be prompted to enter your phone number (with country code, e.g. +9627...)\n";
echo "[!] Then, enter the OTP code sent to your Telegram app.\n\n";
$settings = new \danog\MadelineProto\Settings();
$settings->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";