Bots with Nodejs

So when I first came across bots when using Slack and HipChat I've ignored them without a thought but i recently attended a local javascript group and the exercise was building a Slack bot. This opened my eyes and I'm not the only one with Facebook also talking about bots.

The bot that we're going to write will be to give the bot a list of ingredients and for it to do a google search for a recipe. Since I like to write code here's how I did it.

Creating the bot in slack

You will require to be logged into your team and be an administrator to create the integration and generate the API token. To create the bot you will be able to add the name of the bot.


And then you can see the API token that is required when we're writing the code later on. Also in this screen you can give the bot a emoji or upload a picture and some other customisations.


Now for the code.

Firstly i'll be using the Botkit and google packages so these can be installed with

npm botkit google --save

'use strict';
var Botkit = require('botkit');
var google = require('google');

var controller = Botkit.slackbot({
    debug: false

// connect the bot to a stream of messages
   token: '<insert here>',

controller.hears('hello',    ['direct_message','direct_mention','mention'],function(bot,message) {
    bot.reply(message,'Hello yourself.');

In my instance I would be using the API token from image 2 for the token.

So what's going on here. We are requiring in the Botkit package and then creating a controller (or you could think of the controller as the bot).

Then we are telling the controller to create a connection with our token and to start listening for messages.

The next thing that we do is tell the controller to listen for a message. with hears(). Hears takes two arguments and a callback.

The first is what to listen for - 'hello' - and the second is an array of types.

The bot.reply() will send a message back to to the requesting stream.

With the bot essentially being a node.js app the scope of what can be done is open to what any node.js app can do. Here is a link to HestonBlumentBot which is a fun way to find a recipe with any ingredients that you currently have in.