Skip to content

Tutorial: Telegram bot

Tim Ermilov edited this page Mar 30, 2016 · 1 revision

This tutorial will guide you through creation of the pipeline that will act as a basic Telegram 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: Telegram source

First, we'll create a source component that will connect to Telegram and will listen for /exynize command from users.
To simplify the creation, we'll rely on node-telegram-bot-api npm package.
It'll also listen for incoming post requests and send them as replies (this is needed because Telegram only allows one connection using same token).
Here's how the code will look:

import telegram from 'node-telegram-bot-api';

const subj = new Rx.Subject();
export const routeHandler = (res) => subj.onNext(res.body);

export default (token, obs) => {
    const bot = new telegram(token, {polling: true});
    subj.subscribe(inc => bot.sendMessage(inc.fromId, inc.response));
    // Matches /exynize [whatever]
    bot.onText(/\/exynize (.+)/, function (msg, match) {
        const fromId = msg.from.id;
        const text = match[1];
        obs.onNext({fromId, text});
    });
};

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

Step 2: Search processor

We'll reuse a simple search component from Slack bot tutorial.

Step 3: Telegram response processor

Next, we'll create a Telegram response processor.
It'll use the POST requests to the first Telegram component we'd created.
Here's how the code will look:

import request from 'superagent';

export default (url, data) => Rx.Observable.create(obs => {
    const response = data.Abstract ? data.Abstract :
            data.RelatedTopics[0] ? data.RelatedTopics[0].Text :
            false;
    request
    .post(url)
    .send({fromId: data.fromId, response})
    .end((err) => {
        if (err) {
            obs.onError(err);
            return;
        }

        obs.onNext(data);
        obs.onCompleted();
    });
});

This processor will send response to the first Telegram component we'd created and then 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 Telegram source component, you'll need to provide your Telegram API token. You can create one by following the guide on Telegram website.

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

When adding Telegram response processor - make sure to add URL of your pipeline as parameter so that it knows where to send POST requests.

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 send a slash command to the bot in Telegram, you should see the results of the search appear as a response to you in Telegram almost immediately.