Automatically post to Mastodon or other remote APIs

The ECA Helper module provides an action to make an arbitrary HTTP post to any URL. That's all that's necessary to post to Mastodon from Drupal, if you have a Mastodon account. I've been using this functionality to automatically post these advent calendar posts for the past week.

Now I'm adding the "Door image" to the post automatically as well -- with functionality that is not (quite) released yet. In version 2.0.2-beta, the ECA Helper module gained a nice feature -- it now automatically turns the response into an array, if it's recognized as JSON.

It still doesn't post files, at least not in the way Mastodon expects -- so I patched in some functionality to make it work. If you apply that patch to 2.0.2-beta, you can use this to post images and toots to Mastodon, from whatever Drupal content you want. See the diagram for the overview, and below for the details.

Steps to post to Mastodon

  1. In your Mastodon account, add an application key.
  2. Set your application key as a token, "mastodon_token".
  3. Use the http request action from ECA Helper to post to the media endpoint, with these settings:
    1. Method: POST
    2. URL: https://drupal.community/api/v1/media (drupal.community is my Mastodon server, replace that with yours)
    3. Data:
      file: "[entity:field_door_image:entity:path]"
      description: "[entity:field_door_image:alt]"
      (the file token should have the stream path, e.g. "public://2024-12/image.png", and the description is used as the image alt tag. Note that this field is likely to change depending on how the patch issue develops).
    4. Data serialization: File
    5. Headers: Authorization: "Bearer [mastodon_token]"
    6. Name of response token: media_response
  4. Use a second http request action to post the actual message, with these settings:
    1. Method: POST
    2. URL: https://drupal.community/api/v1/statuses (Note the change in the endpoint, from "media" to "statuses").
    3. Data:
      {"status": "[message]",
      "media_ids": ["[media_response:json:id]"]
      }
    4. Data serialization: JSON
    5. Headers: Authorization: "Bearer [mastodon_token]"
      Content-Type: "application/json"
    6. Name of response token: response

... and that's pretty much all you need to post from Drupal to Mastodon!

I did also start to create an automated post the same way to BlueSky, but that turns out to be a bit more complicated - authentication tokens are far more short-lived, and links and hash tags are not automatically connected. BlueSky really needs an additional library to make work.

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <blockquote cite> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h1> <h2 id> <h3 id> <h4 id> <h5 id> <p> <br> <img src alt height width>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.