My new Masotdon Bot; Writing social media bots

August 1, 2020

This week I created my own bot for the social network Mastodon. The idea came about when I was using Hacker News “past” feature, which shows an archive of older posts sorted by date. So, my bot will post each day three times the top posts from one decade in the past. Here is a link so you can check it out: the bot

It is interesting and grounding to see what was going on one decade ago. Will the top posts be about some new exciting tech that has since been completely forgotten? Or perhaps some contemporary blog post that still remains insightful? The comments on the articles remain a time capsule of a different internet age.

This experiment shows how temporary the internet is also. So many of the links are to content that doesn’t work. Even if the website is still operating, images or video the articles are often missing. If you want more proof of this, check out your youtube favorites/liked playlists. Odds are it’s riddled with [Deleted Video]s, with no information about what was ever in its place. It’s a reminder to download anything that you truly wish to keep around.

Onto the code

The Mastodon.py library provides a python wrapper for the Mastodon API. The documentation for it provides an OK explanation at setting things up. So to clarify, there is one piece of code it gives to generate a clientcred.secret file. After generating this, you will generate a usercred.secret file. You then use this to log in when you want to use your bot. While this is simple after the fact, it does make it so you have to run separate code to set up the secret files. Here is a link to the source file for my bot, but keep in mind it requires you to have generated the secret files.

After the setup, you should be ready to use any of the docs to interact with Mastodon as you please. Sending a string as a toot is about as simple as it can be. I generate the string by parsing the HTML from Hacker News with Beautiful Soup, finding the link to the story and the comments. I could not find a way to use the HN API to view past posts, so I am just requesting the web page. Since I am only doing this three times a day, this shouldn’t really be be causing any stress on the system. I set it up so the bot takes an argument from 0 to 2, for which post it should use (the first through third). I set up cron to run three separate times, each calling the script with a different argument. This was simpler than parsing which hour of the day went with which post.

The Good Old Days

Writing this bot was very easy for me, and it was nostalgic. In highschool I ran multiple twitter bots that would send out all sorts of things. It has been over 5 years since I last touched social media bots (though I have been working with telegram bots), and my programming skill has improved hundreds of times over. I struggled with understanding the OO paradigm back then, and reading documentation was hard and dense. I am able to look back on my old code with some fondness for how hacked together everything seems, piecing together random bits of various example tutorials. Luckily, I have them all saved, and I know they won’t just drop off from the internet ;)