Action

Post to Mastodon (Direct)

Posted by agiletortoise, Last update about 1 year ago - Unlisted

NOTE: Although still functional, direct integration with Mastodon is now available and recommended. See integration guide for examples.

Post the content of the current draft as a status update on Mastodon.

To use this action, you will need to create an application in your Mastodon account to obtain an access token. To do so:

  • Go to your Mastodon account preferences page.
  • Click on “Development”
  • On the Development page, click “New Application” and configure values. You will not be using OAuth, so you need only configure a name, and make sure the application has at least the write:statuses scope selected.
  • After saving changes, click on the application details and copy the “Your access token” value. This will be used as the token value the first time you run this action.

With that information complete, run the action. The first time you run it, you will be prompted to enter the token you obtained above, and the base URL of your Mastodon instance, which should be something like https://mastodon.social/(include trailing slash). These values will be remembered for subsequent use of the action.

Steps

  • script

    // setup and request credentials
    let credential = Credential.create("Mastodon", "Enter the base URL of your Mastodon instance (like: https://mastodon.social), and a valid access token for an application you have created in your account.");
    
    credential.addTextField("host", "Base URL");
    credential.addPasswordField("token", "Access Token");
    credential.authorize();
    
    const host = credential.getValue("host");
    const token = credential.getValue("token");
    
    // make post
    let endpoint = `${host}api/v1/statuses`;
    var id_key = new Date().toISOString();
    
    // configure status details
    // for more info on options see:
    // https://docs.joinmastodon.org/methods/statuses/
    let data = {
    	"status": draft.content,
    	"visibility": "public"
    };
    
    // create and post HTTP request
    var http = HTTP.create();
    
    var response = http.request({
    	"url": endpoint,
    	"method": "POST",
    	"data": data,
    	"headers": {
    		"Authorization" : "Bearer " + token,
    		"idempotency_key" : id_key
    	}
    });
    
    if (response.statusCode == 200) {
    	let d = JSON.parse(response.responseText);
    	console.log("Posted to Mastodon, ID: " + d.id);
    }
    else {
    	console.log("Mastodon Error: " + response.error);
    	context.fail();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.