RSS to Telegram Bot
A simple PHP script that monitors RSS feeds and automatically sends new articles to Telegram channels. Perfect for news aggregation, blog updates, or any RSS-based content distribution.
What it does
- 📡 Monitors multiple RSS feeds simultaneously
- 🚫 Prevents duplicates automatically
- ⚡ Supports Telegram Instant View
- 🎨 HTML formatting with rich text
- 📝 Comprehensive logging system
- 💾 Persists state between runs
- ⏰ Cron job compatible
- 🛠️ Error handling and automatic recovery
How it works
Tracking system
The script uses a simple but effective system to avoid duplicate messages:
- State file (
rss_state.json
): Stores info about processed articles for each feed and date - Daily reset: Automatically cleans old data, keeping only current day's records
- URL-based detection: Uses article links to identify duplicates
- Per-feed tracking: Each RSS feed maintains its own state
Workflow
RSS Feed → Parse XML → Filter by date → Check duplicates → Format message → Send to Telegram
- RSS parsing: Uses SimpleXML to read feeds
- Date filtering: Only processes today's articles (configurable)
- Anti-duplicates: Checks against already sent URLs
- Formatting: Creates HTML messages with source attribution
- Delivery: Sends formatted messages to the channel
Installation
-
Clone the repository
git clone https://github.com/yourusername/rss-to-telegram-bot.git cd rss-to-telegram-bot
-
Create a Telegram bot
- Message @BotFather on Telegram
- Use
/newbot
to create a new bot - Copy the API token
-
Get channel ID
- Add @userinfobot to your channel
- Forward a message from the channel to @userinfobot
- Copy the channel ID (starts with
-100
)
-
Configure the script Edit the configuration section:
$apiToken = "YOUR_BOT_TOKEN_HERE"; $channelId = "YOUR_CHANNEL_ID"; $rssFeeds = [ ["https://example.com/feed/", ""], ["https://another-feed.com/rss/", "INSTANT_VIEW_HASH"] ];
Configuration
Basic settings
$apiToken
: Your bot token from BotFather$channelId
: Target Telegram channel ID (must start with-100
)$rssFeeds
: Array of RSS feeds to monitor
RSS feed format
$rssFeeds = [
["RSS_URL", "INSTANT_VIEW_HASH"],
["https://example.com/feed/", ""], // Without instant view
["https://news.site.com/rss/", "abc123def456"] // With instant view
];
Instant View (optional)
Telegram's Instant View provides a clean, fast-loading version of articles:
- Create an Instant View template at instantview.telegram.org
- Get the template hash from the URL
- Add it as the second parameter in the RSS feed array
Automation
Cron job (recommended)
Add to your crontab to run every 15 minutes:
crontab -e
Add this line:
*/15 * * * * /usr/bin/php /path/to/rss-to-telegram-bot.php >/dev/null 2>&1
Other scheduling examples
# Every 5 minutes
*/5 * * * * /usr/bin/php /path/to/script.php
# Every hour
0 * * * * /usr/bin/php /path/to/script.php
# Twice daily (9 AM and 6 PM)
0 9,18 * * * /usr/bin/php /path/to/script.php
File structure
rss-to-telegram-bot/
├── rss-to-telegram-bot.php # Main script
├── rss_state.json # Generated: tracks processed articles
├── rss_log.txt # Generated: execution logs
├── LICENSE # GPL v3 license
└── README.md # This file
Generated files
State file (rss_state.json
)
Tracks processed articles to prevent duplicates:
{
"https://example.com/feed/": {
"2024-01-15": [
"https://example.com/article1",
"https://example.com/article2"
]
}
}
Log file (rss_log.txt
)
Contains execution logs with timestamps:
[2024-01-15 10:30:01] Script started for date: 2024-01-15
[2024-01-15 10:30:02] Feed: https://example.com/feed/, New items: 3
[2024-01-15 10:30:05] Message sent successfully: Breaking News Article
[2024-01-15 10:30:06] Script completed
Message format
The bot sends formatted messages with:
- Source attribution with publication date
- Article title in bold
- Description/excerpt with preserved HTML formatting
- Direct link to original article
- Invisible Instant View link (if configured)
Example output:
Source: Example News - 15/01/2024
Breaking: Important News Update
This is the article description with basic HTML formatting preserved.
https://example.com/article-url
Troubleshooting
Common issues
-
No messages sent
- Check bot token and channel ID
- Verify bot is admin in the channel
- Check if RSS feed URLs are accessible
-
Duplicate messages
- Ensure
rss_state.json
has write permissions - Check if script is running multiple times simultaneously
- Ensure
-
HTML formatting issues
- Verify Telegram bot API limits (4096 characters max)
- Check for unsupported HTML tags
Debug mode
For testing, modify the date to process yesterday's articles:
// $todayDate = date('Y-m-d');
$todayDate = date('Y-m-d', strtotime('-1 day'));
Permissions
Ensure proper file permissions:
chmod 755 rss-to-telegram-bot.php
chmod 666 rss_state.json rss_log.txt
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement
) - Commit your changes (
git commit -am 'Add new feature'
) - Push to the branch (
git push origin feature/improvement
) - Create a Pull Request
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
What this means:
- ✅ You can use this software for any purpose
- ✅ You can modify and distribute it
- ✅ You can use it commercially
- ⚠️ If you distribute modified versions, they must also be GPL v3
- ⚠️ You must include the original license and copyright notices
Support
If you encounter issues or have questions:
- Check the troubleshooting section
- Review the log file for error messages
- Open an issue on GitHub with detailed information
Changelog
v1.0.0
- Initial release
- Basic RSS to Telegram functionality
- State tracking system
- Instant View support
- Comprehensive logging