If you’ve ever used Slack before, I’m sure you’ve interacted with bots. A bot is a type of Slack app designed to interact with users via conversation. We use bots to post messages, send DMs, upload files and such just by mentioning it in the channel. In this post, I’m going to create a simple Slack bot that fetches random Chuck Norris jokes from an API and displays it to your channel. The bot will basically be waiting for messages with trigger words and sending the random joke.
I will use a Node.js library called SlackBots.js to operate the Slack API. For fetching the random jokes I will use ICNDb. You can just about build any bot you wish and use any API to fetch the data you want.
Create a workspace
If you don’t have a Slack workspace you can create one here and fill in the required fields. You can either add your own channel or use the #general channel. A channel is a single place for a team to share messages, tools, and files.
Create a Slack App
Next, we have to create a Slack App for us to access the Slack API. Click here to create a Slack app and give it a name. I will name mine
jokebot. On the Basic Information menu you will be listed with a couple of features to add to your app. Choose bots as this is what we are interested in.
On the bot users menu, set your display name as
jokebot and click on
Add Bot User then
Save Changes. Leave the rest as is.
Next, click on Basic Information menu and you’ll see that ‘Add Features and Functionality’ is checked. Click on
Install your app to your workspace. Click on the
Install app to workspace button which will basically ask you to authorize the app to your workspace. On the
Display Information card, add a description and app icon for your bot. You can refer to this documentation to set up.
Installing node libraries/packages
Using your preferred Text Editor, I personally use and recommend Visual Studio Code, open a terminal and type
npm init. Fill in the required responses to create a
- lists the packages your project depends on
- specifies versions of a package that your project can use using semantic versioning rules
- makes your build reproducible, and therefore easier to share with other developers.
After that is completed, we need to install slackbots and axios. Slackbots was the library I referred to earlier while Axios is a promise based HTTP client for the browser and node.js. Type
npm i slackbots axios on the terminal to install both libraries.
package.json file and make this change on the
echo key and value. Delete it and replace it with the code below in your
“start”: “node index.js”
Building the bot
On your Slack app dashboard, click on OAuth & Permissions and copy the
Bot User OAuth Token. Create a file and name it
index.js. This will house the logic of our bot and fetch the API. The final file should look like this:
You should paste your Slack token that you copied above in the
token key as its value when we declare the new Slackbot. The bot posts the message
Laugh out loud with @jokebot! to the #general channel. The base URL for fetching our jokes is http://api.icndb.com/jokes/random.
The bot listens for the string
chucknorris and responds with a random joke upon calling
@jokebot chucknorris in the general channel. After making all the changes, run your application by using the command
npm start. You’ll then see that jokebot becomes activated and is online.