DEV Community

Cover image for RSS reader in Python for Discord
Jorge Castro
Jorge Castro

Posted on • Edited on • Originally published at Medium

RSS reader in Python for Discord

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

If everything is ok, when you run the program you should be able to see a terminal output similar to the following image

Example of RSS reader result

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
Enter fullscreen mode Exit fullscreen mode

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.

details of the bot’s post on the Discord channel

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()
Enter fullscreen mode Exit fullscreen mode

Finally, we run the program.

python main.py
Enter fullscreen mode Exit fullscreen mode

Divider

Result

Example of messages in Discord channel

See the full code here

Divider

If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰

Ko-fi

Buy me a coffee

Follow me in

Top comments (5)

Collapse
 
tamaraglenn profile image
TamaraGlenn

A complete bot that can post link on discord for every news from Twitter/Reddit/Youtube channel/RSS feeds . iplwin

Collapse
 
jorgecastro profile image
Jorge Castro

Thanks for your feedback. This is the first article explaining a bit of the most basic, although it is detailed for Discord, modifications could be made so that it is reading information from other sources, thanks to Webhooks, we could also send notifications to other channels for example: slack

Collapse
 
jorgecastro profile image
Jorge Castro

what is ipwin?

Collapse
 
quitsmoking profile image
Vaselios soteros

Thanks for making this, Jorge .

For some reason, I am getting a an error for No module named 'discord_module'

Collapse
 
jorgecastro profile image
Jorge Castro • Edited

You need to create a package called discord_module, example:

Python Project
- discord_module
- - DiscordNews.py