In this article, we will consider creating a chatbot in the Telegram messenger from scratch. No special knowledge will be required of us. For starters, basic knowledge of Python as a programming language will suffice.
First, you must be registered in the Telegram messenger. Next, in the desktop or web version of the messenger, you open communication with the @BotFather bot.
After starting communication with this bot, pressing /start, you will receive a response from the bot indicating its capabilities. We are interested in creating a new bot - /newbot.
After we enter /newbot we are asked to come up with a name for our new bot. Let it be MyFirstTestBot.
Next, we need to come up with a username for our bot. In our case, it will be mft001_bot.
After that, BotFather sends us a special token:
Our token turned out to be: 851216368: AAG6_JHHsIqAK-lX2CxOWQHTAM109zdrcZM (In your case, the token will be different.)
We will need this token when setting up our bot. Token must be saved. It is he who is the only key to interact with the bot.
We also get a link to our bot. In my case, this is t.me/mft001_bot.
Now our Tekegram bot has been created. We can start customizing our bot, for example, setting an image for a bot, changing or adding a bot description, etc.
With the registration of the bot, we are done. Our bot is already there, but at the moment it still does not know. Now we need its filling - some kind of mechanism that will process our requests for this bot and return us the answers.
As described above, we will create our bot in Python. Install it from the official site if you use Windows:
sudo apt-get install python python-pip
Next, we use the PIP package management system, which is used to install and manage software packages, and install the PyTelegramBotAPI (Telebot) library:
pip install pytelegrambotapi –user
Let's create the logic of our bot. Using a full-fledged IDE or a simple text editor, create ourbot.py file and fill it with the necessary logic.
First, we need to import the PyTelegramBotAPI (Telebot) library by writing in our file:
Next, connect our bot using the previously received token:
bot = telebot.TeleBot ('851216368: AAG6_JHHsIqAK-lX2CxOWQHTAM109zdrcZM')
Now create a method to receive messages.
The capabilities of PyTelegramBotAPI allow sending bot audio (content_types = ['audio'), video (content_types = ['video'), documents (content_types = ['document'), text (content_types = ['text'), geographical address (content_types = ['location'), contact details (content_types = ['contact') and stickers (content_types = ['sticker'). For simplicity of experience, we will communicate with the bot only in text:
@ bot.message_handler (content_types = ['text'])
def handle_text_messages (message):
Now let's look at the processing logic of our text messages. We want to hardcode the simple communication of the bot with the user: the bot must be able to greet when they greet him, be able to answer the questions "Who are you?", "What is your name?" and "What can you do?"
if message.text == "Hello":
bot.send_message (message.from_user.id, "Hello")
elif message.text == "Who are you?":
bot.send_message (message.from_user.id, "I am a test chatbot for a case study.")
elif message.text == "What is your name?":
bot.send_message (message.from_user.id, "My name is MyFirstTestBot.")
elif message.text == "What can you do?":
bot.send_message (message.from_user.id, "I can answer a few simple questions - who am I, what is my name and what can I do.")
bot.send_message (message.from_user.id, "I don't understand you. Write something else.")
After the body of the method that processes our requests to the bot, add a method call:
bot.polling (none_stop = True, interval = 0)
The objective of this method is to create a stream in which the bot sends requests to the server, clarifying in this way whether someone wrote a message to it. The none_stop: False parameter means that our program will continue to send requests to the server after receiving an error message from the Telegram server.
Save our code:
We can test the operation of our bot by running its code in the IDE in which we wrote. And writing to our bot in the messenger.
Our training Telegram bot is created. We can run our file locally, and it will process requests to it through the messenger directly on our computer acting as a server. But this is not a very convenient practice. For normal operation, it is advisable to upload the code to a separate server and run it there.