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.
Setting up your telegram bot
Telegram has a Bot Father bot which creates all the dummy bots. So, just search “@BotFather” in telegram’s search bar and you will find the bot father bot.
Let’s now get our dummy bot from @BotFather bot.
- Send a /newbot command to @BotFather bot. This command will create a bot for you
- 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
Let’s just quickly set our bot’s Profile Picture. To do that send a /setuserpic command to @BotFather bot and then send a photo.
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
NOTE: Make sure you have nodejs installed on your computer.
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
Before we begin writing out bot, we need to install some packages. Type in the command below in your terminal to install those 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”
Inside your .env file, store your bot’s token name in a variable called “BOT_TOKEN” and pass your bot’s secret token number which you got from @BotFather bot in the above steps as the value of that variable.
Add the following code in your index.js file
You can now run your bot by typing “node index.js” in the terminal
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
Okay, now we are moving towards the final part of this article. Let’s code out 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…
So that was it. You now have a basic telegram bot up and running which can simply echo what you said.
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.