DEV Community

loading...

Starting a Hello world with Telegram bot in Node.JS!

andrecrjr profile image André Carlos Originally published at Medium on ・4 min read

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

Photo by Christian Wiediger on Unsplash

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 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!

Botfather the father of bots, the first bot.

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.

/newbot in botfather, What will be the name of our bot!?

then you’ll be give the bot name url to him, that’ll need to have “Bot” in the end

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

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

Finally code!

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!

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:

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!

talking to my bot! get your link from the botfather when he gave you the API!

TeleBotis 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, the most famous is Telegraf.

See what you can do with telegram bots here!

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

I hope you all enjoy it!

Discussion (1)

pic
Editor guide
Collapse
lmolivera profile image
Lucas Olivera

As Telegram just became a trend I wanted to create a simple bot just to know how do they work. Your tutorial is incredibly short and I was able to create a Hello World, thanks!