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.

Ashu
Ashu
Sep 15, 2019 · 5 min read

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 :

Don’t worry about the token

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 ) :

{
“jason”: {
“name”: “Jason Lengstorf”,
“age”: “24”,
“gender”: “male”
},
“kyle”: {
“name”: “Kyle Lengstorf”,
“age”: “21”,
“gender”: “male”
}
}

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

https://core.telegram.org/bots/api#getupdates

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 )

https://api.telegram.org/bot980569822:AAGmatHbU5tMQoAN9hNNP5P3SGOqibfatVI/getUpdates

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.

{
"ok": true,
"result": [
{
"update_id": 213295102,
"message": {
"message_id": 1,
"from": {
"id": 330959283,
"is_bot": false,
"first_name": "Ashu",
"username": "infinite4evr",
"language_code": "en"
},
"chat": {
"id": 330959283,
"first_name": "Ashu",
"username": "infinite4evr",
"type": "private"
},
"date": 1568522499,
"text": "/start",
"entities": [
{
"offset": 0,
"length": 6,
"type": "bot_command"
}
]
}
},
{
"update_id": 213295103,
"message": {
"message_id": 2,
"from": {
"id": 330959283,
"is_bot": false,
"first_name": "Ashu",
"username": "infinite4evr",
"language_code": "en"
},
"chat": {
"id": 330959283,
"first_name": "Ashu",
"username": "infinite4evr",
"type": "private"
},
"date": 1568522508,
"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,

https://core.telegram.org/bots/api#update

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

https://core.telegram.org/bots/api#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

JSON update object reply to the query we made to bot API

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.

https://medium.com/code01/coding-a-telegram-bot-and-hosting-it-7a7d849c69c5

Thanks for being here, Hopefully, you understand a bit of how the bot API works.

Code101

A programming blog to describe my projects or programs I…

Ashu

Written by

Ashu

Cs Student | Melomaniac

Code101

Code101

A programming blog to describe my projects or programs I code

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade