Automatically posting to Mastodon

Volume 7, Issue 10; 15 Jan 2023

A few random notes and observations about how this weblog toots.

A week or so ago, when I decided to tidy up a bit, I automated posting a Mastodon update when a new post is made to this weblog. In particular, I did this because I wanted to make it possible to automatically post photographs to Mastodon by adding them to my weblog.

For a normal post, the weblog “toot” is just the title of the post, the abstract, and a link to the post. Easy peasy. To post a photograph, and have the photograph appear in the toot, I have to work a little harder.

I download the image from, extract the alt text, and send it to Mastodon with the toot as a media attachment. Except sometimes it doesn’t work and that’s really frustrating.

I assumed it was a bug in toot, even though I’d written a patch that made sure the media was successfully uploaded. I decided it would be easier to just use directly than it would be to debug toot. Besides which, writing a Python script to do some work and then execute another Python script as a subprocess seemed kind of silly.

I used mastodon-local to spin up my own instance. That way I wouldn’t be spamming my account with dozens of test toots. The first time I tried to spin up the docker containers, redis fell over, but it worked fine the second time.

I wanted to create a second account, a non-administrator account, for testing but my laptop isn’t configured with an MTA listening on port 25. I worked around this by grabbing the Sendria container and adding it to docker-compose.yml:

    image: msztolcman/sendria:v2.2.2.0
      - "1025:1025"
      - "1080:1080"
      - external_network
      - internal_network

By configuring Mastodon to use mastomail:1025 to send mail, I could capture the “verify email address” message and complete the account creation dance.

I discovered a couple of interesting things. First, I don’t think missing attachments is a bug in the command line client. The Mastodon API just drops media attachments sometimes. I can see from the logs that I successfully uploaded an image and got back an image ID. I can see that I successfully used that image ID in the toot. And I can see from the reply that the toot has no media attachments. 🙁

I changed my script so that it compares the media attachments it thought it was making with the media actually attached. If they don’t match, it immediately deletes the toot. Then it waits two seconds and tries again. It’ll do this three times before giving up. Hopefully that’s the problem sorted.

The other interesting thing is that you can’t reuse media ids. I figured if I knew that I’d successfully uploaded an image, I would keep the ID around and reuse it rather than clogging up the server with another upload of the same image. Apparently not. AFAICT, attempting to use the same media ID in more than one toot just doesn’t work. The server ignores the ID.

Anyway. I think it all works now. If it does the right thing with this post, I’m going to go take a walk on the beach, photograph some things, and see if those get posted correctly too!


Please provide your name and email address. Your email address will not be displayed and I won’t spam you, I promise. Your name and a link to your web address, if you provide one, will be displayed.

Your name:

Your email:


Do you comprehend the words on this page? (Please demonstrate that you aren't a mindless, screen-scraping robot.)

What is nine times seven?   (e.g. six plus two is 8)

Enter your comment in the box below. You may style your comment with the CommonMark flavor of Markdown.

All comments are moderated. I don’t promise to preserve all of your formatting and I reserve the right to remove comments for any reason.