How to schedule posts with Mastodon

The other day I wanted to post several links and posts on Mastodon. I didn't want to flood my followers' timeline with my face several times in a row. So I thought, I'd love to have a way to schedule posts on Mastodon 🤔

So far there doesn't seem to be a way to do it out of the box and I haven't read this feature being actively worked on. I used to use it a lot back when I was on Twitter using TweetDeck 🐦

So can this be done with GitHub Actions?

Yes ✅

🤓 How does it work?

Using the Python API wrapper for Mastodon to connect to your instance and create a post.

Using GitHub Actions to run every X minutes to check if a new post should be published.

💡 Proof of concept

I created a small repo (lacking of features) but works as a proof of concept for me.

The repo has a JSON file where I write the posts I want to defer or schedule ⏳

The GitHub Action runs every hour, takes 1 element of the JSON file, and posts it.

The GitHub Action updates the repository removing the item just posted to Mastodon, so the item doesn't get posted twice.

Sure, it's not real scheduling, it's just deferring to the next time it runs. But you could add a field for time. GitHub Actions allows you to run an action every 5 minutes if you wanted to, so your JSON of posts could have post content and post time. Your Python script could check the time and if it's over and publish all the posts that match it 🔥

👨‍💻 Check out the code

Here's the initial version of the code.

  1. Open the JSON file
  2. Read the JSON
  3. Take the first item
  4. Post it
  5. Update the JSON file removing the posted item
fileName = 'posts.json'

with open(fileName, 'r') as f:
  posts = json.load(f)['posts']

  # Only post the latest available
  if len(posts) > 0:
    # We only want to post 1 item at a time
      status = posts[0],
      visibility = "public",
      language = "en"

    # Remove the item we just posted from the list of posts
    posts = posts[1:]

    # Update the file with the posts
    with open(fileName, 'w') as f:
      f.write(json.dumps({'posts': posts}))Code language: Python (python)

🍴 Fork my repository

Play with it, full working code here:

Leave a Reply

Your email address will not be published. Required fields are marked *