A Simple Sloppy Slack Slash-command Show-how (with Meetup API)

(We know “Show-how” isn’t a real word. Alliterate all the things!)

Checking out BotList lately got me curious about that world, so yesterday I decided to sit down and create what is probably the simplest form of bot — a Slack slash-command of my own. For those of you unfamiliar, in Slack you have the ability to create a full-fledged Bot that you can interact with (or that can serve up push notifications), or just a simple command like /whenisthenextmeetup that we’re going to build.

This whole “show-me” should take you about 10 minutes if you want to follow along. There’s a bit of very sloppy php code at the end you can use (although you’ll probably want to clean it up quite a bit if you leave it in production). You’re going to want the following:

  • A Slack account with at least one organization that won’t mind you doing this (ask, or better — ask if you can make them something)
  • A Meetup account
  • A web host account where you can drop a single .php file

I built this for HNKansai , a Hacker News group based in the Kobe/Osaka/Kyoto triangle (join us!). We recently started using Meetup for our monthly get-together and co-host Sacha Greif suggested this idea might be helpful.

Your first step is to actually find the link on the Slack website to get started. Don’t laugh — I’m not the brightest bulb in the chandelier, but it took me round and round clicking of the sort typically associated with Google APIs to figure out a generic starting point I could share. The key is to know which of your organizations you want to make this on before you get started:

https://[yourTeam].slack.com/apps/build/custom-integration

So for me, for example, it was https://hnkansai.slack.com/apps/build/custom-integration (which won’t work for you unless you are a member of HNKansai - put your own team in). After that it is all downhill.

We’ll use Slash Commands, obviously

We’ll use Slash Commands, which takes us to a starting point asking for the command we want to use. Type ‘/whenisthenextmeetup’ and click the Add Slash Command Integration (don’t worry about how long it is — we’ll be adding autocomplete in the next step).

You’ll be taken to new page with a lot of useful information at the top that you really don’t need to know about right now. About half-way down the page you will see Integration Settings:

URL will not yet be filled in — that’s a coming step

The Command is self-explanatory; the URL is where we are going to put the link on our hosted account where our actual script is that does something. In our case, that “something” is going to be to call the Meetup API with a usergroup slug and have it return to us the next event information, which we will parse and pass back to Slack.

First, while we’re here, let’s give it the rest of the information we want to show in slack. Customize Name is your bot name, and the icon will be its avatar. The autocomplete part is important — it will not only save typing but let users see usage instructions. Mine looks like this in production:

Autocomplete for our command

Save that when you’re done and let’s go grab our API key from Meetup.

When you log into Meetup you’ll find the usual dashboard at https://www.meetup.com/meetup_api/; you can find your API key at the top. That’s all we really need from here, but the endpoint we’ll be working with is https://www.meetup.com/meetup_api/docs/:urlname/events/#list which you’ll find handy if you want to modify our results later.

Now we need some code for our api calls. If you’re an experienced programmer, this is the point where you ask yourself, “PHP…Meetup…gotta be something out there, right?” and run to Mr. Google, who tells you about this nice little single file library: https://github.com/FokkeZB/Meetup Not only did I not bother to use composer to pull it in (just grabbed the “raw” version), I even stole part of his example code and created my own endpoint here:

// Place this at http://XXX.codebyjeff.com/bots/whenisthenextmeetup/index.php as noted in the Slack settings
// from  https://github.com/FokkeZB/Meetup
require 'meetup.php';
$meetup = new Meetup(array(
'key' => 'YOUR-API-KEY-FROM-MEETUP'
));

$groups = [
'ro' => 'React-Osaka',
'owd' => 'Osaka-Web-Designers-and-Developers-Meetup',
'ccc' => 'Coffee-and-Coding-Conundrum',
];

if (empty($_POST['text'])){
$url = 'Hacker-News-Kansai';
}
elseif (!empty($groups[$_POST['text']])){
$url = $groups[$_POST['text']];
}
else{
$url = $_POST['text'];
}

$response = $meetup->getEvents(array(
'group_urlname' => $url
));
// If response is empty, send back some little message. I didn't bother.

$date = new DateTime('@' .$response[0]->time / 1000);
$date->setTimezone(new \DateTimezone('Asia/Tokyo'));

echo $response[0]->group->name . ' - ' . $date->format("D, M j H:i") . ' at ' . $response[0]->venue->name . ' ' . $response[0]->event_url. PHP_EOL;

I told you it was simple & sloppy. (It’s also in production — how’s your weekend project?) About half of the code is just letting people use shortcuts for some of the other local groups many of us belong to. The Meetup actually requires the group’s slug (ex: ‘Osaka-Web-Designers-and-Developers-Meetup’) which would be a bit tiresome to type out, so I made a shortlist to allow 3-letter codes instead, with a default.

The meeting start time is stored in milliseconds, so be sure to convert. It is also GMT, but since we are all in Japan I did it (say it together!) the Simple, Sloppy way. It looks like this in Slack:

This is three separate requests, using “ccc”, default and “owd” as arguments

That’s really all there is. Very simple code which can be written in any web language you want and a few dashboard settings. Feel free to use this “as is” or anyway you care to. Next time I’ll get into some bots that are a little more interactive than this. Enjoy!