Created
July 27, 2025 15:22
-
-
Save aligalehban/6f0aedf41c1dd434cd1b17d7396a5b6b to your computer and use it in GitHub Desktop.
RSS2Telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// --- CONFIGURATION --- | |
// Replace with your Telegram Bot Token | |
$bot_token = 'YOUR_BOT_TOKEN_HERE'; | |
// Replace with your Telegram Channel ID (e.g., @yourchannel or -100XXXXXXXXXX) | |
$channel_id = 'YOUR_CHANNEL_ID_HERE'; | |
// Replace with your desired RSS feed URLs | |
$feeds = [ | |
'https://your-feed-url-1.com/rss', | |
'https://your-feed-url-2.com/rss', | |
]; | |
// Minimum and maximum character limits for message description | |
$min_chars = 70; | |
$max_chars = 300; | |
// Footer text added to every Telegram message | |
$footer = "\n\n——————\n add your channel id\nJoin our channel to get the latest news \nFollow us for more updates "; | |
// --- FUNCTIONS --- | |
// Fetch and parse feed items using SimplePie | |
function fetch_feed_items($url) { | |
require_once __DIR__ . '/simplepie/autoloader.php'; | |
$feed = new SimplePie(); | |
$feed->set_feed_url($url); | |
$feed->enable_cache(true); | |
$feed->set_cache_location(sys_get_temp_dir()); | |
$feed->init(); | |
return $feed->get_items(); | |
} | |
// Send message to Telegram | |
function send_to_telegram($token, $chat_id, $message) { | |
$url = "https://api.telegram.org/bot{$token}/sendMessage"; | |
$params = [ | |
'chat_id' => $chat_id, | |
'text' => $message, | |
'parse_mode' => 'HTML', | |
'disable_web_page_preview' => false, | |
]; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_exec($ch); | |
curl_close($ch); | |
} | |
// Check if message has already been sent (by hashed link) | |
function already_sent($id) { | |
$log_file = __DIR__ . '/sent.log'; | |
$log = file_exists($log_file) ? file($log_file, FILE_IGNORE_NEW_LINES) : []; | |
return in_array($id, $log); | |
} | |
// Mark message as sent (by hashed link) | |
function mark_sent($id) { | |
file_put_contents(__DIR__ . '/sent.log', $id . PHP_EOL, FILE_APPEND); | |
} | |
// --- MAIN LOGIC --- | |
// Loop through each feed and send new items to Telegram | |
foreach ($feeds as $feed_url) { | |
$items = fetch_feed_items($feed_url); | |
foreach ($items as $item) { | |
$link = $item->get_link(); | |
$title = $item->get_title(); | |
$desc = $item->get_description() ?: $item->get_content(); | |
$desc = strip_tags($desc); | |
$desc = html_entity_decode($desc, ENT_QUOTES | ENT_HTML5, 'UTF-8'); | |
$desc = trim($desc); | |
// If the description is too short, add a placeholder | |
if (strlen($desc) < $min_chars) { | |
$desc .= "\n\n📌 This news item has no summary or couldn't be fetched properly."; | |
} | |
// Truncate the description to the max length | |
$desc = mb_substr($desc, 0, $max_chars) . '...'; | |
// Generate unique hash for each news item | |
$hash = md5($link); | |
if (already_sent($hash)) continue; | |
// Format the Telegram message | |
$msg = "<b>{$title}</b>\n\n{$desc}\n\n<a href=\"{$link}\">Click here to read more</a>{$footer}"; | |
// Send and log the message | |
send_to_telegram($bot_token, $channel_id, $msg); | |
mark_sent($hash); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment