Sobes.tech
Junior — Middle

How do you organize the order of questions in a Telegram bot written in Python?

sobes.tech AI

Answer from AI

To organize the order of questions display in a Telegram bot on Python, several approaches can be used:

  1. Store the list of questions in an ordered manner (for example, in a list or database with a sequence number).
  2. Save the current question index for each user (for example, in memory, a file, or a database).
  3. When receiving a command or user response, send the next question based on the index and increment the index.

Example of storing questions and the current index:

questions = ["Question 1", "Question 2", "Question 3"]
user_states = {}

def send_next_question(user_id):
    index = user_states.get(user_id, 0)
    if index < len(questions):
        question = questions[index]
        # send question to user
        user_states[user_id] = index + 1
    else:
        # all questions have been asked
        pass

This approach guarantees sequential display of questions to each user.