Building A Slack Bot and then packaging it as a Slack App — Slack Ping Bot

Let me start with a short background on what Inspired me to build this app, who knows, you might just see the reason why you need the app installed on your slack channel.

Last week, the company I work with, iQube Labs, had to move hosting for a Web App we developed for a client from our server to its own self hosted server. If you are familiar with this process, you would know that it would require updating the nameserver for your domain name to that of the new hosting servers, this takes about 24–48 hours to update. (Well for my network provider, it took about 72 hours 🙂).

So, after doing the switch, I noticed that we had to ping the domain name several times within the speculated 48 hours to know if the nameserver updates had propagated. This involved us pinging on our local computers and pasting the results on slack. I felt we could make it easier, if we could just ping the domain from inside our slack channel; this led to slack-ping-bot.

Now, let’s get down to code. I chose to use node.js to develop the pinging bot simply because I like using node.js for anything realtime, but you can use any language of your choice.

I started out by building a module that handled pinging of requested domains. It would be a simple function that would take a message (domain or ip) as input and also an output function which would be invoked with the response text. A snippet of the ping-bot.js is shown below (full code can be found on github):

So you would use it on import like this:

This way, I could (re-)use the bots functionality in any context and it also made writing tests for the bot easier. Cool, right!

Now, for the interesting part - creating the slack bot. I started by diving into the Slack API documentations, two links were particularly useful:

You should go through the above links, they are really helpful and it is worth mentioning that you get conversant with Slack’s API in order to understand all the capabilities and limitations of a bot.

I’ll recommend you use a library from here when developing your slack integration, so you don’t reinvent the wheel. In my case, I used Botkit.

Install botkit using npm:

npm install --save botkit

Then you can instantiate a botkit process as shown below:

Botkit provides various ways of interacting with a slack user in realtime, you can see more on their page, but for the purpose of my bot, I’m only interested in direct mentions and direct messaging events. So, using botkit and my ping-bot module from earlier on, I can do this:

The full functioning source code can be seen here.

Now what is left is finding a place to host the app, so it would be readily available. Once again, I consulted slack for options and I found this page. I decided to go with BeepBoop, because they support node.js and it is ridiculously easy to setup.

All I had to do was signup and hookup my github repo to a bot project. All configurations are done via bot.yml and Docker file in the repo. I have default configurations setup in my repo here. So BeepBoop pulls all files from the connected github repository and builds the app using the environment defined in the Docker file.

For node.js apps, just make sure you define the appropriate command for your ‘start’ script in your package.json file and then go to your apps dashboard on BeepBoop to configure your SLACK_TOKEN to be used by your bot.

Voila! Your bot is live and ready to mingle.

Packaging The Bot as An App

What I’ve illustrated so far has been a custom integration, which would only work for a single slack team. To make other slack teams enjoy the bot integration, you need to package it as an app that can be installed.

One thing I noticed is that there isn’t much tutorials on how to do this (other than the Slack API’s page I shared earlier), so I’ll give a step by step guide as to how to do it.

Because I chose BeepBoop to host my slack bot, they made it remarkably easy to convert it to an App. Here are the steps as to how:

  • Go to your BeepBoop App Dashboard and Click on the Settings Tab, under this section you will see an option with a toggle to Enable App as a Slack App.
  • Toggle the Enable Button ‘On’, and follow the four steps of the instructions (more like tutorials) shown to you. These steps will walk you through creating an App on Slack with Instructions on what to put in each of the required fields.
  • Once done with creating your App on Slack, you should add a Bot User to your App. This can be done under the Bot User Section of your Slack App page.
  • Then, copy your Client ID and Client Secret over to BeepBoop. Yeah, I know it is weird to copy your Client’s Secret over to BeepBoop, but this is necessary for BeepBoop to handle OAuth of teams for you.
  • Next, under the Settings Tab in your BeepBoop Dashboard, Enable Multi Team Socket mode. This is required to work with BeepBoop’s Resourcer API, which allows you to interact with Slack Teams that add your bot to their team through socket connections. More information about the BeepBoop’s Resourcer API can be found here.

Now, you just need to modify your Bot code to work with the Resourcer API. Good thing BeepBoop has a library for this, that also integrates well with Botkit - beepboop-botkit.

So, you install the beepboop-botkit using npm like this:

npm install --save beepboop-botkit

And then you modify the botkit code we wrote above to include beepboop-botkit like this:

And that is it. Push your update to github and Beepboop will build a new version of your App, that can work with multiple Slack Teams.

Another cool thing about BeepBoop is that they even help you build a Webpage for your Slack App that can be customized from your bot.yml file.

What are you waiting for?

To experience this App in action on your Slack Team, head over to Slack Ping Bot’s Webpage here and add it to your team.

Or just Click this button to add Ping Bot to your Slack team.

If you have any further questions or comments, don’t hesitate to ask in the comments section below.

And don’t forget to Recommend this post, if you liked what you read.

Show your support

Clapping shows how much you appreciated Perfect Makanju’s story.