Intro to Telegram Bots

Welcome to the World of Automation :)

Well, hello mate, you must have used Telegram once or twice in you lifespan either for getting links to some cute sexy web serieses or either for some education material.

Ever wondered with the term Bot (Robot in short) and what and how some TG chats keep hapening on just a click of your button!? "is there some person attending to miliions of users at the same time, or its just a robot" ?

Well you guessed it right, It is and it will always be a robot, a slave to human needs :) no offence.

Introduction to Telegram Bots

Assume telegram bots as just some other web-application you use to interact with some server over the cloud, then how and what makes it more power then a normal web-app?

Well mate the differnce isnt much, the seamless integration with large messaging platform "Telegram itself" and the taste of automation, to respond to millions of users on a real time basis is what makes it intersting as it is to the day it is.

"Its the world of possibilities and integration thats give tg bots the pinch they need and a developer the meal he deserves."

Working of a Web App

I wont get into much details of the topic rather i will share and image with you for sake of completion .

web request-response cycle

The image is taken from "Blog on Web application architecture

Now lets get into Telegram bots and what makes it different

Telegram Bot Working

Well have a look

Here's the flow:
A telegram Bot is a part of our Telgram Application, so when trying to buidl a bot one needs to authroize her phone number with the telgram and register her application with the Tg Servers.

So there are:

  • User Authentication

  • Telegram App Registeration

  • Bot Authorization

All of these processes require some efforts which are easy to understand and get it, once one knows the basics.

User Authentication

Any dev or builder for the tg bot needs to authenticate her account with the telegram for the prove sake that this number belongs to them with which the application is to be associated.

There are ways to do it and some python frame works does them by themself for you, so that you can focus on building your applicaiton rather than these trivial things.

The one we will be using is Pyrogram a python framework

Pyrogram does automate few things for us with just the use of Client() class.

Application Registeration

Now offcourse any telgram app must need to be registered with the telegram itself right? so the question is, how one does that, and what are the things required for it?

This brings us the concept of API_ID AND API_HASH, how one obtains them :

Obtain your app id and hash

API ID and API Hash:

Well these are unique identifiers for our application, obtained via procedures explained in the link,

  • API ID: A unique identifier for your application.

  • API Hash: A secret key associated with your application, used to authenticate API requests.

  • Together, they allow Telegram to identify which application is making the requests and to verify that those requests are legitimate.

Bot Authorization

Well this is the simplest of the above, each bot has a unique identification token associated with the Telegram ecosystem.

This can be easily generated via BotFather with simple steps mentioned there, you just have to follow them and save it for your future use. BotFather

Steps to Create a Telegram Bot

  1. Developer Account: Authenticate your phone number with Telegram.

  2. Application Registration: Obtain API_ID and API_HASH.

  3. Bot Creation: Use BotFather to generate a unique token for your bot.

Conclusion

For our Telegram Application a dev need to have a tg account for building, and further she would be required to associate and verify that the application is infact linked to the phone number.

Then later one application can have multiple bots associated with it, and each bot have their own unique identity tokens associated with the Telegram.

So three things required are :

  • A developer Account

  • An Application

  • Bots associated with the app

Post Script:

This blog was writted to get you familiar with the topic of TG Bots, the coding concepts and principles are intentionally excluded from here for the sake of simplicity and understanding.

In future blogs we will be using Pyrogram a python based framework and will gradually aim to understand the concept and build a bot by ourselves.

10
Subscribe to my newsletter

Read articles from Sarvesh Ghildiyal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sarvesh Ghildiyal
Sarvesh Ghildiyal