Slack is the rising star of the Silicon Valley. A simple tool like IRC, but in a complete new dress. Slack improves communication in teams everywhere around the world. Have you already started to use Slack?
There are a lot of superlatives about Slack out there, but let’s just keep it as an amazing tool to not only communicate with each other (which is the obvious feature) but also to dramatically increase productivity and efficiency.
Are you using Slack?
Slack offers the possibility to program your own Slackbot who can listen to events, be part of the conversation and do just whatever you can imagine a bot could do. There is not a lot of information out there about this topic, that’s one of the reasons why I am working on a online course called Mastering Slack.
Make sure to subscribe the prelaunch list if you want to get awesome information about how to save time and money with Slack.
Right now we will start with a simple Bot, which is written in some NodeJS running on an Express Server, which we will then push to Heroku.
Sounds awesome to you?
Let the fun begin!
Setup the Server
We don’t need a lot of files for this, so it’s quite easy to follow I hope. Start with a blank folder and create a package.json which contains these information:
{ "name": "devdacticBot", "version": "0.0.1", "description": "Simple testing Node server", "main": "app.js", "author": "Simon Reimler", "license": "MIT", "dependencies": { "express": "^4.x.x", "body-parser": "^1.x.x", "request": "2.56.x" } }
We define our bot app and also 3 dependencies, which you can then install by running
npm install
This will create a folder node_modules with the modules, you should not change anything in!
The next part is the server which will host our bot. Create a new file inside your folder named app.js and insert:
var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var port = process.env.PORT || 1337; // body parser middleware app.use(bodyParser.urlencoded({ extended: true })); // test route app.get('/', function (req, res) { res.status(200).send('Hello world!'); }); app.listen(port, function () { console.log('Listening on port ' + port); });
We create some variables for our app and add URL encoding. Now this will launch a simple Express server which listens on port 1337, plus a testing route which responds with “hello world”. Give it a try!
First, launch your app with:
node app.js
This should print “Listening on port 1337″ so open a new Terminal and test:
curl 'localhost:1337/'
This is a simple Bash command to grab information from a URL. If you get the correct respons, we can move on.
Creating the Slackbot
The next part is really simple, so go ahead and open your app.js and append:
app.post('/hello', function (req, res, next) { var userName = req.body.user_name; var botPayload = { text : 'Hello ' + userName + ', welcome to Devdactic Slack channel! I\'ll be your guide.' }; // Loop otherwise.. if (userName !== 'slackbot') { return res.status(200).json(botPayload); } else { return res.status(200).end(); } });
This is a route which will be called once a POST request was received at /hello. In that case we extract some information about the sending user from the request (this is where we finally interact with Slack stuff!) and create a JSON response which will be then send back to guess what: Slack!
Deploying the Slackbot to Heroku
For now this only runs local, but (for this example) we need to have the Slackbot be reachable from the Internet. You will see why later on.
The (in my eyes) fastest way to do so is to simply power up a Heroku server which can run our app. If you haven’t used it yet, go ahead and create a free account for Heroku or check out my previous tutorial about Deploying your Ionic app to Heroku.
I also recommend to install the Heroku toolbelt as we use this in the next steps.
To launch an app on Heroku, we need a Procfile so create this one next to your app and just insert:
web: node app
This tells Heroku to start a new Web context and run the node command (which you have also run local) to start your server!
To finaly push our code to Heroku we need to create a Git repository, but this can also be done in just 3 steps :
git init git add . git commit -m 'Initial commit.'
Now we just need one last command which will push the code to Heroku and start building our app (you can follow the building progress inside your Terminal where you run the command):
heroku create git push heroku master
Was this to fast are you still with me?
Those were maybe a lot new tools for some of you, so if there are any problems let me know!
If not, congratulations! Just a few more steps until you can chat with your bot. For now, make sure to copy the URL where your app is running. This should be somewhere in the last lines of the response from Heroku, or otherwise inside your Heroku dashboard.
Configure the Slackbot inside the Slack settings
The last thing we need to do is to setup Slack to use our bot. Create a team on Slack if you don’t have one, it is free. If you already have one, go ahead and select integrations inside your account area.
Now scroll down to Outgoing Webhooks which looks like this:
and configure a new hook. We want to set a Trigger Word and a URL, so add your word (mine was hello) and the URL of your Heroku app + “/hello” which was our POST route we defined before. This should finally look something like this:
The result
Go to your Slack chat, type whatever trigger you defined and the Bot should become active to greet you:
What is this Hook shit doing?
Well, every time someone now starts with the word “hello” in one of your channels, the custom route we specified will be called along with some data about the message and the one who said it. That’s when our bot gets active and responds with what we defined.
Not really hard, is it?
It might be a bit tricky, but in general it’s not that hard. Now go ahead and let the bots rule your Slack team!
I would be really interested if you have worked with Slack and what’s your opinion, so please share your thoughts below!
See a video version of this article below.
So long,
Simon
The post Say Hello To Your First Slackbot appeared first on DevDactic.