Creating your "hello world" bot in Telegram with Node.js!
Telegram Bots is love and we will create our Hello world!

Creating your "hello world" bot in Telegram with Node.js!

By Eroshi | devnewbies | 29 May 2020


Hello guys, I’m a web developer/front-end lover and i wanted to start here with something (easy) different than habitual: chat bots!

First things first...

To start this tutorial you will need to have and know the basic about NodeJS/npm and API, and some notepad to start our codes, as VSCode or Sublime Text, or whatever notepad from your own preference.

There is a lot in the internet about web development with this guys and I will not explain about them for now!

 

About Telegram

I think everybody knows what Telegram is, one of the most famous web chats and open source in the world!

So today we gonna talk about one of this cool things that we have there; its chat bots!

 

What is "Chat Bot"

In this crypto world of course (i think) you had been in telegram bot faucets thinking "amazing now i will win a bit of satoshis" but the withdrawal never comes... This is an example about a kind of bots in this great botworld, so bots is just an algorithm that do an “automatized” steps to achieve a certain goal.

In the case of faucet bot they create your own bitcoin address and just put a fake number to you see lying to you that you are winning something, i'm kidding neither all the bots do the same :)!!!!

More about chat bot here!

 


Starting with Node.JS

So we will use NodeJS, a server-side framework to work with Javascript in the server side, if you don't know what is server-side or how to install, i will not explain it here, you can see this article, talking about how to mount in your computer your first web dev server!

If you already have NodeJS in your machine we will do the following steps:

  • Create a new folder helloworldbot in your computer
  • Access this folder by terminal and use the following command: 
npm init -y
  • Use your visual code or sublime text to start code!

 

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!

1*joQJ2X5M-yR8V5JdhGEJ-A.png

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.

1*_1TXPooCNjcSK_yt36ol0Q.png

1*HDGwkMAwsSvgbqyc7HMzJg.png

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

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, you can start your bot in telegram using it!!

 


Crawling back to the NodeJS

So we created our first bot and get our first token... Let's code! (finally). Remember the previous step? That folder which i asked you to create in terminal using NPM?

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

Telegraf 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.

First we will need to install it in the folder to put in our package.json(created by the npm init -y):

npm install telegraf --save
or
yarn add telegraf

 

Amazing, now let’s code our index.js(javascript) inside the folder:

const Telegraf = require("telegraf");

//instantiate Telegraf with our token got in the BtFather
const bot = new Telegraf(process.env.BOT_TOKEN or YOUR TOKEN FROM BOTFATHER)

//our command
bot.command('start', (msg) => msg.reply(`Hello ${msg.from.username}`))

//start polling
bot.start();

It's simple, but there is a lot here!

line 1 - Get the package telegraf!

line 4 - Instantiate the telegraf class as our bot put the TOKEN got in the botfather.

line 7 - the first command /start from our bot, it receives a callback with "msg" as our information that cames from telegram, and we reply here for our dear user using reply a keyword from telegraf to send a message back to telegram! :)

line 10 - Polling our bot as a server to telegram(don't forget this) the way we connect to our bot!!

And to begin you must not forget to change your package.json to start in your terminal your folder!

//folder package json

{
  "name": "hello world",
  ...,
  "dependencies": {
    "telegraf": "^3.38.0",
  },// put your index.js as a script here and start in the terminal
  "scripts": {
    "start": "node index.js",
  },
}

then you can run in your bash or windows terminal like this inside your folder:

C:\Users\andre\...\helloworld> npm start

 

Now you can go back to telegram! I hope you started your bot in your contacts using the URL given by the botfather to you.

hello bot!

Nice! I know it's simple but is a start! We can go beyond but I think first if you don't know nodejs or javascript at all you will need to learn about it, but I wanted just to explain this simple bot, to don't start with more advanced things as Inline Commands and Keyboards!

You can play more with the telegraf and you can see it here, and about Telegram Bot API here, there is a lot of cool things that we can do answering our User normally, send photos, music, stickers and do good things to improve your own as a developer and achieve the users from this chat app! :D

So guys that's it, I cover the simple basics from creating Bot, I hope you'll enjoy it!

 

Some doubts? you can find me at Telegram: @andrecrjr!


Eroshi
Eroshi

Front end developer and Crypto-like, follow me: https://twitter.com/andrecrjr


devnewbies
devnewbies

Web development, Front-end, Telegram Bot, Javascript in a newbie way, follow me to good development things!

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.