in technology

How to tweet your toots with PHP and IFTTT

Note: To use this tool, you need access to a server where you can upload a PHP script that will run when queried over HTTP.

Mastodon is a relatively new decentralised social network I’ve been trying out recently (you can find me at @ldjb@status.ldjb.uk). It’s a lot of fun and I’m excited about the possibilities it opens up.

A number of us are currently taking part in #woollyweek, in which we log out of Twitter entirely for an entire week. In fact, in the longer term, I’d like to phase out my use of Twitter entirely and supplant it with Mastodon.

That, however, raises the question of how your Twitter followers will be able to find you on Mastodon. You can tweet out a link to your Mastodon profile and pin that tweet, which is probably a good idea, but even pinned tweets are unlikely to attract much attention in the longer term. What you really want to do is to tweet your toots.


What do I mean by tweeting your toots? I mean automatically posting your toots to Twitter. It’s not enough to simply tweet the contents of the toot – you’ll want to link back to the toot, too, so people can find your Mastodon profile.

Some people have found an easy method just with IFTTT, but this didn’t meet my requirements. The issues are:

  • The tweets don’t link back to the original toot;
  • The tweets include boosts (I only want to tweet out my own toots);
  • The tweets include replies (I don’t want toots intended for select individuals to appear on all my Twitter followers’ timelines);
  • Any @mentions in a toot will be converted into a Twitter @mention (I don’t want random Twitter users to be receiving notifications left, right and centre).

So I had to find my own solution.


Alright, after that rather long preamble, here’s what I actually did.

I could have solved this problem with a running process, but I didn’t want an overly-complicated solution. Instead, I went with something simple – a PHP script I can just upload to a server, and integrate that with IFTTT.

Essentially, I created a proxy for the Atom feed generated by Mastodon. It strips out entries for anything other than my own toots (no boosts), it strips out toots beginning with @, and it replaces all remaining instances of the @ symbol with [AT], so as not to trigger any Twitter @mentions.

The code is a bit messy as I just wanted to get something up-and-running, but here it is:

<?php

define("ATOM_FEED", "https://mastodon.social/@Gargron.atom");
define("NEW_STATUS_BY", "New status by");

header("Content-type: application/atom+xml; charset=utf-8");

$data = new SimpleXMLElement(file_get_contents(ATOM_FEED));

$toRemove = [];

for ($i=0; $i<count($data->entry); ++$i) {
	if (strpos($data->entry[$i]->title, NEW_STATUS_BY) !== 0) {
		array_unshift($toRemove, $i);
	}
	else {
		$data->entry[$i]->content = strip_tags($data->entry[$i]->content, "<p><br>");
		if (strpos($data->entry[$i]->content, "<p>@") === 0) {
			array_unshift($toRemove, $i);
		}
		else {
			$data->entry[$i]->content = str_replace("@", "[AT]", $data->entry[$i]->content);
		}
	}
}

foreach($toRemove as $i) {
	unset($data->entry[$i]);
}

echo $data->asXML();

You’ll want to replace the URL to the Atom feed at the top of the script and upload the PHP file to your server. Also, if your Mastodon instance is not set to English, you’ll need to update the NEW_STATUS_BY constant to the appropriate string (and possibly adapt the code a little – how to do so is left as an exercise for the reader).

Then you can create a new IFTTT applet, use the “New feed item” trigger in the Feed channel with the uploaded PHP script, and set the Action to “Post a tweet” in the Twitter channel. I set the tweet text to:

{{EntryContent}}
{{EntryUrl}}

That way, both the toot text and a link to the toot are included in the tweet.

Since Mastodon has a character limit of 500 and Twitter has a character limit of 140, longer toots will be truncated to fit. The link to the toot will always remain intact, however.

And there you have it. By the way, I am releasing the code above into the public domain, so do with it as you like!


EDIT: By the way, if you get some sort of error message about failing to enable crypto, you can try adding your Mastodon Atom feed to Feedburner, then pointing the PHP script to the Feedburner feed created.