Integrating Slack with custom API

Team Scope
thescopecom
Published in
5 min readMar 6, 2015

It has been a while since our team switched to Slack for internal communication. This reduced drastically the number of daily emails (I would say by 95%) and improved the way we share thoughts, ideas, important messages and personal feelings among each other.

One of the great things about Slack that I love are integrations. There is almost “a plugin for everything”: for our Nagios and NewRelic monitoring systems, for our JIRA tasks and, in case we would need, for Jenkins build&deploy, Github repository, Mailchimp mails and Twitter account. Moreover, one can also feel more “at home“ with customized icons, that might be used everywhere in the chat once set up (have a look at our nice Niuws and Newscron logos in the picture below).

[caption id=”attachment_2800" align=”aligncenter” width=”300"]

Giacomo on Slack

Custom Niuws and Newscron icons in Slack[/caption]

What if one is really eager for more and the current plugins are not enough? For eager users like me there is the possibility of “Customized Slack Commands”. This allows to bring “everything into a [Slack] plugin”. In fact, the “Customized Slack Commands” can be found among the Slack plugins (see picture below).

[caption id=”attachment_2799" align=”aligncenter” width=”500"]

Customized Slack Commands among Slack Plugins

Customized Slack Commands among Slack Plugins[/caption]

Slack commands are nice “input texts” every user can write in any channel to perform some actions (see picture below). My favourite one is “/remind me at time to do something”, which generates a slack notification at the given time to remind me of that particular thing I had to do (e.g. calling someone or getting a bus to go home earlier). The great thing is that Slack offers the possibility of adding new “custom” commands on top of the standard ones.

[caption id=”attachment_2798" align=”aligncenter” width=”499"]

Commands in Slack

Commands in Slack[/caption]

I have to admit that I discovered this functionality only recently but it was the perfect time for it: it was just after the launch of Niuws (our new app for iOS and Android, to get high quality hand-curated news) and we were discussing about how we could have an overview on curators activities, to answer very simple questions like “how many followers does a box have?” or “when was the last box updated?”.

This was intriguing me for several reasons: we already had two public dashboards for performance controlling that were pulling data from our database, however the needed information was not suited for such dashboards. Moreover the information had to be accessible to different members of the team (at any time) without knowledge to SQL or database. Last but not least, I was the one daily reporting that information to our Slack #general channel and, after a while, I was starting to forget to do it. Therefore I was motivated to find an alternative solution.

Since I was already daily posting that information as snippets (text files) in Slack and I just discovered the “Customized Slash Commands”, I decided to make that custom integration my pet projects for some evenings.

The first step was to understand how to integrate the statistics code in our REST API with Slack. Slack offers an easy and fast way to “talk” with API: it was just a matter of deciding the custom Slack command name, the url to the API and eventually the method POST or GET to receive the call.

[caption id=”attachment_2797" align=”aligncenter” width=”500"]

Customise Slack commands

Customise Slack commands[/caption]

We are using Jersey in our backend to manage the API calls, so I had to read and process the parameters Slack was sending to the server. Luckily Slack is pretty open and clear on this: for every call one can determine which Slack user is executing the command (user_id and user_name), which command is executed (command) and with which parameters (text). The last very important parameter is the token. Having a token for each command allows for more security and prevents from undesired applications or user to access that API.

[caption id=”attachment_2796" align=”aligncenter” width=”500"]

Slack output parameters

Slack output parameters[/caption]

[caption id=”attachment_2795" align=”aligncenter” width=”500"]

Reading Slack parameters with Jackson in Java

Reading Slack parameters with Jackson in Java[/caption]

After that, I was then ready to extend the API code. I just added in the JAVA code a token verification control and a user check to give permissions to visualize data only to some users in our team. I then executed the database queries and formatted the output as a string with some tabular spaces here and there and wrapped around the slack formatting backticks ``` .

[caption id=”attachment_2794" align=”aligncenter” width=”300"]

Adding Backticks

Adding Backticks[/caption]

Surprise surprise: this was enough to get the desired result! In the image below an example of the output for the /boxstatistics custom command executed in Slack.

[caption id=”attachment_2793" align=”aligncenter” width=”500"]

slackbot

Slack custom command output (example)[/caption]

This was however just a start and I extended the API multiple times to support more input options and today there are still some things to adjust … However, what I reached with this project was a simple and effective way of getting data from a custom API in Slack, avoiding complex access management system (I am just filtering usernames from logged users in Slack) and saving time on building interfaces. And for a startup of few people and lots of work such as Newscron (and many others), this means also to save time (and consequently money) for other tasks.

--

--

Team Scope
thescopecom

Handkuratierte Nachrichten und Fachinformationen, zusammengestellt von über 75 Expertinnen und Experten.