Writing a telegram bot using telegraf.js (part-1)

This part is just the introduction of writing a telegram bot. This article is all about setting up a telegram bot a.k.a A hello world from your own telegram bot.

Throughout the series we’ll be using telegraf.js library. Telegraf.js is a very powerful library to build telegram bots using node.js.

Though you will need to have a good grasp of javascript and nodejs in order to build a telegram bot using telegraf.js, I will keep this article a very noob friendly tutorial. So, grab your coffee, sit back, and code along with me.

Setting up your telegram bot

Let’s now get our dummy bot from @BotFather bot.

Getting Started

  • Next, the bot father will ask you to setup your bot’s name. So, just send the name of your bot in the reply
  • After setting up the name, bot father will ask you for your bot’s username, so just send the username in the reply.

Now the conversation should look like this^. As you can see I’ve set my Bot’s name as “Chomtu” and it’s username is “@ChomtuBot”.

Once you successfully set your bot’s name and username, you will get a sucess message with a TOKEN TO ACCESS in it, just like in the image below.

Save this token somewhere safe and do not share this anyone. If a malicious user manages to get your bot’s token number then he can set your bot on fire and will have full access to your bot. So, just don’t share it.

Setting Up Bot’s Profile Picture

Alright, so far so good. Now you have a dummy bot ready. But it can’t do anything for now, it’s just a dumb dead bot.

In this tutorial we will create an “echo” command which will simply return whatever the user will type.

Yay! It’s time to write some code now.

Setting up coding environment

Now, create a folder in which you will write the code for you bot. I have created a folder named “ChomtuBot”.

Navigate to that folder from your terminal and do an “npm init -y” to initialize your nodejs project.

npm init -y

This command will give the following output and you will get a new file called “package.json” in your folder.

Installing and setting up essential npm packages

npm install telegraf dotenv

This command will install 2 packages i.e telegraf and dotenv.

We are will use dotenv package to store out secret tokens.

Upon succesfully installing both the packages, you will get an output like this…

Aditionally, you will find a new file named “package-lock.json” and a folder named “node_modules”. Don’t worry about those and just ignore them for now.

Now, create 2 new files named “index.js” and “.env

.env file

index.js file

You can now run your bot by typing “node index.js” in the terminal

node index.js

and the terminal should give and output like this…

and now if you send a “/start” command to your bot, your bot should reply “Hello World!

The Echo command

Right below the Start Command, add this code…

Make sure, you re-run the bot by doing “node index.js” after you complete writing the echo commands function.

Final code should look something like this….

Now you can try the echo command…

Conclusion

I have planned to write few more commands like “weather”, “random dogs”, “random cats” and much more. So do let me know if you want to see those posts in the future.

Also, If you stumble accross any error or can’t get past any doubt then be sure to post it down in the comment section below.

Cheers✌️

Loves technology.