A Simple Sloppy Slack Slash-command Show-how (with Meetup API)
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:
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, 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:
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:
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
$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',
$url = 'Hacker-News-Kansai';
$url = $groups[$_POST['text']];
$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->time / 1000);
echo $response->group->name . ' - ' . $date->format("D, M j H:i") . ' at ' . $response->venue->name . ' ' . $response->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:
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!