Action

Post: Twitter+Mastodon

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

Post draft to both Twitter & Mastodon. This combines a Twitter action step, with a script that posts to the Mastodon API.

To use this action, you must create an application in your Mastodon account to obtain an access token. To do so (if you have already configured the Post to Mastodon action, you do not need to repeat these steps):

  • 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

  • twitter

    template
    [[draft]]
  • 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) {
    	console.log("Posted to Mastodon, ID: " + response.responseData.id);
    }
    else {
    	console.log("Mastodon Error: " + response.error);
    	context.fail();
    }

Options

  • After Success Archive , Tags: tweet, mastodon
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.