вот за 10 мин набросал тебе пример, изучай.
Вместо словарей прикрутить запросы к БД, можно логику меню подправить, например добавить выход из викторины.
from random import choice
import telebot
from telebot import types
API_TOKEN = ""
bot = telebot.TeleBot(API_TOKEN)
guessing_game = {
0: {'question': 'Кто убил Кеннеди?', 'choices': ['Lee Harvey Oswald', 'Pamela Denise Anderson', 'Joseph Robinette Biden'] , 'answer': 'Lee Harvey Oswald'},
1: {'question': 'Кто президент России?', 'choices': ['Горбачёв', 'Путин', 'Медведев'] , 'answer': 'Путин'},
2: {'question': 'Что?Где?Когда?', 'choices': ['1', '2', '3'] , 'answer': '2'}
}
answer_guessing_game = {
0: 'Lee Harvey Oswald',
1: 'Путин',
2: '2',
}
@bot.message_handler(commands=["start"])
def start_message(message):
keyboardmain = types.InlineKeyboardMarkup()
ask_button = types.InlineKeyboardButton(text="Угадывать!", callback_data="play")
keyboardmain.add(ask_button)
try:
bot.send_message(message.chat.id, "Привет, я бот помощник по тестам!", reply_markup=keyboardmain)
except telebot.apihelper.ApiException:
print('Error')
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.data == "play":
keyboard_main = types.InlineKeyboardMarkup()
question_id = choice(list(guessing_game.keys()))
question = guessing_game[question_id]['question']
choices = guessing_game[question_id]['choices']
choices_buttons = [types.InlineKeyboardButton(text=x, callback_data=f"{question_id}_answer_{x}") for x in choices]
keyboard_main.add(*choices_buttons)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text=question, reply_markup=keyboard_main)
if "answer" in call.data:
question_id, _, answer = call.data.split('_')
right_answer = answer_guessing_game.get(int(question_id))
keyboard = types.InlineKeyboardMarkup()
if right_answer == answer:
back_button = types.InlineKeyboardButton(text="back", callback_data="play")
keyboard.add(back_button)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text="Правильно!", reply_markup=keyboard)
else:
back_button = types.InlineKeyboardButton(text="back", callback_data="play")
keyboard.add(back_button)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text="Не правильно!", reply_markup=keyboard)
if __name__ == '__main__':
bot.infinity_polling()