Regela

Questions_01

Oct 14th, 2022
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import logging
  2.  
  3. from aiogram import Bot, Dispatcher, executor, types
  4. from aiogram.contrib.fsm_storage.memory import MemoryStorage
  5. from aiogram.dispatcher import FSMContext
  6. from aiogram.dispatcher.filters.state import StatesGroup, State
  7. from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup, KeyboardButton
  8.  
  9. from aiogram.contrib.fsm_storage.files import JSONStorage
  10. from settings import API_TOKEN
  11.  
  12. logging.basicConfig(level=logging.DEBUG)
  13.  
  14. bot = Bot(token=API_TOKEN)
  15.  
  16. # storage = JSONStorage("states.json")
  17. storage = MemoryStorage()
  18.  
  19. dp = Dispatcher(bot, storage=storage)
  20.  
  21.  
  22. class StateMachine(StatesGroup):
  23.     questions_started = State()
  24.  
  25.  
  26. questions = [
  27.     {
  28.         "text": "Вопрос1",
  29.         "answers": ["Вопрос1Ответ1", "Вопрос1Ответ2", "Вопрос1Ответ3", "Вопрос1Ответ4"]
  30.     },
  31.     {
  32.         "text": "Вопрос2",
  33.         "answers": ["Вопрос2Ответ1", "Вопрос2Ответ2", "Вопрос2Ответ3", "Вопрос2Ответ4"]
  34.     },
  35.     {
  36.         "text": "Вопрос3",
  37.         "answers": ["Вопрос3Ответ1", "Вопрос3Ответ2", "Вопрос3Ответ3", "Вопрос3Ответ4"]
  38.     },
  39. ]
  40.  
  41.  
  42. def get_question_text_by_id(id):
  43.     return questions[id].get("text")
  44.  
  45.  
  46. def generate_answers_markup_by_id(id):
  47.     markup = InlineKeyboardMarkup()
  48.     for i in range(len(questions[id].get("answers"))):
  49.         answer_text = questions[id].get("answers")[i]
  50.         markup.add(InlineKeyboardButton(answer_text, callback_data=f"answer_{i}"))
  51.     return markup
  52.  
  53. @dp.message_handler(commands=['start', 'help'], state="*")
  54. async def send_welcome(message: types.Message):
  55.     markup = ReplyKeyboardMarkup().add(
  56.         KeyboardButton("Начать"),
  57.         KeyboardButton("Позже"),
  58.     )
  59.  
  60.     await message.reply("Здравствуйте! Я предлагаю вам пройти опрос!", reply_markup=markup)
  61.  
  62.     logging.info(f"{message.from_user.username}: {message.text}")
  63.  
  64.  
  65. @dp.message_handler(text='Начать', state="*")
  66. async def start_questions(message: types.Message, state: FSMContext):
  67.     await StateMachine.questions_started.set()
  68.  
  69.     async with state.proxy() as data:
  70.         data["current_question"] = 0
  71.         data["answers"] = []
  72.  
  73.     await message.answer(get_question_text_by_id(0), reply_markup=generate_answers_markup_by_id(0))
  74.  
  75.  
  76. @dp.callback_query_handler(text_startswith="answer_", state=StateMachine.questions_started)
  77. async def but_pressed(call: types.CallbackQuery, state: FSMContext):
  78.     answer = int(call.data.split('_')[1])
  79.  
  80.     await call.message.edit_reply_markup(InlineKeyboardMarkup())
  81.  
  82.     async with state.proxy() as data:
  83.         data["current_question"] += 1
  84.         current_question = data["current_question"]
  85.         data["answers"].append(answer)
  86.  
  87.     await call.message.answer(get_question_text_by_id(current_question), reply_markup=generate_answers_markup_by_id(current_question))
  88.  
  89. if __name__ == '__main__':
  90.     executor.start_polling(dp, skip_updates=True)
  91.  
Advertisement
Add Comment
Please, Sign In to add comment