Commenting with Mastodon

By jeff


I missed having the potential of discussions around things that I post here, but at the same time I didn't want to add load to the site to manage comments or complexity of databases, etc.

I remember seeing somewhere that people used to use Facebook for comments on their site, and thought, well can't I use twitter or Mastodon? I decided to work with Mastodon as the test case and I figured it out.

How it works

The idea is to add a link at the bottom of every post, and when someone clicks it they go to a dedicated post on my Mastodon Instance. There is a quick check before I redirect them, if someone has clicked the link before it sends them to the existing "master" thread, and if no one has clicked it the API creates that thread.

I will use a text file to keep track of everything, which for an active site with a lot of posts may get bogged down and a database would make more sense, but this site isn't like those. I may start to worry about it when I get to about 1000 posts.

Using PHP

I used PHP because I could just add it to my current host with little change.

To get started I cloned this PHP Mastodon API class and then created my bot. They have a couple examples that can get you started.

The github repo is here.

Using a text file rather than a database

PHP has a great function where you can easily import and manipulate csv files as arrays.

So the first thing that the script does when someone clicks the link is convert the text file to an array and search for the URL. This is the function that I'm using to do the search.

function find_status($filename, $url) {
$f = fopen($filename, "r");
$result = false;
while ($row = fgetcsv($f)) {
    if ($row[0] == $url) {
        $result = $row[1];
return $result;

If the $result is not false, then I 301 redirect to the existing post, and if it is falase I create the new post.

Creating the status post

Because I think that statuses look better when there is an image, and mastodon isn't great at displaying the open graph image, I first need to upload the open graph image that I have for the post, if available.

But because I use windows, and the pelican-photos plugin has been doing something "strange" with the url adding a '' in the middle for some reason, I needed to replace them when there.

It was easy enough to do when I figured it out

$postImage = str_replace("\\",'/',$postImage);

Then I can upload the image with the API wrapper and move to the next step, the status text.

I originally was using both the title and description of the post, but decided to remove the description. So the status text looks like this:

$status_text = $postTitle. ' -> '.$postURL;

Then the post is posted and we keep track of the status id so that we can add it to text file for the next person to click the link because of one of my hot takes.

$status_id = $status_response['id'];

$f = fopen($comment_record, 'a');
fputcsv($f, [$postURL, $status_id]);

And finally redirect to the post.

This all takes a few seconds, and maybe if I have time I will try to speed it up, but the only thing that takes time is the image upload, it is very fast if the status already exists.

Adding to theme

I'm using the custom theme that you can find here -> github repo. The only change I made to the theme was adding this link:

{% if MASTODON_COMMENTS is sameas True %}
        <a href="{{ SITEURL }}/mastodon-comments/bot.php?
            title={{ article.title|striptags }}
            &url={{ SITEURL }}/{{ article.url }}
            {% if article.photo_image %}
            &image=../{{ article.photo_image[1] }}
            {% endif %}">
            Click Here to Comment on Mastodon.
{% endif %}

What this does is pass the variables to the bot in a way that works. I will be adding a post here about mastodon and the fediverse in general which will also include HOW to get a username, etc.

Next Steps

I want to make the code cleaner, add some information around the comment link and maybe, in the far future add some javascript that will call the thread if there are comments, but that is far in the future.

The other link I may add is one for twitter too. I know a lot more people in real life on Twitter vs Mastodon, and then cross-post their comments to the mastodon thread too.

For now my itch has been scratched and I can move on to other things, like billable work.