Building a Telegram bot using PythonChatbots are generally touted as a revolution in the manner users interact with technology and businesses. They have a simpler interface compared with traditional applications, as they only need users to chat. The chatbots are meant to understand and perform the tasks the user demands from them, at least in theory. Many industries are shifting their customer service to chatbot systems because of the huge drop in the cost compared to actual human beings and the robustness and constant availability. Chatbots provide a degree of user support without substantial additional cost. In the present day, chatbots are utilized in multiple scenarios, ranging from menial activities like displaying time and weather data to more complex operations like rudimentary medical diagnosis and customer communication/support. We can devise a chatbot that supports the customers when they ask specific questions about the product. We can build a personal assistant chatbot that can handle fundamental activities and work as a reminder to remind different day-to-day activities like time to head to a meeting or the exercise. There are various options available to us when it comes to where we can deploy the chatbot, and one of the most common utilizations is social media platforms, as most people utilize them on a general basis. The same can be said of instant messaging applications, though with some caveats. Telegram is among the more famous Instant Messaging (IM) platforms these days, as it enables us to store messages on the cloud instead of just the device, and it boasts good multi-platform support, as we can have Telegram on Android, iOS, Windows, and just about any other platform that can support the web version. Building a chatbot on Telegram is quite easy and requires steps that take a little time to complete. We can integrate the chatbot in Telegram groups and channels, and it also works on its own. In the following tutorial, we will understand how to create a telegram bot with the help of the Python programming language. But before we get started, let us understand the basic requirement for the project. Requirements
How to Install the python-telegram-bot Module?In order to install the Python module, we need 'pip', a framework to manage packages required to install the modules from the trusted public repositories. Once we have 'pip', we can install the python-telegram-bot module using the command from a Windows command prompt (CMD) or terminal as shown below: Syntax: Verifying the InstallationOnce the module is installed, we can verify it by creating an empty Python program file and writing an import statement as follows: File: verify.py Now, save the above file and execute it using the following command in a terminal: Syntax: If the above Python program file does not return any error, the module is installed properly. However, in the case where an exception is raised, try reinstalling the module, and it is also recommended to refer to the official documentation of the module. Steps to create the first botWe will follow some steps in order to create our first bot that is shown below: Step 1: After creating an account on Telegram, we will go to the search bar and search for the "Botfather". Step 2: Now, we will click on the "BotFather" (first result) and type command /newbot. Step 3: We will then give a unique name to the bot. Once we name it, BotFather will ask for its username. Then also give a unique name BUT remember the username of the bot must end with the bot. For example, telebot, mybot, welcomebot, etc. Step 4: After providing a unique name and if it gets accepted, we will get a message something like the following: In the above image, the token will be different for everyone; we will utilize this token in the Python code in order to make changes in the bot and make it just like we need, and different commands in it. Understanding the Stepwise implementation in Python codeIn the following section, we will understand the stepwise implementation of the telegram bot in the Python programming language. These steps include: Step 1: Importing required libraries Step 2: Defining the functions for operation Step 3: Adding the Handlers to handle the messages and commands Step 4: Running the bot Importing the required librariesWe will start by importing the libraries and functions required to create the telegram bot. The brief usage of these functions are as follows:
Let us consider the following snippet of code demonstrating the same: File: teleBot.py Explanation: We have imported the required libraries and functions in the above snippet of code. Defining the functions for the operation
Let us now consider the following snippet of code demonstrating the same: File: teleBot.py Explanation: In the above snippet of code, we have used the Updater() function and provided the API token received from BotFather. We have then defined two functions as the_start and the_help, including the messages to be displayed for the user. We will now add some more functionalities to the Bot. Let us consider the following snippet of code demonstrating the same: File: teleBot.py Explanation: In the above snippet of code, we have added different functions. These functions include the one to open Gmail, the one for YouTube, one to open LinkedIn Profile, and the last one for Javatpoint.com official website. These are not MANDATORY functions, and one can add any kind of functions and their reply_text as their preferences; these are only for illustrating. Here, we have also defined a function as unknownText that will send the message written inside it whenever it receives some unknown messages. We have also added the function as unknownCommand that will allow us to filter out all the unknown commands sent by the user and reply to the message written within it. Adding the Handlers to handle the messages and commandsNow we will be using the add_handler() function to handle different commands of the bot. Here is the following snippet of code that demonstrates the same: File: teleBot.py Explanation: We have used the add_handler() function in the above snippet of code. Within this function, we have used the CommandHandler() function that gets the command from the user, and in return, a reply is generated with the message enclosed within the function. Running the botWe will use the start_polling() function to run the bot. Let us consider the following snippet of code demonstrating the same: File: teleBot.py Explanation: We have used the start_polling() function in the above snippet of code. Whenever we start polling, the bot will be active, and it will look for any new message sent by any of the users, and if it matches the command specified there, it will reply accordingly. Now, before we see the output of the project, a complete code is provided in the next section. A Complete Python CodeThe complete Python code to create a Telegram bot is shown below: File: teleBot.py Output: Next TopicWeb2py Framework in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India