Make Telegram bot for notifying about new RSS feed items

Ljubiša Moćić
3 min readApr 11, 2020

Replace your RSS reader with simple Telegram bot

Motivation

Recently I’ve got order on Fiverr to create Telegram bot which would notify public channel with new job postings in Singapore.

The data from the website was available through RSS feed. The bot should check every 20 minutes for a new content.

It was very simple to create it, so I want to share with you, how I did it.

Prerequisites

So let’s get started

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 .

We will need library for making requests

pip3 install requests

Add the code for sending the message to channel

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!')

Now let’s run this script and it should send the message to the channel

python3 main.py

Parse feed

Let’s add library for parsing RSS feeds

pip3 install feedparser

Now let’s see if it works

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))

But now, it sends all of the items from RSS feed. Let’s change that.

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

I’ve deployed it to AWS Lambda and set it to run every 20 minutes with CloudWatch. You can see how to deploy here and how to add CloudWatch trigger here.

But if you want to run it locally, we could add something like this:

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

You can find full code sample here

Conclusion

This is an interesting way of getting notified about RSS feed items, it takes some time to set it up, but it seemed to me like a cool thing to do.

I’ve really enjoyed doing this, but there is a little bit more stuff to do, like error handling or more simplified deploying. But, hopefully it will be useful to you!

Ping me on @ljmocic if you have any questions!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

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

--

--