Chuck Norris jokebot for Slack

In this tutorial, we will build a Slackbot using JavaScript and Node.js that reaches out to public APIs and tells random jokes within Slack. We will use the Slackbots.js library to achieve this.

Step 1.

First, you will have to register to Slack.com and create a new workspace. After you do that you will have to register your application. To do that go to https://api.slack.com/apps and choose your workspace that you created in the previous step. In Add features and functionality select Bots. Give it a name and username. Leave Always Show My Bot as Offline to OFF and click Save Changes.

On the left menu click on Basic Informations and under Install your app to your workspace click on Install Bot to workspace button and authorize it. In Display informations you can add short description and image for your bot.

On the left menu go to Oath premissions and leave it open because we gonna need Bot User OAuth Access Token or write it down.

Step 2.

Now we can write some code so let’s start. Create a working folder and open it in your editor. Open your terminal and we will create package.json file using the command

 

Answer all the questions. Since we are working with Node.js and we will work with third-party API. We want to be able to send a request. That’s why we will install axios and also slakbot.js. To do that type in terminal following command

 

In package.json delete test under scripts and type

 

and save.

Step 3.

Create index.js file and we will bring in slack bot and axios.

Remember that Bot User OAuth Access Token in Step 1. Go to the website and greb it because we gonna need it now.

Replace token with your token that starts with XOXB- and give it a name to whatever you want.

When we start a bot we want him to give some message so we gonna type

General means that this message will be shown on general chat in Slack and as always you can choose whatever message you want. With the last line, we gonna catch some errors if there is some.

Next, we gonna work on messages that our bot will receive. First, copy the code and we will talk about what it’s doing.

The first function gets if statement if there are no messages to return else if there is a message to run a function called handleMessage that receive parameter data.text

Here is the fun part. In the second function called handleMessage in if statement we can choose words that will trigger a function that tells a joke. So if user type message that includes words like yes, joke or another the chuckJoke will run. Make sure to make space before word because in sentence words are separated by space and you can include as many words as you wish just follow the pattern.
In else statement, if someone is rude to our bot, he will get a message to and you will see it later 🙂

Now, let’s call API to get that jokes. The API that we will be using is https://api.icndb.com/jokes/random and if you copy and paste this link into your browser you will get a response with random Chuck Norris joke and every time you refresh the page you will get a different one. So this is how we call the API

Using Axios we are getting information from an API, then we are getting a response and from that response, we will create const joke that is equal to res.data.value.joke. Res is response and data is data that we received and if you look at browser where you put this API you will see that joke is under value. So we will type value.joke.

Now, let’s handle that users who are bad to our bot.

What this does it`s that it will send a gif to a chat. To change img od gif you just have to change image_url link I have choose this famousse Chuck Norris roundhouse kick

And we are done. Now lets test it. Run

npm run start

You should receive a message from your bot. Now ask him to tell you a joke. To select him use @botname_that_you_give If everything goes well you should have something like this

Here is a full code:

And here is the link to source code. https://github.com/SekeNikola/Slack-ChuckNorrisJoker-Bot

Hope you learned something. If you have some question or suggestions leave in a comment or write to me on any social media you want.

Have an awesome day

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.