With the growing exodus of people to various Mastodon instances in the wake of the purchase of Twitter by serial boofhead Elon Musk, people will be starting to look for integration tools to start exploiting the Mastodon API.
My first dabble into it has been to update my personal management console to post my links to my chosen Mastodon instance, as well as Twitter for the time being at least.
Here’s a simple PHP function I wrote this morning that allows for simple toots to be posted via scripts or other tools you might like to do this for.
function mastodon_toot ($user_token,$mastodon_url,$toot_content)
{
$post_data = Array("status"=>"".@str_replace("\'","▒~V~R~V~R~@~Y",$toot_content)."","language"=>"eng","visibility"=>"public");
$post_headers = ['Authorization: Bearer '.$user_token.''];
$curl_handle = @curl_init();
curl_setopt($curl_handle,CURLOPT_URL,"https://".$mastodon_url."/api/v1/statuses");
curl_setopt($curl_handle,CURLOPT_POST,1);
curl_setopt($curl_handle,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl_handle,CURLOPT_HTTPHEADER,$post_headers);
$curl_execute = @json_decode(@curl_exec($curl_handle));
$http_code = "".@curl_getinfo($curl_handle,CURLINFO_HTTP_CODE);
@curl_close($curl_handle);
if ($http_code <> "200") {
$result_code = "HTP";
$result_message = "cURL returned HTTP code '".$http_code."' when attempting post to the '".$mastodon_url."' instance!";
$result_data = NULL;
} else {
$result_code = "AOK";
$result_message = "The flilm is okie dokie!";
$result_data = Array("api_response"=>$curl_execute,"toot_id"=>"".$curl_execute->id."");
}
return(Array("result_code"=>$result_code,"result_message"=>$result_message,"result_data"=>$result_data));
}
There are three required parameters – $user_token, $mastodon_url, and $toot_content.
To make use of the function, create an application within your Mastodon account. In most cases you’ll find this in “Preferences -> Development“, then click “New Application“.
Fill in the fields – just use the URL of your website for the requested URLs. We won’t be doing any two-way communication between your application and Mastodon, so they won’t matter in this instance. Save your new application.
Take note of the “Your access token” value – this is what needs to be passed to the function as $user_token. Remember, you must keep this token secret and secure – as anyone with the token will be able to post to Mastodon as if it were you.
The value of $mastodon_url should just be the name of your Mastodon instance – eg: for me, it is “aus.social”.
The value of $toot_content is exactly what it sounds like – whatever you want to post to Mastodon via your script.
The first line in the function does a little cleanup on the content, just to remove crappy characters – I’m not 100% sure this is needed for Mastodon, but I carried this over from my corresponding Twitter tweet function, and it seems to not cause any issues here – so it’s there!
And that’s it. The function returns an array with some diagnostic information – (did it/didn’t it work?) – and the ID of the toot posted, if it was successful.
While the above function is written in PHP, given I’ve chosen to use PHP’s cURL library, if your chosen scripting language is not PHP it should be pretty straightforward to use the cURL library for whichever language you prefer, to come up with an equivalent script.
Over time I’ll delve more into what the Mastodon API can achieve, but in the meantime, happy automated tooting!