Slack

Introduction

Hello, guys! My name is Lydia. And now I want to guide you to the world of Slack bots using Dokkur.

This tutorial will show you how to create a simple “Hello World” Slack bot on Node.js and deploy it to Dokkur in several minutes. These instructions will also give you enough information to make the most out of Dokkur.

Be sure that you already have:

  1. Dokkur account.
  2. SSH keys added to your account.
  3. Node.js and npm installed locally.
  4. A Slack team to abuse.
  5. A banana milkshake to celebrate at the end (obligatorily!).

Create a simple Slack bot

Note that if you don't want to copy the code from here, you can clone this GitHub repository.

We won’t create a lot of files. First of all, we need to create a package.json file just to tell Dokkur that this app is a Node.js app.

Create a package.json file which contains these information:

{
"name": "dollurBot",
"version": "0.0.1",
"description": "Simple testing Node server",
"main": "app.js",
"author": "Lydia Khashina",
"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.

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 Dokkur Slack channel! I\'ll be your guide.'
};
// Loop otherwise..
if (userName !== 'slackbot') {
return res.status(200).json(botPayload);
} else {
return res.status(200).end();
}
});

As you see, 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 and create a JSON response which will be then send back to Slack!

Set up Dokkur Toolbelt

In this step you will install the Dokkur Toolbelt that gives you you access to the Dokkur Command Line Interface (CLI).

After installation you can use dokkur login command to log in with your email and password you set when creating Dokkur account.

Deploy the Slack bot

To deploy the bot to Dokkur you need to create a Procfile file with this line in it:

web: node app

This tells Dokkur to start a new Web context and run the node command (which you have also run local) to start your server!

To finally push our code to Dokkur 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.'

Then you need to do the following.

For deployment you will use web-interface and Dokkur Toolbelt. Follow these steps:

  1. Open website, go to Personal apps (left menu bar)
  2. Click ⨁ button to open New App interface
  3. Enter the app’s name (Caution! Use latin lowercase and numbers, e.g. myapp007), choose available and convenient instances for hosting, pick Custom App template and press Create App button.

Congrats! Your app has easily been created. Now you need to deploy it. Just do it!

  1. Go to Settings page, copy the app git url from the Git URL section.
  2. Switch to Dokkur Toolbelt and execute this command:

    git remote add dokkur git_url
    Instead of git_url paste your app's git url.

  3. Push your app:
    git push dokkur master
    If you want to deploy another branch, run:
    git push dokkur yourbranch:master

Hell yeah! Your app is deployed! Open yourappname.dokkurapp.com in your browser.

If you have started to celebrate the success with the banana milkshake, then stop it! Don’t be in a hurry. We need to do more steps to integrate our bot with your Slack team.

Integrate the bot with Slack

This is the last thing we should do. For this step you need to have a Slack team. If you don’t have one, create it, it’s free.

Go to App Directory in your Slack team menu.

App Directory

Click on Build button.

Build

Chose Make a Custom Integration in Something just for my team section. There you will see different kinds of ingeration. We need to choose Outgoing WebHooks.

Build a Custom Integration

Great! Now we are ready to configure a new integration. You need to paste necessary info in these fields. Channel - choose any channels you want or create a new one. Trigger Words - enter the words that will be used as a trigger for the bot to start working. URL - paste your dokkur app URL that you can find in Domains section in your app’s menu.

Integration Settings

Below these fields there are several fields that helps you customize your bot: upload an icon or choose an emoji and create a name for it.

Save settings and go to Slack!

Type whatever trigger you’ve defined in settings and voilà! It may look like this.

Slack Example

Congrats! Now you can drink your banana milkshake!

Thank you for your attention. If you still have some questions, feel free to contact me at lydia@dokkur.com.