Bot for telegram channel, example with: Swiss-IT-News
-
Clone or Download the content of this repository.
-
Optional: Ensure Docker is installed on your system.
-
Configuration options:
You have two options for configuring the bot: using a
.env
file or a TOML file namedconfiguration.toml
. You can use one of them, but remember that settings in the TOML file will take precedence over the ENV variables.-
Using a
.env
file:Create a
.env
file in the directory./bot
with the following content, replacing the placeholders with your actual data:# Development settings TIME_INTERVAL_MIN=20 ENTRY_MAX_TIME_OLD=1200 BOT_TOKEN=yourBotToken CHANNEL_ID=@yourChannel RSS_URLS="https://korben.info/feed https://www.ictjournal.ch/taxonomy/term/404/feed https://www.ictjournal.ch/taxonomy/term/31/feed https://www.ictjournal.ch/taxonomy/term/406/feed https://www.newsd.admin.ch/newsd/feeds/rss?lang=fr&org-nr=1&offer-nr=308 https://feeds.feedburner.com/TheHackersNews https://www.xavierstuder.com/feed https://www.thelazyadministrator.com/feed https://4sysops.com/feed https://www.thomasmaurer.ch/feed https://www.cyberciti.biz/atom/atom.xml https://blog.codinghorror.com/rss https://www.schneier.com/feed/atom" # Docker container settings TZ=Europe/Zurich
-
Using a
configuration.toml
file:Alternatively, you can create a
configuration.toml
file in the directory./bot
with the necessary configuration. Here's a template for the TOML file:# configuration.toml # General settings TIME_INTERVAL_MIN = 20 # Run the feedparser every n minutes ENTRY_MAX_TIME_OLD = 1200 # 1200 seconds = 20 minutes # Bot Configuration BOT_TOKEN = "YOUR BOT TOKEN" CHANNEL_ID = "@YourChannel" # News, RSS feeds RSS_URLS = [ "https://korben.info/feed", "https://www.ictjournal.ch/taxonomy/term/404/feed", "https://www.ictjournal.ch/taxonomy/term/31/feed", "https://www.ictjournal.ch/taxonomy/term/406/feed", "https://www.newsd.admin.ch/newsd/feeds/rss?lang=fr&org-nr=1&offer-nr=308", "https://feeds.feedburner.com/TheHackersNews", "https://redmondmag.com/rss-feeds/columns.aspx", "https://redmondmag.com/rss-feeds/in-depth.aspx", "https://redmondmag.com/rss-feeds/news.aspx", "https://redmondmag.com/rss-feeds/tech-library.aspx", "https://www.xavierstuder.com/feed", "https://www.thelazyadministrator.com/feed", "https://4sysops.com/feed", "https://www.thomasmaurer.ch/feed", "https://www.cyberciti.biz/atom/atom.xml", "https://blog.codinghorror.com/rss", "https://www.schneier.com/feed/atom", ]
This file allows you to easily manage your bot's configuration in a structured format. Remember to replace the placeholders with your actual configuration details.
-
-
Build the Docker image:
docker build -t telegram-rss-bot:latest .
-
Run the Docker container:
docker run -d --name telegram-rss-bot:latest --env-file /path/to/your/.env
Replace
/path/to/your/.env
with the actual path to your.env
file.
If you prefer not using Docker, follow these steps:
-
First, download or clone the repository containing the script to your local machine or server.
-
Get a Raspberry Pi (or any computer with Python 3) and install the needed libraries:
virtualenv .venv -p python3 source .venv/bin/activate pip install -r requirements.txt
-
Create a
configuration.toml
file or an.env
file to set up your bot token, channel ID, and RSS feeds. Below are the templates for each file type:configuration.toml:
# Configuration file for Telegram Bot # General settings TIME_INTERVAL_MIN = 20 # Run the feedparser every n minutes ENTRY_MAX_TIME_OLD = 1200 # 1200 seconds = 20 min # Bot Configuration BOT_TOKEN = "YOUR BOT TOKEN" CHANNEL_ID = "@YourChannel" # News, RSS feeds RSS_URLS = [ "https://korben.info/feed", "https://www.ictjournal.ch/taxonomy/term/404/feed", "https://www.ictjournal.ch/taxonomy/term/31/feed", "https://www.ictjournal.ch/taxonomy/term/406/feed", "https://www.newsd.admin.ch/newsd/feeds/rss?lang=fr&org-nr=1&offer-nr=308", "https://feeds.feedburner.com/TheHackersNews", "https://mspoweruser.com/feed/", "https://www.numerama.com/feed/", ]
.env:
# Development settings TIME_INTERVAL_MIN=20 ENTRY_MAX_TIME_OLD=1200 BOT_TOKEN="YOUR BOT TOKEN" CHANNEL_ID="@YourChannel" RSS_URLS="https://korben.info/feed https://www.ictjournal.ch/taxonomy/term/404/feed https://www.ictjournal.ch/taxonomy/term/31/feed https://www.ictjournal.ch/taxonomy/term/406/feed https://www.newsd.admin.ch/newsd/feeds/rss?lang=fr&org-nr=1&offer-nr=308 https://feeds.feedburner.com/TheHackersNews https://redmondmag.com/rss-feeds/columns.aspx https://redmondmag.com/rss-feeds/in-depth.aspx https://redmondmag.com/rss-feeds/news.aspx https://redmondmag.com/rss-feeds/tech-library.aspx https://www.xavierstuder.com/feed https://www.thelazyadministrator.com/feed https://4sysops.com/feed https://www.thomasmaurer.ch/feed https://www.cyberciti.biz/atom/atom.xml https://blog.codinghorror.com/rss https://www.schneier.com/feed/atom"
-
The script will continuously run and execute an update at the time interval specified in your configuration file.
This chapter guides you through creating your Telegram channel and setting up a bot using BotFather, which will be used by your script to post updates.
- Open Telegram and search for the "New Message" button, usually found at the bottom right corner. Click on it and select "New Channel".
- Set your channel name and description. For example, "Swiss IT News" for IT-related updates in Switzerland.
- Choose your channel's privacy setting. For public channels, set a unique link, such as
https://t.me/SwissITNews
. - Add any desired users to your channel or skip this step to proceed to channel creation.
- In Telegram, search for the
@BotFather
user or access directly via https://telegram.me/BotFather. - Start a chat with BotFather and use the
/newbot
command to create a new bot. - Follow the instructions from BotFather to set up your bot's name and username. BotFather will then provide you with a token to access the Telegram API, which looks something like this:
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
. - Note down your bot's token; you'll need it for the script configuration.
- Add your bot to your channel as an administrator. To do this, go to your channel's "Info" section, select "Administrators", "Add Administrator", and then search for your bot by the username you gave it.
- Give your bot permissions to post messages.
This code supports a TOML formatted configuration file:
$ cat configuration.toml
# Configuration file for Telegram Bot
# General settings
TIME_INTERVAL_MIN = 20 # Run the feedparser every n minutes
ENTRY_MAX_TIME_OLD = 1200 # 1200 seconds = 20 min
# Bot Configuration
BOT_TOKEN = "YOUR BOT TOKEN"
CHANNEL_ID = "@YourChannel"
# News, RSS feeds
RSS_URLS = [
"https://korben.info/feed",
"https://www.ictjournal.ch/taxonomy/term/404/feed",
"https://www.ictjournal.ch/taxonomy/term/31/feed",
"https://www.ictjournal.ch/taxonomy/term/406/feed",
"https://www.newsd.admin.ch/newsd/feeds/rss?lang=fr&org-nr=1&offer-nr=308",
"https://feeds.feedburner.com/TheHackersNews",
"https://mspoweruser.com/feed/",
"https://www.numerama.com/feed/",
]
If the configuration file fails (doesn't exist or incorrect synthax), it will default to ENV VARs
. This last approach is highly recommended, to follow the Twelve Factor App.
To ease the development work, you can rely on a .env file with:
# Development settings
TIME_INTERVAL_MIN=20
ENTRY_MAX_TIME_OLD=1200
BOT_TOKEN="YOUR BOT TOKEN"
CHANNEL_ID="@YourChannel"
RSS_URLS="https://korben.info/feed
https://www.ictjournal.ch/taxonomy/term/404/feed
https://www.ictjournal.ch/taxonomy/term/31/feed
https://www.ictjournal.ch/taxonomy/term/406/feed
https://www.newsd.admin.ch/newsd/feeds/rss?lang=fr&org-nr=1&offer-nr=308
https://feeds.feedburner.com/TheHackersNews
https://redmondmag.com/rss-feeds/columns.aspx
https://redmondmag.com/rss-feeds/in-depth.aspx
https://redmondmag.com/rss-feeds/news.aspx
https://redmondmag.com/rss-feeds/tech-library.aspx
https://www.xavierstuder.com/feed
https://www.thelazyadministrator.com/feed
https://4sysops.com/feed
https://www.thomasmaurer.ch/feed
https://www.cyberciti.biz/atom/atom.xml
https://blog.codinghorror.com/rss
https://www.schneier.com/feed/atom"
Env Variables can be set/intjected via Dockerfile or a Kubernetes manifest.