How To Build a Telegram Bot With Node.js in Under 3 Minutes – Best Telegram

How To Build a Telegram Bot With Node.js in Under 3 Minutes

It’s probably easier than you think

How to build a Node.js telegram bot
Source: the author

Telegram is a WhatsApp alternative — in my opinion, a pretty good one. It is free to use is considered very secure. But the best:
We can program our own bots!

Here is how to build one with Node.js.
Let’s put loose & build our own Telegram Bot!

Getting started

First, we need to register our bot. We can do that in the Telegram app by contacting BotFather.

Just type “botfather” in the search box, and choose the suggestion with the blue tick.

Node.js telegram bot creation
Source: the author

Just click on “start” in the chat and go through the options. You only need to choose a name and a username — then you can save the key. Please keep it secret.

You can already find your bot in the Telegram search. Look for
“@<bot-username>” — but of course, the bot can’t do anything yet.

Let’s get started with the coding. I use an NPM package for working with the Telegram API. Just install it with:

yarn add node-telegram-bot-api

Then we can use the package. Place your token in the variable for it.

Done! Now we can start doing things with the bot. Here is a simple example.

With onText we can react to messages that fit the regex-pattern. In this example, the expression /\/echo (.+)/says that we have to write “/echo” and some text behind it.

The variable chatId is the ID of the user. Only with it, we can send a message from the bot to a Telegram user. So if we want to make the bot self-sufficient, the users' IDs have to be stored somewhere.

bot.sendMessage is used to send a Message (what a surprise!). resp is the sent string after the echo-command.
Yes, the bot sends back what we sent to it.
Run node app.js, open your Bot chat and send something like this:

Node.js telegram bot
Source: the author

Sending messages automatically

As I said, we need to use the chatId to send messages to the users — for that; we need to store it somewhere. In a production app, of course, you should choose non-volatile storage for this — MySQL, MongoDB, or another database.

For our example, I store the Ids in an array. A user can register with the bot with “/register” — then the bot is allowed to send him messages. The bot sends a message to each user once per second — we just go through the array of users with a for-loop.

As soon as we send “/register” to the bot, we get spammed with messages.

React to all messages i

With bot.onText we were able to react to regex-matching messages. But what if we want to handle all incoming messages? This is possible with bot.on("message") — it gets triggered whenever somebody sends a message.

Here is a quick example. The bot gets triggered for each message but only responds when the user sent “dog.”

Sending Polls

On Telegram, there is much we can exchange. Not just stickers, videos, and text messages, but also polls. Creating one ist easy:

bot.sendPoll(chatId, 'Is Telegram great?', [‘Sure’, ‘Of course’])

Right after the Id, we pass the Question of the poll. The third one is an array of possible answers — of course, you can have more than just two options.

This is how easy we can build a Telegram bot with Node.js!

Thanks for reading — Would you like to see a more complex project in the future? Then feel free to leave me your opinion.

Join my newsletter to stay up to date

Ten articles before and after

How to get money with your Telegram Bot. – Best Telegram

Recap: KAI <> Bitbns Telegram AMA – Best Telegram

Я пишу и работаю в Telegram. В конце девяностых Евгений Козловский… – Best Telegram

What Crypto Can Learn From Russia’s War On Telegram – Best Telegram

Как использовать Telegram с пользой – Best Telegram

Blockchain Cuties Telegram Stickers Are Here – Best Telegram

Polymath becomes one of the largest Telegram groups in the world – Best Telegram

Telegram Bot — Como fazer um Bot com JavaScript e Heroku – Best Telegram

JUST Community Campaign —Join JUST telegram group to share a total of 65,000 JST – Best Telegram

Dragon Coin and Telegram Take Lions’ Share of ICO Investments in March – Best Telegram