Learn How to make a bot for Slack with node.js



Why did the chicken cross the road?

Slack is a very popular communication and collaboration tool for teams that’s used by many well known companies. Slack is a bit like IRC, but improved with better design and some very useful features, like file sharing, persistent history, and more. And much like IRC, Slack also supports bots.

Slack bots range from useful little helpers that make you more effective at your job, to bots that update you on sports and let you play silly little games. Visit the Slack App Directory to see more examples of bots that have been created for Slack.

In this tutorial, I will show you how to use node.js and Botkit to create your very own Slack bot. In the process, we are going to make a bot that knows a few jokes about chicken crossing the road 🙂

Getting set up
First, we will need to install node.js. You can follow the official download and installation guide for your particular operating system.

There is a few options when it comes to hosting your bot, but most of them are paid, starting at $5-7/month. This is not a bad price, and most services, like Heroku, have a limited free tier. But for this tutorial we are going to go with OpenShift, a cloud application development and hosting platform. OpenShift offers a limited free tier that puts your app to sleep if it gets no traffic for 24 hours, but you can upgrade to the free Bronze plan to avoid this limitation.

After you sign up for an OpenShift account, go ahead and create a new node.js app. You don’t need to change anything other than the Public URL for your project.

To be able to interact with your OpenShift project, you should also download the rhc command line tool.

With rhc installed, let’s run the following code to download our newly created node.js app:

Let’s also install Botkit, a node.js library for building Slack (and Facebook Messenger) bots.

Assuming you already created a new Slack team, log in as your team’s administrator and go to your Apps and integrations page. On the Custom Integrations tab click Bots, and then Add Configuration.


After you name your bot, you will receive your bot’s API Token. You will need to add this token to your OpenShift app like this:

If you want to also try running the bot from your local machine (you can use the command node server.js once you’re done with the updates below), you will have to set the environment variable on your computer as well.

I recommend looking up the instructions for your particular operating system. For example, on Linux, you could run the following command:

You can run rhc env list -a NAMEOFYOURNODEJSAPP to make sure the token was added correctly to your app. Once you confirm that, open the file server.js and replace the content of the file with following code:

Let me quickly go through the code and explain what each part does.

Loading node.js modules typically looks like this:

For some reason, this doesn’t seem to work with the current version of Botkit (0.2.0), hence the above solution.

The get_response() function returns a random element from an array of prepared responses. We are going to use these to reply to users asking it a question about chicken crossing the road.

You could also save these as a module and then require it, but for simplicity of this tutorial, I am doing it this way.

Here we are initializing the bot, and finally —

Now, save the server.js file and deploy your updated app.

And you’re done! Go and /invite your bot to a channel, and ask it why did the chicken cross the road 🙂

Your bot can do a lot more, of course! Check out the Botkit GitHub repo for documentation and relevant links, and also some examples of what other people made with Botkit.

One quick note: OpenShift seems to expect your app to run on the 8080 port and returns a failure otherwise. This is not really a big issue, though, as the bot will still run correctly.


Please enter your comment!
Please enter your name here