Originally published at: http://www.sitepoint.com/getting-started-slack-bots/
Slack is a popular tool for encouraging better communication among team members. It comes with a slew of helpful features and services including third-party integrations and webhook support. Slack webhooks can be fun and useful, and they’re easy to set up. In this tutorial we’ll set up a simple server using Node.js and Heroku that will respond to your team’s messages from Slack as a “bot.” Familiarity with JavaScript and Git will be helpful. If you haven’t used Git before, take a look at Git for Beginners to get started. Access to a Slack account is also assumed, but don’t worry – they’re free!
Creating a New Slack Integration
From the Slack web interface, open the menu in the top left corner next to your team’s name and select Configure Integrations. Here, you can see a dashboard with all the integrations available to you. Scroll to the very bottom under DIY Integrations & Customizations, and add an Outgoing WebHook. Read the short description and then click Add Outgoing WebHooks Integration.
You will see the Outgoing WebHook configuration and an example of what the request and response payloads should look like. Our first bot will simply greet the user when called. To do this, we will allow the hook to listen for the Trigger Word “hello” on any channel. We don’t know the URL it will be posting to yet, so we will come back to this page later.
Creating a Web Server
We’ll be using Express 4 on Node.js to build a simple web server.
Create a new directory for your app, and run npm init
to generate your package.json
file. In your package.json
dependencies, include "express": "^4.x.x"
and "body-parser": "^1.x.x"
. The body-parser
package will be used to parse the payload sent from Slack. Create the server file named app.js
. Here, we will require
the necessary modules, add the body parser middleware, error handler, and a test route. Then, we tell the server to start listening.
var express = require('express'); var bodyParser = require('body-parser');var app = express();
var port = process.env.PORT || 3000;// body parser middleware
app.use(bodyParser.urlencoded({ extended: true }));// test route
app.get(‘/’, function (req, res) { res.status(200).send(‘Hello world!’) });// error handler
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(400).send(err.message);
});app.listen(port, function () {
console.log('Slack bot listening on port ’ + port);
});
Run the server with node app
and verify that visiting http://localhost:3000
responds with “Hello world!”.
Writing the Bot Logic
Create a file named hellobot.js
This module will be where our bot logic lives. It will include one exported function that returns a message to Slack with status code 200
. We’ll start by parsing the data that Slack sends to our server, and then we’ll respond with a properly formatted payload of our own. To do this, let’s revisit our Outgoing WebHook under Configured Integrations. The Outgoing Data example shows us all the info Slack sends when an Outoing WebHook is triggered.
You can see that some of the data can be useful for authentication or routing, but for now we’re just going to focus on the user_name
property. Using this value, we can send a personalized greeting back to the user.
module.exports = function (req, res, next) { var userName = req.body.user_name; var botPayload = { text : 'Hello, ' + userName + '!' };// avoid infinite loop
if (userName !== ‘slackbot’) {
return res.status(200).json(botPayload);
} else {
return res.status(200).end();
}
}