How to make Telegram Bots for absolute beginners
This is a detailed series and not a full-on the go bot development tutorial, a lot of students who have learned any programming language ever struggle to understand what’s going in the documentation of different APIs or libraries out there. So let’s jump into Telegram Bot API.
Prerequisite — You must know a bit of at least one programming language ( Java, Python, PHP, JS ……etc), Any one of them will work.
Here’s the link to the official Bot API documentation of telegram: https://core.telegram.org/bots/api
I would want you to look at it and see what all you can understand there?
— — — — If you read it carefully you can continue — — — —
First things first, we need to give birth to our bot on telegram app, There’s a bot named “Botfather” who manages all the bots on telegram https://telegram.me/BotFather, Open that link on telegram and create a bot with a unique username, It’s easy, After that he will handle you the “Token” you need to identify your bot, Keep it a secret and don’t expose it publicly because anyone having the access to that token has access to your bot.
For example, I have created a bot named The Example Bot :
Yep, Our new bot is ready but he is clueless, He does not know how to handle commands and all for now.
Bot API tells you how your bot will contact you and how you can contact your bot, Between all of these, there comes a thing called “JSON”.
“JSON” is nothing but a format, a format in which text data exchange takes place between two web services online, Your bot cannot be contacting you like: “Hey, I am The_Example_Bot with a new message from Groot, The message is ‘bla bla bla’ “, So, therefore, It’s very necessary to use some format and most of the internet services heavily use JSON to transfer text info.
Here is an example of a JSON Object ( data ) :
“name”: “Jason Lengstorf”,
“name”: “Kyle Lengstorf”,
See how accurate and easy to understand the data is now, Two persons, Their name and info is given in JSON here. Google JSON if you want to learn more.
Coming back to our bot, How do we see if someone sent a message to our bot? and more importantly, how do we handle it?
Bot API says
All queries to the Telegram Bot API must be served over HTTPS and need to be presented in this form:
Like this for example:
We have the token, the URL to get in touch with our bot, but what’s the “/METHOD_NAME”, A method, more formally is a function in programming world, a function which does something, So Method is a function which you want bot API to perform, Since we need an update about out bot, There’s a function ( method ) for that called “getUpdates”, Please read what this method is and what its format is from the link below
Okay Done? Don’t worry about what was written below ( offset, limit, timeout etc)
So we now know the method we need to use, First, send some message to your bot “hello bot”
Now Let’s make a request now for our example bot keeping in mind the request format : ( Of course, you are going to use the token of your own bot )
This is what I get when I open the link in browser :
Now Yes, That is a JSON Object, It’s not beautified tho, Use a JSON beautifier online to beautify it, After beautifying it.
"text": "Hello Bot"
You can clearly see, there are updates, Updates with the text “/start” and “hello bot”, Bot API gives very detailed info of how is each update going to look like, You need to understand the updated format, The JSON format in which it will be sent to you. Let’s see what getUpdates Method definition was
getUpdatesUse this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
It says we will get an “UPDATE” object, let’s see what “Update” object is,
You can see there are too many fields which have an explanation with them, For now, let’s parse our update and send the bot a reply.
To reply to the bot we need a “Method” again right? That’s the only way to tell your bot to do something.
A simple “send” query on bot API page gets me to this method “sendMessage”.
The parameter is the info we need to send to the bot API to let API know whom to send the message and what message
“chat_id” and “text” are required parameters, they are mandatory.
Let’s construct a simple sendMessage request, our update JSON object above told us that we got a message from a person with chat_id: 330959283
We have the chat_id of the person who sent us the message, so here’s a sample reply request I have made, Don’t forget to use your own token and a proper chat_id
https://api.telegram.org/bot980569822:AAGmatHbU5tMQoAN9hNNP5P3SGOqibfatVI/sendMessage?chat_id=330959283&text="Hey Master? How are you"
Using this kind of request in browser I get a message from my bot
And also an update to my request
See how cool and easy it is to work with bots?
But we are not going to keep on doing this manually right? We need a machine to do this all day, Let’s code a bot then and run it on a computer.
For this post, I am going to put an end here, See my next post regarding how to code a bot and host it on a platform.
Thanks for being here, Hopefully, you understand a bit of how the bot API works.