Hey guys, I hope everyone is having a good day. Today I want to create a short article on how to read RSS feeds with Python and notify a Discord channel using Webhook. First of all I wanted to use some of the Discord bot alternatives on the market, but the free options were very limited for what I wanted š , itās not bad to pay for the use of tools that make your life easier, but honestly I took this as an excuse to do some coding over the weekend. It is my hobby! ā¤ļø.
Letās start
To get started, we need to search for any site with RSS support; in my case i searched for sources for android development and for this example i will use the following URL:
Once the project is created, we need to install the **feedparser **library to read rss with python.
pip install feedparser
Next, weāll create our main.py, add the webhook, username, and avatar url.
import feedparser
webhook = "https://discord.com/webhooks/{webhook_id}/{webhook_token}"
username = "Androd News"
avatar_url = "https://source.android.com/static/docs/setup/images/Android_symbol_green_RGB.png"
feed = feedparser.parse(feed_url)
print(feed)
If everything is ok, when you run the program you should be able to see a terminal output similar to the following image
Letās continue, the next thing we will do is create a class to manage our Discord data and have the ability to launch notifications. We will call our class discord_module/DiscordNews.py
import requests
class DiscordNews:
def __init__(self, webhook, username, avatar_url, feed):
self.webhook = webhook
self.username = username
self.avatar_url = avatar_url
self.feed = feed
def prepare_and_notify(self):
for entry in self.feed.entries:
self.__notify_to_discord_channel(entry)
def __notify_to_discord_channel(self, data):
# Code for notify to Discord channel
As you may have noticed we have 2 parameters in the class constructor: avatar_url and username, these are used to customize the posting of the message in the Discord channel; with avatar_url we specify a url of an image that will be the icon of our bot and username will be its name.
The next thing is to import our class into main.py and call the function prepare_and_notify
discord = DiscordNews(webhook, username, avatar_url, feed)
discord.prepare_and_notify()
Finally, we run the program.
python main.py
Result
See the full code here
If you like my content and want to support my work, you can give me a cup of coffee āļø š„°
Follow me in
Twitter: @devjcastro
Linkedin: dev-jorgecastro