Telegram Bot with Python

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:

telegram bot

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.

Follow Us:

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1128

3 Comments

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

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

Leave a Reply