Skip to content

Tutorial: Slack bot

Tim Ermilov edited this page Feb 18, 2016 · 3 revisions

This tutorial will guide you through creation of the pipeline that will act as a basic Slack bot that will search for things. It assumes that you have either registered at cloud version of Exynize or deployed your own copy of the platform, and finished the "Hello world" tutorial.

Step 1: Slack source

First, we'll create a source component that will connect to Slack and will listen for direct mentions.
To simplify the creation, we'll rely on botkit npm package.
Here's how the code will look:

import Botkit from 'botkit';

export default (token, obs) => {
    const controller = Botkit.slackbot({debug: false});
    controller.spawn({token}).startRTM(err => err && obs.onError(err));
    controller.on('direct_mention', (bot, message) => {
        obs.onNext(message);
    });
};

This component will keep dispatching mentions as they come in without ever completing (so, it has to be stopped manually).

Step 2: Search processor

Next, we'll create a simple search component that will search for most relevant thing using the input text.
We'll use existing npm package called node-ddg-api that wraps around Duckduckgo search API.
Here's how the code will look:

import {DDG} from 'node-ddg-api';

const ddg = new DDG('my-custom-ddg');

export default (data) => Rx.Observable
    .return(data)
    .flatMap(input => Rx.Observable.create(obs => {
        ddg.instantAnswer(input.text, {skip_disambig: '0'}, (err, res) => {
            if (err) {
                obs.onError(err);
                return;
            }

            obs.onNext(Object.assign({}, data, res));
            obs.onCompleted();
        });
    }));

This processor will get the best search match for the text field of incoming data object, then it will append results to the data and return the new data object. You can test this by entering {"text": "google"} into data field in Exynize editor and hitting "Test" button.

After test succeeds, hit the "Save" button to save your new processor component.

Step 3: Slack response processor

Next, we'll create a Slack response processor.
It'll use the same API as the first Slack component we'd created.
Here's how the code will look:

import Botkit from 'botkit';

export default (token, data) => Rx.Observable.create(obs => {
    const controller = Botkit.slackbot({debug: false});
    const bot = controller.spawn({token}).startRTM(err => {
        if (err) {
            obs.onError(err);
            return;
        }

        const result = data.Abstract ? data.Abstract :
            data.RelatedTopics[0] ? data.RelatedTopics[0].Text :
            false;

        bot.reply({
            type: data.type,
            channel: data.channel,
            user: data.user,
            text: data.text,
            ts: data.tx,
            team: data.team,
            event: data.event,
        }, result ? `Here's what I found: \n> ${result}` : `Sorry, couldn't find anything :(`);
        obs.onCompleted();
    });

    return () => {
        bot.closeRTM();
    };
});

This processor will send response to the Slack and will dispatch the same data it receives. After test succeeds, hit the "Save" button to save your new processor component.

Step 4: Basic renderer

Since we don't really need a renderer for this pipeline, we'll use basic string renderer from "Hello world" tutorial for debugging purposes.

Step 5: Pipeline assembly

Now that all the components have been created, we need to assemble them into a pipeline.

When adding Slack source and reply component, you'll need to provide your Slack API access keys. You can create one on Slack website.

Search processor does not require any configuration - just adding it is sufficient.

Make sure to test the pipeline by pressing "Test" button before saving it using the "Save" button.

Step 6: Running and viewing results

Now that you've assembled, tested and saved your new pipeline, you can start it and view the rendered result by clicking "Web" button next to pipeline name.

Once you do a direct mention to the bot in Slack, you should see the results of the search appear as a response to you in Slack almost immediately.