Simple Telegram's bot with Node.js
Tags: telegram, chatbot, node, javascript
Published: 2017-11-06 10:34:46

---
title: Simple Telegram's bot with Node.js
published: true
description: Easy yet powerful snippet for building a chatbot with custom triggers and replies
tags: telegram, chatbot, nodejs, javascript
---

First article here, yeah!

Today we are going to build an awesome Telegram's bot.
You will need a little Node.js and [Telegram's bot platform](https://core.telegram.org/bots/api) knowledge.
Yup, I won't teach you something you can already read somewhere else.

We'll use [Telegraf.js](https://telegraf.js.org/) as a *wrapper/framework* for Telegram API. It's a pretty awesome library which simplifies a lot of annoying tasks you will encounter while developing bots.
**Plus, it is [middleware powered](https://en.wikipedia.org/wiki/Message-oriented_middleware).**

Enough talk, let's begin. Our bot will have a list of keywords, or commands, which will trigger a reply. The bot reply can be a simple text message, a sticker, or an animated GIF:

![A cool Batman GIF](https://res.cloudinary.com/zaphodias/image/upload/f_auto/v1509899478/batman.gif)

## 1. Creating the bot account
Assuming you are already familiar with Telegram, contact the [@BotFather](https://t.me/BotFather) and create your bot account.

Please note the token (something like `410509983:AAF3kbJrAGKIrW6ceOdrUg-zLlk5Tuy-NhU`), we'll use it later.

Now you have to *disable privacy mode*, in order to make the bot able to read *all* the messages in groups and not just commands.

## 2. Init the directory
Easy peasy.
```bash
$ mkdir coolbot
$ cd coolbot
$ npm init
# I'm assuming you already have Node & NPM installed. If not:
# https://nodejs.org/en/download/package-manager/
```
Now answer the few questions NPM will ask and - done. We can install Telegraf.js now.

To do so:
```bash
$ npm install --save telegraf
```

You will have a package.json file in your folder, edit the `scripts` part to add a 'start' script:
```javascript
"scripts": {
  "start": "node main.js",
  "test": "echo \"Error: no test specified\" && exit 1"
},
```

## 3. Let's code
![Coding is fun](https://res.cloudinary.com/zaphodias/image/upload/f_auto/v1509903780/coding.gif)

I think the code it's pretty easy to understand, I added some comments to make it even easier.

We want the bot to have a list of trigger, and for each trigger a reply the bot will send. The reply can be a text message, a sticker, or a GIF.

As a pretty feature, we also want that if I reply to John's message using one of the triggers, the bot should send his message replying to the John's message.

*(I know, bad English is strong here.)*

These triggers will be defined in a separate file later. For now, let's have a look at the core of the bot:

{% gist https://gist.github.com/Pitasi/8da6c0d11eb1ff6fc8a64e594f5c4f64 %}

In this file we are using:
 - `bot.command` to define what to do in case of someone writing `/list`,
 - `bot.on` to manually define what to do in case of a Telegram event  (any text message)
 - `bot.startPolling` to make the bot start asking Telegram for incoming new messages periodically.

## 4. Triggers and replies
Okay now you can customize your bot by creating and editing the `replies.js` file:

{% gist https://gist.github.com/Pitasi/09b4c754601fb85f418311a06d379258 %}

To get stickers and GIFs IDs, I suggest sending them to [@jsondumpbot](https://t.me/jsondumpbot) and look for "file_id" (pay attention to **not** use file_id of the "thumb"!).

## 5. Running the bot
![RUN!](https://res.cloudinary.com/zaphodias/image/upload/f_auto/v1509903869/stallone.gif)

Easiest part ever:
```bash
$ TOKEN=410509983:AAF3kbJrAGKIrW6ceOdrUg-zLlk5Tuy-NhU npm start
```
Obviously, use your token instead of the sample one.

## The end
This is it for now. I'm not used to make tutorials and that kind of stuff, so I'll appreciate any comment.