Herfststraat.
Adding Micropub to your IRC bot
In last weeks IndieWeb newsletter I read a blogpost by Sven about having notifications for webmentions and such via IRC. He described more or less the setup I have too: using Aaronâs TikTokBot framework, running on the same machine as a private IRC server for it and your IRC bouncer. In my case, the machine is a Raspberry Pi in my living room, but Svenâs guide describes more or less what I did too, so please check that out.
I divided my webmention notifications into webmentions and silomentions (thatâs backfeed from services like Bridgy and OwnYourSwarm) and I also get notifications for logins and Micropub requests, so I can keep track of whatâs happening on my blog. But thatâs just expanding the notifications. I added some other functionality I wanted to talk about.
Hey bot, please like this post
The one thing that made me post on my site the most, was adding Micropub support. There are various Micropub clients out there that you can use to just write a blogpost, to post a like, to import checkins from Swarm or what have you. But since itâs so simple (once you know how), I also made a lot of Micropub clients out of Workflow, Paw or, in this case, my IRC bot called Bop.
I can now say like https://example.com/a-post
in the same channel my notifications come in, and Bop will post a like-post on my site. My site will then post the webmention and if the other site accepts it, it will be shown as a like under the post, just like that.
Please refer to Svenâs blogpost to learn how to set the bot up. I also assume you already have a site that supports Micropub. I will go into obtaining an access token a bit, but that might be a tricky part and depends on how your Micropub endpoint works.
Getting your bot hooked
Sven mentions the file âhooks.ymlâ, which contains hooks: []
for him. The hooks-file defines the things the bot will respond to, and since Sven only want to receive notifications, not talk to his bot, he does not need hooks. But we do. Replace the contents of the hooks file with the following:
hooks:
- match: "^(like|bookmark|rsvp|yes|no|maybe|interested) (https?\\:\\/\\/[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,}(\\/[^>,\\\")\\s]*)?)"
url: "http://localhost:8000/micropub.php"
channels:
- "@seblog"
- match: "^(tweet|note|fb) (.*)"
url: "http://localhost:8000/micropub.php"
channels:
- "@seblog"
As you can see, I like to match for a URL to support like, bookmark and rsvp, and for anything to support notes on your own site, Twitter and Facebook. Feel free to change the syntax to your own needs.
Under channels, I defined @seblog
, which means âevery channel in the server seblog
â. That name comes from your config.yml
, and itâs a safety measure, so that when I connect my bot to Freenode also, it wonât accept likes from other people. I am the only person connected to the private IRC network Seblog, so that will be fine.
Creating the Micropub.php
As you saw, the hooks define a URL that will be called by the bot. I specified that to be on localhost:8000 and point to micropub.php. In your TikTokBot folder (or where you want, really) create a folder called âserverâ and a file within it called âmicropub.phpâ. Letâs start with the following to test it out:
<?php
header('Content-Type: application/json');
echo json_encode([
'content' => 'Hi, you called for a Micropub?'
]);
Then go to the newly created folder and run the command php -S localhost:8000
. This starts up a webserver from that folder, so if you now go to your private channel and say note This is a test
, you will see that the bot responds. If you say something that does not match the regex, the bot will stay silent.
Give me my data back
If we want to send data to our Micropub endpoint, we need to make sure that our bot has that data. Letâs make the bot echo whatever we said, so we know that he heard us.
TikTokBot gives us a JSON object of the message via the POST data. Long story short, you can get it by using the following:
<?php
header('Content-Type: application/json');
$message = json_decode(file_get_contents("php://input"));
$full = $message->content;
$action = $message->match[0];
$param = $message->match[1];
echo json_encode([
'content' => "Hi, you said '$full', you wanted to $action $param?"
]);
Now, if you say like https://seblog.nl/2017/08/19/6/micropub-irc-bot
, the bot will respond with Hi, you said 'like https://seblog.nl/2017/08/19/6/micropub-irc-bot', you wanted to like https://seblog.nl/2017/08/19/6/micropub-irc-bot?
. Try it out, and different actions too, so you know it works!
Make sure not to echo the full message, because since the bot listens to itself, that message will trigger itself again, resulting in the bot repeating the message over and over again. (Just restart the bot if that happens.)
Sending a POST request
A Micropub request is, in the end, just a POST request. Hereâs a little helper function to send one for you. Just start with this code at the top of your file, right after <?php
.
function send_post($url, $fields, $bearer = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($bearer) {
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$bearer]);
}
$body = http_build_query($fields);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$resp = curl_exec($ch);
if(!$resp) {
$resp = 'Error: '.curl_error($ch);
}
curl_close($ch);
return $resp;
}
Next, I have a plain and dirty if-chain, to determine the action. You can add this right after you defined $action
and $param
:
if ($action == 'like') {
$fields = [
'like-of' => $param
];
} elseif ($action == 'bookmark') {
$fields = [
'bookmark-of' => $param
];
} else exit();
send_post('https://seblog.nl/micropub', $fields, 'xxx');
Make sure to replace that URL with your own Micropub endpoint, and xxx
with your own access token. Iâll talk about obtaining one at the bottom of this post.
I donât have the bot say anything after this, because the bot will give a notification once a Micropub request is made, which is enough feedback for me. You could also return whatever send_post()
returns, I leave that up to you.
More post types
As you can see in the code above, you can just add fields to the array. Here are some other elseif
s I use.
} elseif ($action == 'tweet') {
if (strlen($param) > 140) {
exit(json_encode(['content' => 'Text is '.strlen($param).' chars!']);
}
$fields = [
'content' => $param,
'syndicate-to' => 'https://brid.gy/publish/twitter'
];
} elseif ($action == 'fb') {
$fields = [
'content' => $param,
'syndicate-to' => 'https://brid.gy/publish/facebook'
];
} elseif ($action == 'rsvp'
or $action == 'yes'
or $action == 'no'
or $action == 'maybe'
or $action == 'interested') {
$fields = [
'in-reply-to' => $param,
'rsvp' => $action == 'rsvp' ? 'yes' : $action,
];
if (strpos($param, 'facebook.com')) {
$fields['syndicate-to'] = 'https://brid.gy/publish/facebook';
}
} elseif ($action == 'note') {
$fields = [
'content' => $param
];
See how easy that is? Please make up new ones and tell me about it!
Iâm particularly proud of the tweet
one, which will, thanks to the exit()
, not post the tweet unless it fits the 140-chars-rule.
Please note that the use of the syndicate-to
fields (or the newer mp-syndicate-to
) depend on your own website. This only gives my webserver the order to syndicate, it does not syndicate by itself. If your server does not know how to syndicate to Twitter, it will not work, but explaining how to do that is itâs own tutorial.
In real life, I actually have note
set to 'private' => true
and 'audience' => 'http://seblog.nl/
, so itâs a private note. But: 1) I donât expect many people to support private posts, 2) how many people are interested in posting things to their website only they themselves can see? and 3) I want to change the 'private' => true
to 'visibility' => 'private'
, or something like that.
Thatâs it!
There you go, a customisable Micropub chatbot. Please double check that itâs only doing itâs thing on your private IRC server, and not on any public servers where your bot might lurk (if you use any). Have fun liking and posting!
POST-scriptum: Obtaining an access token
The question âwhat do I replace âxxxâ with?â is a hard one to answer, because it totally depends on how your site handles access tokens. Most Micropub clients ship with an IndieAuth flow to obtaining one, but since weâre making this one ourselves, we have to get one manually.
You can do this by following the steps on the wiki, if you have a way to send POST requests. You can also log in to a Micropub client like Quill, which shows you the access token it got, and use that. Since this all depends on your own site, it might offer an easier way, or it might not.
Update 2017-10-04: In an earlier version of this post, I did not set a Content-Type: application/json
-header, so the bot didn't actually respond. Setting that header at the top of your file will fix that.