A bot is a software application programmed to perform certain tasks. The robots are automated, which means that they operate according to their instructions without a human user needing to start them. Bots often mimic or replace the behaviour of a human user. In this article, I will create a Telegram bot with Python, by using the telegram API.
To create a Telegram Bot using Python, you need to go through some steps to get a telegram bot API from the BotFather account on Telegram. BotFather is simply s Bot which helps in creating more bots by providing a unique API. So before using python to create our Telegram bot, we need to go through some steps to get the API.
Also, Read ā Bubble Plots with Python.
Steps to Get the Telegram Bot API
First, create an account on telegram if you donāt have an account. After making your account search for BotFather, which is an official telegram bot that provides API to create more bots. When you will open the chat just write /start and send. The BatFather will reply you with a long text without reading the text you can typeĀ Newbot.
Now it will reply you again with a long text, asking about a good name for you Telegram bot. You can write any name on it. Now the next step is to give a username to your bot which should be in a format Namebot or Name_bot. And the main thing to notice in this step is that your username should be a unique one, it should not match any other username all around the world.
Also, Read ā Scraping Twitter with Python.
Now after typing a unique username, it will send you an API key between a long message, you need to copy that username and get started with Python.
Telegram Bot with Python
Now, we have the API key to build our telegram bot, the next step is to install a package known as telegram, which can be easily installed by using the pip command in your command prompt or terminal āĀ pip install python-telegram-bot.
After successfully installing the package, now letās import the required packages and get started to make a Telegram Bot with Python. We only need the telegram package for this task, I will import it and prepare our program to read our API Key:
import telegram
bot = telegram.bot(token='TOKEN') #Replace TOKEN with your token string
Code language: PHP (php)
Now that everything is working, letās follow the tradition and create a Hello World program. I will simply program our chatbot here with a command on which our telegram bot will respond with the message āHello, Worldā:
Now letās create a hello function that sends the desired text message through the bot:
We now use a CommandHandler and register it in the dispatcher. Basically, we bind the / hello command with the hello () function:
And thatās it. To start our bot, add this code at the end of the file:
updater.start_polling()
Code language: CSS (css)
Now, run the code and writeĀ /helloĀ in you your telegram messenger to your telegram bot. It will reply with the text āHello Worldā.
COVID-19 Telegram Bot with Python
Now, letās build our program to get information related to the COVID-19 to get results from a simple text. Now here, you need to import two modules here known asĀ requestsĀ andĀ json.Ā Now letās import these two modules and build a COVID-19 telegram Bot with Python:

Also, Read ā Password Generator with Python.
I hope you liked this article on building a Telegram Bot with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.
TypeError: āmoduleā object is not callable
Giving me this error on the 2nd line, when I replace the token with my token string.
Can you tell me right way to import telegram?
I tried āimport telegramā and āfrom telegram import *ā But it doesnāt worked for me.
have you installed this library
Yes I installed the library, Actually we donāt need to import telegram. I skipped the first 2 lines and started my code āfrom telegram.ext import ā¦ā¦.ā and it worked for me.
But one question bro, I can only get total cases and total stats with āGlobalā. How can I print the data of a specific country? Like I tried a lot. I tried āprint(data[āIndiaā])ā but there is no such response. It would be great if you give me little hint about it.