Action

Post to Mastodon with !Content Warning

Posted by agiletortoise, Last update over 1 year ago

Example action demonstrating using the Mastodon API via script to post a public status update.

This example looks for a line in the drafts starting with “!”, and, if present, uses that line as a content warning for the post.

IMPORTANT: Before using this action, edit the first to “Define Template Tag” steps to enter the Mastodon instance host and credential identifier to use.

For more examples, see our Mastodon Integration Guide

Steps

  • defineTemplateTag

    name
    mastodon-host
    template
    your.mastodon
  • defineTemplateTag

    name
    credential-id
    template
  • script

    // grab host-id from tags
    let host = draft.processTemplate("[[mastodon-host]]")
    let credentialID = draft.processTemplate("[[credential-id]]")
    
    // validate values
    if (host.length == 0 || host == "your.mastodon") {
    	alert(`Mastodon host must be configured to run this action.
    
    Edit action and set your Mastodon host name (like "mastodon.social") as the template value in the first "Define Template Tag" action step.
    `)
    	context.cancel()
    }
    
  • script

    // create Mastodon instance
    let m = Mastodon.create(host, credentialID)
    
    if (draft.content.trim().length == 0) {
    	alert("No content found")
    	context.cancel()
    }
    else {
    	// setup content to use
    	// remove any lines starting with !, and use those as a CW
    	let contentWarning
    	let lines = draft.lines
    	lines = lines.filter(function(value, index, arr){
    		if (value.startsWith("!") && value.length > 1) {
    			contentWarning = value.substring(1).trim()
    			return false
    		}
    		return true
    	})
    	let content = lines.join('\n')
    	
    	// post to the /statuses endpoing
    	if (!m.updateStatus(content, contentWarning)) {
    		console.log(`Post Failed: ${m.lastError}`)
    		context.fail()
    	}
    	else {
    		console.log(`Posted to Mastodon`)
    	}
    }

Options

  • After Success Archive , Tags: 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.