Starting a Hello world with Telegram bot in Node.JS!
Tags: node, webdev, telegram, chatbot
Published: 2020-05-27 14:37:12

---
title: Starting a Hello world with Telegram bot in Node.JS!
published: true
date: 2020-05-22 04:00:08 UTC
tags: nodejs,web-development,telegram,chatbot
canonical_url: https://medium.com/@andrecrjr/starting-a-hello-world-with-telegram-bot-in-nodejs-d89ac85ccdb
---

Using Telebot to create our first hello world Bot in Telegram!

![](https://cdn-images-1.medium.com/max/1024/0*0lyfMqzUHPbZZe2u)<figcaption>Photo by <a href="https://unsplash.com/@christianw?utm_source=medium&amp;utm_medium=referral">Christian Wiediger</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Hello guys, this is my first article in Medium! I’m a web developer/front-end lover and i wanted to start here with something different than habitual: chat bots.

### About Telegram

Telegram is one of the most famous chat platforms bellow Whatsapp around the world and had been the one of the most famous open source project in the world, but its almost impossible to talk in Telegram without talk about its bots…

### Whats is “Bots” ?

Bots or Chat bots in a simply talking is about a chat that you have a interface with **an “automatized” robot to achieve a certain goal** _(algorithm)_; you will have keywords/commands to start interaction with the bot, what can be like: _“/giphy i want gif of something”_, thats a kinda command that will be searching a gif inside Telegram for example.

In Telegram, chat bots is amazing, you can create bots to [pay as a e-commerce](https://core.telegram.org/bots/payments) inside them and send to the user that will be interacting with him what the platforms can send as photos, location, music etc, its API to create a chat bot is real amazing!

> **_Warning: I will explain the basic with NodeJS to create the minimum BOT without deploy, will be nice to create a new post about it in the future if someone asks for it :) !_**

### Botfather

First things first, we need to create our new Bot in Telegram, so you’ll need to go to Telegram Web/Desktop, and find the father of the Bots, BotFather!

![](https://cdn-images-1.medium.com/max/764/1*joQJ2X5M-yR8V5JdhGEJ-A.png)<figcaption>Botfather the father of bots, the first bot.</figcaption>

Just do the /start and they will “chat ” with you a lot of commands that you can do with him! And obviously let’s start a new bot: send **/newbot** to him.

![](https://cdn-images-1.medium.com/max/683/1*_1TXPooCNjcSK_yt36ol0Q.png)<figcaption>/newbot in botfather, What will be the name of our bot!?</figcaption>

![](https://cdn-images-1.medium.com/max/695/1*HDGwkMAwsSvgbqyc7HMzJg.png)<figcaption>then you’ll be give the bot name url to him, that’ll need to have “Bot” in the end</figcaption>

If your bot name is the **unique** in all the **around bot universe of Telegram…**

![](https://cdn-images-1.medium.com/max/666/1*tzFWZszF3fEmWakWbmAUEw.png)

Congrats, you’ll get your API KEY and your link to your bot, in my case here “t.me/hellwrldbot” it’s the url, your can start your bot in telegram using it!

### Coding with Node

![](https://cdn-images-1.medium.com/max/540/1*npxtXyLjCSpM7Sma1eQj8g.gif)<figcaption>Finally code!</figcaption>

You will need to create a folder in your terminal/bash and a project using Yarn or NPM as if you started a normal project in nodejs:

```
yarn init -y helloworldbot OR npm init -y helloworld
```

Awesome you create a folder project, now we will need a guy that will help us with the telegram API and ladies and gentlemen, i present you [Telebot](https://github.com/mullwar/telebot/)!

Telebot is a lib that will help us to deal with the “polling” to the bot (or webhook to most advanced uses) and we will give our commands of the bot to it.

So we’ll add him to our package.json:

```
yarn add telebot
```

Amazing, now let’s code our index.js:

{% gist https://gist.github.com/andrecrjr/0d8fcc48380956d69b22e5353d00ef26 %}

It’s simple but there is a lot here:

**line 1-** We get the Telebot from the package(orly)

**line 4-** We create a bot instance from Telebot with your API token got in the BotFather!

**line 9-** I use the “event listener” bot. **on** to listen two commands in this case /hello and /start

**line 12-** Inside this listener **bot.on** we have a callback function

This callback function will receive **msg** , this is the **JSON** which telegram will send to you, everything about the user will be sent in this  **JSON.**

so **bot.message(msg.from.id, “your message to user!”)** will be the reply as message!

**line 16-** We start the bot to polling and listen our bot in telegram

Now you need to start the server to Bot works, you can modifying your package.json adding a script to NPM/Yarn:

```
"scripts": {
"start": "node index.js",
}
```

above your dependencies in package.json and start it in the terminal:

```
yarn start
```

and now your bot in the url/link that Botfather gave you in telegram!

![](https://cdn-images-1.medium.com/max/853/1*5rDwMUkNIO543Mrwszqm8g.png)<figcaption>talking to my bot! get your link from the botfather when he gave you the API!</figcaption>

[TeleBot](https://github.com/mullwar/telebot)is really good, there is a lot of ways to answer our users in telegram, as I said before, you can send photos, stickers, music, location, sell things using bot(!!!) and secure way, you can study their API!

> There is another libs that do the same as telebot, you can see it [here](https://core.telegram.org/bots/samples), the most famous is [Telegraf](https://github.com/telegraf/telegraf).

[See what you can do with telegram bots here](https://core.telegram.org/bots)!

That’s all folks, for beginners, direct, simple, clean and easy!

I hope you all enjoy it!