Hello, in this case I will show you how, I made my quiz bot.
I will provide the source code of the python and bash scripts. I will explain the structure and purpose of each file
and the handler. And of course you can interact with my ready-made bot.
Alas, when I made it, I did not intend it to be multilingual. And when I realized it, I didn’t dare to redo everything. Yes and
The essence of this bot is not multilingual. But next time, I will definitely make a multilingual bot. So it will be easier for all non-Russian speakers.
An example of creating a Telegram survey bot.
Quiz bot structure
My bot has a linear/sequential structure. With some 'floating' handlers.
What do I mean by this?
Since I have a survey bot. And he must ask questions sequentially, one by one.
Then the handlers that process user responses must be called sequentially, one after another.
By 'floating' handlers, I meant those functions that can be called at any time during communication with the bot.
There are 5 of them.
/start ➩ The bot starts. Welcome message.
/help ➩ Shows available commands
/menu ➩ Shows available commands
/settings ➩ Configure result output
/result ➩ Displays the result of people's surveys
Let's look at the structure of the survey bot project.
.env ➩ File containing the bot token. When downloading from the repository, it will not be there. You need to do it yourself. Take a look at README.md file
data.json ➩ Stores data about all users. Database. Yes, without MySQL and other SQL for now. Created automatically.
result_template.md ➩ Template that is used to display information.
requirements.txt ➩ Necessary packages for working in a virtual environment
main.py ➩ Entry point. Contains all request handlers
config.py ➩ Configuration file. Contains global variables.
formaters.py ➩ Functions, the essence of which is to be able to change the output of the results in the message.
placers.py ➩ Functions that interact with the template. Insert data into it.
utils.py ➩ Various utilities.
/start command
Welcomes new arrivals. Creates buttons in a message that invites you to take a survey.
Age of respondent
Asks for age and creates keyboard buttons.
Country of interviewee
Creates a dictionary user_values, and immediately assigns it the age and user id.
Creates buttons on the keyboard with country emojis on them. And of course he asks the country of the person being interviewed.
Gender of respondent
Catches the text of the selected country and adds it to user_values.
Asks for gender, and creates buttons on the keyboard with stubs ♂ and ♀
Interviewee's work
Processes text from the previous handler, your_sex_handler, adding gender to the user.
Asks about work, creates buttons in the message.
Interviewee's car
Stores the user's job value in user_values
He asks if there is a car?
Creates buttons on the keyboard
End of survey
Adds information about the machine to the user.
After which, it saves the user to the data.json database. Unless of course the user
polled completely, rather than accidentally calling a handler in the middle.
It might be worth adding a check for duplicate users. But I didn't do that
because I wanted to give users the opportunity to participate several times.
Displays a standard message and a button in the message with the ability to show the result.
Main Menu
This handler can be called via the commands */help* or */menu*.
He doesn't do anything special. Create buttons in the message. We display a standard message.
Settings
This handler is divided into 4 functions. One, *setting_update_format_numbers*,
directly implements the functionality of changing the format of the displayed information.
The other one, *setting*, shows possible settings, creates the corresponding buttons.
And the other two are implemented via *callback_query* and *message*. And they both cause
*setting*. This was done in order to be able to work with this function,
both with a command and with a button.
Result
There are two handlers. One for processing the "/result" command, the other for calling
through buttons. Both have the same functionality *result*.
The *result* handler itself is responsible for formatting result_template.md.
Inserts data using functions from *placers.py*. And the format is changed using *formaters.py*
Download a ready-made telegram bot, or copy the code
You can download the bot from my repository.
TimQuizzer-bot
Or you can view the source code here and copy the parts you are interested in
main.py
config.py
formaters.py
placers.py
utils.py
result_template.md
Conclusion
This is how I wrote my quiz bot. Perhaps it will seem simple to you, and you will be entirely right.
But you need to publish articles, right? In any case, I hope this case can help you in writing
own survey bot.
It is a guide for deploying a telegram bot on VPS. This bot will be written in Python/aiogram. Also you will know how to install, run, and update it. As …
How to implement an inline telegram bot or activate inline mode for an already existing Telegram bot and how to use it. The entire example code is available, and the …
Used termins
Telegram bot ⟶ This is a program that, using the Telegram **API**, can perform various actions in chats without a person.
aiogram ⟶ It is a framework that build upon asyncio and aiohttp python modules, fully asynchronous, for creating telegram bots.
VPS (Virtual private server) ⟶ This is a service whose essence is that access to a dedicated server on a specific machine is provided. There can be thousands of such dedicated servers on one machine. Typically, managing such a server is no different from managing a regular, physical one.
Serializer ⟶ Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types.
Server side rendering (SSR) ⟶ It is a technique in web development where the webpage's content is rendered on the server instead of the client's browser. The primary advantage of SSR lies in its ability to significantly enhance user experience by facilitating faster page transitions and quick loading times.
Related questions
What is a telegram bot for?
Telegram bots can be used for various reasons. They are universal assistants in business, can provide a convenient format for interaction with clients or be an excellent platform for hosting a website or tool.
Is python best option to create a telegram bots
Python is one of the most popular programming languages for developing bots. It is very simple and intuitive. It is a great choice for beginners. Python also has a huge number of libraries in its collection that will make your life and work easier.
What сhatbots сan't вo
Despite chatgpt capturing more and more users under its influence, chatbots are still poor at solving individual user queries and complaints. They won't find you new customers, and they won't help you save money either.