![]() Figure 5: Downloading the Python library python-telegram-bot To create a bot controller script, we shall use a Python library python-telegram-bot so download this using Pip: pip install python-telegram-bot Writing a Python script to create a bot controller using python-library-telegram In the next section, we will look at creating a bot controller through a script written in Python. There is however a second method in which we can use a Python script to access the messages/ commands coming from the bot user to our bot hosted on the Telegram server. The first is of course the method we used earlier, where we used a Web browser and a URL of the format. Now there are two ways to interact with the bot. Figure 4: A screenshot of the output on giving a command Here the command is /getme and so it will provide details of your bot in the form of a JSON object (similar to a Python dictionary). The first two parts of the URL will allow your browser to access your bot (which resides on the Telegram server) and the last part will give the appropriate command to your bot. Figure 2: Using the command ‘/start’ on Note that your newly created bot is hosted on the Telegram server, and you can now access your bot on a Web browser by using a command of the format:ġ104497538:AAEbXXXi PegAxUXXXX_TevmyPG01GnXXLY Figure 3: Creating a new bot using above URL has three parts: (1) (2) and (3) /getme. (Some letters have been replaced with X to preserve privacy.) Figure 3 shows the process on the Telegram app. The token should look something like what’s shown below:ġ104497538:AAEbXXXi PegAxUXXXX_TevmyPG01GnXXLY will send you a message, which will include a token. Now create a new bot using the command /newbot and give it an appropriate name, which must end in the word bot.It must receive a text message (one without a forward slash) or a command (one with a leading forward slash) to respond to such messages.įigure1: The two different schemes for working the bot A Telegram bot cannot respond on its own (to reduce spamming by bots). However, if it has a leading forward slash, then it will be interpreted as the function / method. The scheme works like this - if it is a message without a leading forward slash (/), then it will be interpreted as a text message. So, as a first step, go to your Telegram app and search for Then send it the message /start. Telegram has a bot named which can create a bot for you.The steps in creating a Telegram bot are as follows: In this article, I am only describing the ‘polling’ scheme. This scheme however requires two additional things - a permanent IP address of the server where the bot controller is located, and that the server hosting the bot controller must have HTTPS enabled. Here the bot controller script does not ‘poll’ the bot rather, it is the bot that informs the bot controller that a new message has been sent by a bot user. The webhook scheme: This is the more efficient scheme.The ‘polling’ scheme: Here the Python script (which controls the bot) will periodically access, i.e., ‘poll’ the bot on the Telegram server, check if there are any new messages from a bot user, and respond accordingly.You need to understand that there are three actors in the scheme - the bot user, the Telegram server that hosts the bot, and the bot controller (which is from the Python script that we write and execute).Īlso, note that you can use the following two ways to control the bot. You will need the Python library python-telegram-bot.You need to install Telegram on a mobile phone and also on your desktop.Spyder, P圜harm, Visual Studio Code or Eclipse (with PyDev)). A Python IDE (I use Jupyter Notebook, while you could pick any of the popular options like.Basic knowledge of Python and using Pip/Conda to install packages.To get started with building the chatbot in real-time, there are some prerequisites, which are as follows: ![]() Understanding the package used in this article will enable the adventurous to extend the functionality of this chatbot on their own. However, in this article, readers get some insights on how to code for a chatbot in Python. The Internet is full of articles about building chatbots on platforms like Telegram.
0 Comments
Leave a Reply. |