Bots on Mastodon

Toot programmatically with • February 4 2023

My Twitter artbot @Philateleology cross-posts to Mastodon, but with the possible shuttering of Twitter’s free API tier, I’m not sure how long the method I currently use (automatically duping tweets to toots) will continue to work. (formerly Integromat), which is what I use to grab the generated images and post the tweets on a schedule, does have a native Twitter integration but does¬†not have a Mastodon integration (yet?). No sweat, Mastodon has an API we can use to to do the same thing. I couldn’t find a guide to smush Mastodon and together, so here we are.

Let’s go! On the Mastodon side:

  • Log into your account, then go to Preferences and then Development. You can also get there by appending /settings/development to the URL of whatever Mastodon instance you’re on.
  • Create a new application, give it an Application Name (in this case I gave it the name of the bot in question), for the Application Website I put (I honestly don’t think this does anything, you can probably leave this blank), and leave the Redirect URI as you found it.
  • Check off the Scopes you need. If you’re setting up an image-posting bot like I am, you will probably only need write:media and¬†write:statuses.
  • Great success! Your app should have been set up and, if you click into it, you’ll see three keys (that all look like a cat tapdanced across the keyboard). The one we want to keep track of is Your access token.

Over to!

  • ¬†Log into and create a new scenario.
  • Your scenario should have some way to getting the image you want to post to Mastodon. Probably an HTTP module set to Get a file¬†(that’s what I use. You can read more about the mechanics of the bot if you like). To get started with tooting, I’m going to add another¬†HTTP module, specifically to Make a request.
  • We’re going to upload the image in question to Mastodon.¬†In the URL field, you want to put https://[your mastodon instance URL]/api/v1/media. Just an FYI, don’t include the square brackets, they’re just to show where you need to put something.
  • Method should be POST.
  • Body type should be Multipart/form-data.
  • Open up the Headers section, we’re going to add two things: add one with the Name¬†Content-Type and the Value multipart/form-data.
  • Add another item, this time with Name Authorization and Value Bearer [your access token]. Yes, that’s the word Bearer, a space, then the access token you got from Mastodon.
  • Now open the Fields¬†section, and to Item 1, choose Field Type File, and for¬†Key, type in file, and select HTTP – Get a file (the module that grabs your image!) as the source, via radio button.
  • Set Parse Response to Yes.

What the heck was all that? Well, making a toot with an image is a two-step process ‚Äď first uploading the image to Mastodon, then referencing it in our actual toot. Whew! Before doing anything else, run this scenario! You need to get a response back from Mastodon for the next step.

On to creating the toot itself:

  • Add another HTTP module, again, Make a request.
  • The URL this time is¬†https://[your mastodon instance URL]/api/v1/statuses.
  • Method is once again POST.
  • Body type is, again, Multipart/form-data.
  • Exactly as in the previous module, add a Header item with¬†Name Authorization and Value Bearer [your access token]. You don’t need to put in the “content-type” stuff this time.
  • Dig into the Fields dropdown again. We’re going to add two items. First: Field type is Text, Key is status. The Value field is where we post the textual content of our toot. For @Philateleology I use an envelope emoji for every tweet/toot, so this is easy. If you use dynamic text, that would be referenced here.
  • The second item again has Field type set to Text. The key¬†should be¬†media_ids[]. Include those square brackets this time! This is how Mastodon knows what media (the image you just uploaded) to attach to your toot.
  • Into the Value field, we’ll insert the reference ID from the previous HTTP block ‚Äď specifically, you want to insert the data: id.

You can now run this scenario and, if everything’s working end-to-end, your image will be posted to Mastodon! I’ve followed my own steps here and, indeed, @Philateleology now happily lives on Mastodon. Happy tooting!

This is specific but you can probably use these instructions to get tools like Zapier or IFTTT to do the same thing. Get at me on Mastodon if you build a cool bot.