Creating Your First Slack Bot in Nodejs

If you are looking for a good introduction to the world of AI or are simply wanting to be trendy and cool, Chat Bots are the answer. In this post, I’ll get you started with a quick chat bot in Nodejs.

Getting Permissions

First, we will need to let Slack know about our bot and grab an API key.

  1. Go to this URL:
  2. Enter a username for your bot. We will call this one, testbotScreen Shot 2018-04-14 at 12.05.38 AM.png
  3. Scroll down to Integration Settings and grab the API key. I’ve deleted the one below ūüėõScreen Shot 2018-04-14 at 12.07.52 AM.png

Creating the Nodejs Bot

Alright, now that we have our creds, let’s create a bot.

First, create a new directory, initialize a project and install the slackbots library.

mkdir my-first-bot
cd my-first-bot
npm init -y
npm install slackbots --save
touch index.js

Alright! So now, let’s initialize the bot using the API key from above. In the ‚Äčindex.js add the following.

const SlackBot = require('slackbots');
const bot = new SlackBot({
    token: 'xoxb-346962835285-DOgbKAsXuvn5tTYOrlUOyJYB', 
    name: 'My Bot'

Sweet, now let’s create an on start message to let everyone know we are connected.

bot.on('start', () => {
    bot.postMessageToChannel('general', 'Hello!');

The above will post a hello message in the general channel when the bot has connected. One last thing we shoul do for this tutorial is respond to users. Let’s start by adding the on message event.

bot.on('message', (data) => {
Next, we will check if the user is messaging our bot and then reply to them if so.
bot.on('message', (data) => {
    if (msg.username === 'My Bot') {
       bot.postMessage(, 'Yo!);
The above checks if the username of the message is to our bot and sends a reply to the user in the same channel.
And that’s all for this tutorial! I’ve been making quite a few bots lately, so I’ll be posting more about how to create bots and the different feature you can use. Let me know if you have any questions.