<?php

$scriptStart = microtime(true);

// Telegram bot token and user ID
$armpToken = "6699635284:AAFklesnB4P6CmMz0mc8nIVq72XjSRf0gCQ";
$armpUserID = "1289522313";

// Read filters from Filters.txt
$filterLoadStart = microtime(true);
$filtersFile = "../Filters.txt";
if (!file_exists($filtersFile)) {
die("Filters.txt file not found.");
}

$Filters = file($filtersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$Filters = array_map('trim', $Filters);

if (empty($Filters)) {
die("No filters found in Filters.txt.");
}

// Read channel usernames from chs.txt
$channelsFile = "chs.txt";
if (!file_exists($channelsFile)) {
die("chs.txt file not found.");
}

$channels = file($channelsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if (empty($channels)) {
die("No channels found in chs.txt.");
}

// Process each channel
foreach ($channels as $Channel) {
$channelStart = microtime(true);

$Channel = trim($Channel);
if (empty($Channel)) {
continue;
}

$url = "https://eitaa.com/$Channel";

// Fetch the HTML content from the URL
$fetchStart = microtime(true);
$html = file_get_contents($url);
$fetchTime = (microtime(true) - $fetchStart);

if ($html === false) {
echo "Failed to fetch content for channel: $Channel\n\n";
continue;
}

// Create a new DOMDocument instance
$parseStart = microtime(true);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$parseTime = (microtime(true) - $parseStart);

// Query to find the last message in the channel
$messages = $xpath->query('//div[contains(@class, "etme_widget_message_wrap")]');

if ($messages->length > 0) {
$lastMessage = $messages->item($messages->length - 1);
$mainMessageTextNodes = $xpath->query('.//div[contains(@class, "etme_widget_message_text") and not(ancestor::div[contains(@class, "etme_widget_message_reply")])]//text()', $lastMessage);
$messageText = "";
foreach ($mainMessageTextNodes as $node) {
$messageText .= $node->textContent . " ";
}
$messageText = trim($messageText);

// Check filters
$filterStart = microtime(true);
$foundFilters = array();
foreach ($Filters as $filter) {
if (str_contains($messageText, $filter)) {
$foundFilters[] = $filter;
}
}
$filterCheckTime = (microtime(true) - $filterStart) * 1000;

if (!empty($foundFilters)) {
$channelFile = "$Channel.txt";
if (!file_exists($channelFile) || $messageText != file_get_contents($channelFile)) {
$filterList = implode(", ", $foundFilters);
$channelTotal = (microtime(true) - $channelStart);

$armpText = "<code>$messageText</code>

━━━━━━━━━━━━━━━━━━━━━━
🔔 NEW MESSAGE FROM EITAA
━━━━━━━━━━━━━━━━━━━━━━

📍 Channel: @$Channel
🔍 Found Filter: $filterList

━━━━━━━━━━━━━━━━━━━━━━
⏱ PERFORMANCE STATS:
━━━━━━━━━━━━━━━━━━━━━━

⏳ Fetch Time: " . round($fetchTime, 2) . "s
⚙️ Parse Time: " . round($parseTime, 3) . "s
🔎 Filter Check: " . round($filterCheckTime, 2) . "ms
📊 Total Time: " . round($channelTotal, 2) . "s

━━━━━━━━━━━━━━━━━━━━━━";

$armp = file_get_contents("https://api.telegram.org/bot$armpToken/SendMessage?chat_id=$armpUserID&parse_mode=HTML&text=" . urlencode($armpText));
if ($armp) {
echo "Text Sent To TG for channel: $Channel. [" . round($channelTotal, 2) . "s]\n";
}

file_put_contents($channelFile, $messageText);
}
else {
echo "Text Already Sent To TG for channel: $Channel.\n";
}
}
else {
echo "Message from $Channel is not important.\n";
}
}
else {
echo "No messages found for channel: $Channel.\n";
}
}

$scriptTotal = (microtime(true) - $scriptStart);

echo "\n━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Script completed in " . round($scriptTotal, 2) . " seconds\n";
echo "━━━━━━━━━━━━━━━━━━━━━━\n";
?>