Geek

How To Build A Slack Chatbot In Node.js Using Botkit


Recently we launched a new product named BotArena, the world’s most informative bot store. So, we thought why not start telling our readers more about the bots and how to make one from the scratch.

Chatbots are gaining popularity after the recent F8 conference where Facebook opened its door for the developers to build chatbots for messenger platform. If are willing to spend a little time, you can make your own bots easily and automate different things.

In this how to guide, we will build a hello world chatbot for Slack using Botkit — a popular and open source bot development kit written in Node.js.

1. Create a bot for your team in Slack:

  • Go to https://slack.com/apps and click on Build your own at the top right corner.
  • Select Something just for my team and click on Make a custom integration.
  • Click on Bots under Build a Custom Integration menu.
  • Give your bot a username and click on Add bot integration. I used the username as botmaker_hello_bot.
  • On the Bot detail page, note down your API Token. You can customize your bot by giving name and uploading a profile pic.
  • Finally, click on Save Integration to save your bot profile.

2. Install BotKit:

  • Open your terminal and run npm install botkit  to install BotKit.
  • Create a js file e.g. slack_hello_bot.js and add following code:
var Botkit = require('botkit');

var controller = Botkit.slackbot();

var bot = controller.spawn({

  token: "<your token>"

})

bot.startRTM(function(err,bot,payload) {

  if (err) {

    throw new Error('Could not connect to Slack');

  }

});

controller.hears(["Hello","Hi"],["direct_message","direct_mention","mention","ambient"],function(bot,message) {

  bot.reply(message,'Hello, how are you today?');

});
  • Run your bot node slack_hello_bot.js
  • Open your Slack team and start talking to your bot.


3. How it works?

  • Botkit simplifies the process of designing and running bots that live inside Slack.
  • It provides a semantic interface to sending and receiving messages so that developers can focus on building core logic and not to worry about API integrations.
  • In the above code, we have initialised a slack bot by providing an API token and used bot.hears()  to hear for users message and reply back with bot.reply()

In the next how to guide, we will build a hello world chatbox for messenger using BotKit. Stay Tuned!

If you wish to explore latest bots for different platforms, or submit your own bot, don’t forget to check out BotArena — the world’s most informative bot store.

Did you find this article helpful? Don’t forget to drop your feedback in the comments section below.

To Top

Pin It on Pinterest

Share This