DEV Community

Antonio Pitasi
Antonio Pitasi

Posted on • Updated on

Simple Telegram's bot with Node.js

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 knowledge.
Yup, I won't teach you something you can already read somewhere else.

We'll use Telegraf.js 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.

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

1. Creating the bot account

Assuming you are already familiar with Telegram, contact the @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.

$ mkdir coolbot
$ cd coolbot
$ npm init
# I'm assuming you already have Node & NPM installed. If not:
Enter fullscreen mode Exit fullscreen mode

Now answer the few questions NPM will ask and - done. We can install Telegraf.js now.

To do so:

$ npm install --save telegraf
Enter fullscreen mode Exit fullscreen mode

You will have a package.json file in your folder, edit the scripts part to add a 'start' script:

"scripts": {
  "start": "node main.js",
  "test": "echo \"Error: no test specified\" && exit 1"
Enter fullscreen mode Exit fullscreen mode

3. Let's code

Coding is fun

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:

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:

To get stickers and GIFs IDs, I suggest sending them to @jsondumpbot and look for "file_id" (pay attention to not use file_id of the "thumb"!).

5. Running the bot


Easiest part ever:

$ TOKEN=410509983:AAF3kbJrAGKIrW6ceOdrUg-zLlk5Tuy-NhU npm start
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

corusm profile image

you can also replace the process.env.TOKEN with the token in "your_token"

yahyakhanmit profile image

Thank you I was finding this for the whole morning.