This tutorial will go through a straightforward set of steps to get a responsive telegram bot up and running from scratch
I spent a considerable amount of time figuring out how to make a functional telegram bot. I mean sure, the official introduction is good, but theres a lot of stuff about what bots are, and a few scattered instructions about the API, but not enough of structure for a beginner to get up and running quickly.
So, heres how to make a responsive telegram bot with the least amount of hassle :
Set up your bot
You don’t need to write any code for this. In fact, you don’t even need your computer! Go to the telegram app on your phone and…
Search for the “botfather” telegram bot (he’s the one that’ll assist you with creating and managing your bot)
/helpto see all possible commands the botfather can handle
Click on or type
/newbotto create a new bot.
Follow instructions and make a new name for your bot. If you are making a bot just for experimentation, it can be useful to namespace your bot by placing your name before it in its username, since it has to be a unique name. Although, its screen name can be whatever you like. I have chosen “Marco Polo Bot” as the screen name and “sohams_marco_polo_bot” as its username.
Congratulations! You have created your first bot. You should see a new API token generated for it (for example, in the previous picture, you can see my newly generated token is
270485614:AAHfiqksKZ8WmR2zSjiQ7_v4TMAKdiHm9T0). Now you can search for your newly created bot on telegram :
Go ahead and start chatting with your bot!
Well, that’s pretty disappointing. Our bot seems to be stupid, in the sense that it can’t really reply or say anything back. Let’s take care of that by building our bot server which runs on the back end.
Set up you bot server
Every time you message a bot, it forwards your message in the form of an API call to a server. This server is what processes and responds to all the messages you send to the bot.
There are two ways we can go about receiving updates whenever someone sends messages to our bot :
- Long polling : Periodically scan for any messages that may have appeared. Not recommended.
- Webhooks : Have the bot call an API whenever it receives a message. Much faster and more responsive.
We are going to go with webhooks for this tutorial. Each webhook is called with an update object. Lets create our server to handle this update.
We will be creating our server using node.js, but you can use whatever suits you to make your server. Once you have node and npm installed :
First, initialize your project
After following the instructions, you will end up with a
Next, install you dependencies by running :
expressis our application server
axiosis an http client
body-parserwill help us parse the response body received from each request
Make a new file
You can run this server on your local machine by running
If all goes well, you should see the message “Telegram app listening on port 3000!” printed on your console.
But, this is not enough. The bot cannot call an API if it is running on your local machine. It needs a public domain name. This means we have to deploy our application.
Deploy your service
You can deploy your server any way you want, but I find it really quick and easy to use a service called now.
Install now on your system :
Add a start script to your
package.json file looks like :
Add a start script, to get :
Once you’ve added the script, run the command :
(remember to run in in the root of your project folder, wherever the
package.json file is located)
If this is your first time using “now”, you will see some instructions for signing in, but after that you should see something like this :
Great! This means your server is deployed on
https://telegram-bot-zztjfeqtga.now.sh (or whatever link you see instead), and your API would be present on
https://telegram-bot-zztjfeqtga.now.sh/new-message (as defined in
Now, all we need to do is let telegram know that our bot has to talk to this url whenver it receives any message. We do this through the telegram API. Enter this in your terminal :
…and you’re pretty much done! Try chatting with your newly made bot and see what happens!