Search code examples
telegram-botpy-telegram-bot-api

Telegram quiz message handler but don't know how to display correct answer when answered


Here is a sample of the code that I am using would be gratefully if someone can point me in the right direction.

import telebot
bot = telebot.TeleBot("API KEY")

print("Bot starting...")

@bot.message_handler(commands=['quiz'])
def quiz(message):

    q = 'What is the capital of Italy?'
    answers = ['Rome', 'London', 'Amsterdam']

    bot.send_poll(message.chat.id, question=q, options=answers, correct_option_id=0, open_period=5)

bot.polling()

What I'm not sure about on how to make work, is once the question has been answered how to display the correct answer out of the options that are available. For Example the question above, for it to say the correct answer is "Rome" after each question.

Thank You in advance


Solution

  • If you pass type='quiz' to send_poll, then TG will automatically take care of that. See the official docs of sendPoll. If you don't want to the native quiz interface of TG, you'll have to make the poll non-anonymous. In that case, you'll receive PollAnswer updates when a user makes a vote and you can respond to that update in the way that you want to.