Make Telegram bot for notifying about new RSS feed items

Motivation

Prerequisites

Create bot

  1. Message @BotFather in Telegram
  2. Enter /start command
  3. Add all of the info it requests
  4. Save the token in the end
  5. Create public channel
  6. Add bot to that channel
  7. Allow it to be admin

Create script

mkdir telegram-bot && cd telegram-bot && touch main.py && code .
pip3 install requests
import requestsBOT_TOKEN = '' # the one you saved in previous step
CHANNEL_ID = '' # don't forget to add this
def send_message(message):
requests.get(f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={CHANNEL_ID}&text={message}')
send_message('it works!')
python3 main.py

Parse feed

pip3 install feedparser
import feedparserFEED_URL = 'https://www.feedforall.com/sample.xml'def main():
rss_feed = feedparser.parse(FEED_URL)
for entry in rss_feed.entries:
print(entry)
send_message(str(entry))

Send recent items from RSS feed

from datetime import timedelta, datetime
from dateutil import parser
FEED_URL = 'https://www.feedforall.com/sample.xml'def main():
rss_feed = feedparser.parse(FEED_URL)
for entry in rss_feed.entries:
parsed_date = parser.parse(entry.published)
parsed_date = (parsed_date - timedelta(hours=8)).replace(tzinfo=None) # remove timezone offset
now_date = datetime.utcnow()
published_20_minutes_ago = now_date - parsed_date < timedelta(minutes=20)
if published_20_minutes_ago:
send_message(entry)

Deploy

if __name__ == "__main__":
while(True):
main()
sleep(20 * 60)

Conclusion

If you got so far, you probably found something useful. Please consider supporting me :D

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Focusing on Programming Elixir

Crust Project Development. — Jan. 11 — Jan. 17

Creating a Webex Chatbot — Part 2

COVID-19 visualizations with Stata Part 5: Stacked area graphs

Why Python for Data Science?

How to Disable Task View on Windows 10?

Installing Pega in Apple Silicon (ARM64) with Docker

Inkdrop 5.3.0 — Web Clipper, Alfred integration, better conflict resolver

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ljubiša Moćić

Ljubiša Moćić

Software Engineer

More from Medium

Decorators in Python

How to install Airflow locally using Docker

Airflow Dashboard

Remote connect AWS windows EC2 without exposing public IP with Omniedge

Building an Air Quality Index App using the OpenWeather API