Action

Post Poll to Mastodon

Posted by agiletortoise, Last update over 1 year ago

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

This action uses the first line of the draft as the poll content (e.g. the question) and any remaining lines as poll options.

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)
    
    // setup content to use
    let lines = draft.lines
    let content = lines.shift() // first line in poll content
    // loop over remaining lines to create poll options
    let options = []
    for (let ln of lines) {
    	if (ln.length > 0) {
    		options.push(ln)
    	}
    }
    const expiresIn = 60*60*24 // 24 hours
    
    // post to the /statuses endpoint
    let path = "/api/v1/statuses"
    let data = {
    	"status": content,
    	"poll": {
    		"options": options,
    		"expires_in": expiresIn
    	},
    	"visibility": "public"
    }
    let response = m.request({
      "path": path,
      "method": "POST",
      "data": data
    })
    
    if (!response.success) {
    	console.log(`Post Failed: ${response.statusCode}, ${response.error}`)
    	context.fail()
    }
    else {
    	console.log(`Posted to Mastodon: ${response.responseData["url"]}`)
    }

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.