A
A
Angelina2019-08-08 14:28:27
Python
Angelina, 2019-08-08 14:28:27

How to implement a telegram bot quiz?

Hey!
I plan to create a quiz that will run on a specific button.
How to implement it?
Text and four buttons appear (keyboard not a problem). The text must be taken from the database and sent as a message. The user presses one of the buttons and a message arrives - this is the correct / incorrect answer.
And immediately a new question appears with the same keyboard, but with a different correct letter.
How to implement it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2019-08-08
@LinaThePython

Create a database with columns:
id , question, correct answer, wrong answer
Create a callback button, when clicked, all questions are pulled from the database and 1 random one of them is displayed to the user, looks something like this:

questions = db.execute('SELECT * FROM QUESTIONS')
question = random.choice(questions)

In order to avoid repeating questions, it is better to create a table with users, where the id of the questions to which the user answered will be entered, then you will need to make a request to it and take data from the table with questions, taking into account the answers given:
answered_ids = db.execute('SELECT answered_ids FROM USERS WHERE (user_id IS ?)', (callback_query.from_user.id,))
questions = db.execute('SELECT * FROM QUESTIONS WHERE id != IN(?)', (answered_ids,))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question