Developing a Telegram Chatbot Using Python 3

Farhaan Nazirkhan
4 min readSep 23, 2022

--

I have recently landed on a small project, developing a Chatbot to be used on the Telegram platform and I’m going to show you how to do the same. By the end of this article, you will be able to create and deploy your own Chatbot on the Telegram platform.

Prerequisites

Creating the Bot and getting the API Key

  1. Log in to your existing Telegram account if you already have one or create a new one.
  2. Search for @Botfather using the search option or you can also access it from here.
  3. Now choose the “START” option and then type “/newbot” command and follow the instructed steps given by the bot to assign a name and username to your bot.
  4. After you have completed step 3, you’ll be given a token to access the HTTP API. This token should be kept secure and safely as it can be used by anyone to control your bot.

For a description of the Telegram Bot API, see this page.

Coding and Deploying the Chatbot

  • A python file consisting of all responses for a specific asked question.
  • An optional python file consisting of random responses to be returned when the bot does not understand a message from the user.
  • A python file with code to handle the input, reply message, help command and start command.

Below is the code for the predefined responses. Here when the input message from the user (user_message) identically matches one of the predefined string, then the appropriate message is returned. datetime module was imported for manipulating dates and times. You can import other modules to be used in your program as well. The module Random_Responses will be described in the next image.

Responses.py

Below is the code for predefined random responses that will be returned to the user when the bot does not understand the input message as only strings defined in the knowledge base (that is responses predefined in the Responses.py file) can return an appropriate message. Long story short, these are like error messages that will be returned to the user. A module called random was imported to randomly select one of the error messages to be returned.

Random_Responses.py

Before writing code in the main.py file, you will need to run this command in your terminal: “pip install python-telegram-bot”

Below is the code for the main program. First of all, the API key that you have obtained from Telegram can be assigned to the variable API_KEY as shown at line 4. At line 8, the start_command code gets executed when the user clicks on “START” or when “/start” is input by the user on Telegram to start the bot. From line 14–18, the function handles the message and gives the user a reply.

main.py

Now after all the coding has been done, simply run the main.py file to start the bot and a message “Bot has successfully started!” will be displayed in the terminal if the bot has been successfully started.

To test out the bot, go on Telegram and simply search for the bot by the username that you have given to it earlier and click on the “START” button which is located at the bottom to start the bot and you are good to go. Write whatever you want, if the message is not recognized, that is, it is not present in the Responses.py file then a message from Random_Responses.py would be displayed as shown by the fifth text from the bot’s side.

AI_12_Bot in action

Note that for the bot to continuously function, the program has to continuously run in the background.

You can find the python files from here.

Thank you so much for taking the time to read my article. Don’t forget to follow me so that you don’t miss out any future articles. You may also check out my portfolio from here.

--

--

Farhaan Nazirkhan

A proactive Software Engineering student with a passion for computer programming and Information Technology.