sperea.es
Published on

Creating a bot to autopost RSS feeds on mastodon with GNU/Linux

Authors

To automate the publication of blog posts to your Mastodon account, you'll need to install a software called feed2toot on your Linux computer. It's a script that associates with your account to automatically publish on Mastodon.

Here's how it works:

First, you need to install Feed2Toot, which requires having Python3 interpreter and Pip3 package manager:

pip3 install feed2toot`

You can create a specific user to run this if you're deploying it on a VPS, or you can have it on your own computer and run it through your own user. For the sake of explanation, let's assume we decide to run it with the user 'sergioperea'.

First, create a folder for the program's configuration files and give the necessary permissions to access it:

mkdir -p /etc/feed2toot/credentials /var/lib/feed2toot
chown -R sergioperea:root /etc/feed2toot /var/lib/feed2toot

Now let's initialize Feed2Toot:

register_feed2toot_app

You will be prompted for your Mastodon access credentials (instance, username, password). If everything goes well, the 'feed2toot_usercred.txt' and 'feed2toot_clientcred.txt' files will be created in your home directory. Copy these files to the '/etc/feed2toot/credentials' folder.

You don't need to modify these files.

Next, create two files wherever you want (such as in a folder in your home directory): 'hashtag.txt' and 'rss.txt'.

rss.txt file:

This file will contain the RSS feed URL for each blog you want to autopost on Mastodon, with each URL on a separate line.

For example:

https://sperea.es/feed.xml
https://www.cdn.geeksforgeeks.org/feed/

hashtag.txt file:

This file will contain one hashtag per line, which will be added to each post.

software libre
open-source

This example file will generate the hashtags '#softwarelibre' and '#opensource'. Note that certain characters and spaces have been removed.

Now, create a configuration file named 'feed2toot.ini' in the '/etc/feed2toot' directory. Here's an example of what mine looks like:

[mastodon]
instance_url=https://mastodon.social
user_credentials=/etc/feed2toot/credentials/feed2toot_usercred.txt
client_credentials=/etc/feed2toot/credentials/feed2toot_clientcred.txt

[cache]
cachefile=/var/lib/feed2toot/feed2toot.db

[rss]
uri_list=/home/sergioperea/Documentos/tootfeeds/rss.txt
toot={title} {link}

[hashtaglist]
several_words_hashtags_list=/etc/feed2toot/hashtags.txt

Running the Bot

If you've followed all the steps correctly, Feed2toot is now ready to be executed.

The problem is that if you run it as is, it will publish all the feeds from the specified addresses, which can result in a flood of toots and inconvenience your followers.

To avoid this, we will run a command to save the feeds in the database as sent but not send them. This way, only new posts will be sent. To do this, run the following command:

feed2toot --populate-cache -c /etc/feed2toot/feed2toot.ini

From now on, each time you run Feed2toot, only new posts will be published.

To ensure regular execution, it's best to schedule the bot using crontab, for example, every half hour:

# Publish feeds on Mastodon
*/30 * * * * feed2toot -c /etc/feed2toot/feed2toot.ini

That's it. You can find my Mastodon account if you'd like to follow me on that social network.

Bonus Track: Going Further

It's possible to publish directly from Python code using the Mastodon.py library to create more sophisticated bots.

pip3 install Mastodon.py

The code is straightforward, so it doesn't require much explanation:

from mastodon import Mastodon

# Token and instance URL
mastodon = Mastodon(
    access_token='43hu433',
    api_base_url='https://mastodon.social/'
)

mastodon.status_post("Message from my Python Bot")

And now, you just need to execute it!