Regela icon

Questions_01

Regela | PRO | 10/14/22 06:00:56 PM UTC | 0 ⭐ | 1129 👁️ | Never ⏰ | []
Python |

3.06 KB

|

None

|

0 👍

/

0 👎

import logging
 
from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import StatesGroup, State
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup, KeyboardButton
 
from aiogram.contrib.fsm_storage.files import JSONStorage
from settings import API_TOKEN
 
logging.basicConfig(level=logging.DEBUG)
 
bot = Bot(token=API_TOKEN)
 
# storage = JSONStorage("states.json")
storage = MemoryStorage()
 
dp = Dispatcher(bot, storage=storage)
 
 
class StateMachine(StatesGroup):
    questions_started = State()
 
 
questions = [
    {
        "text": "Вопрос1",
        "answers": ["Вопрос1Ответ1", "Вопрос1Ответ2", "Вопрос1Ответ3", "Вопрос1Ответ4"]
    },
    {
        "text": "Вопрос2",
        "answers": ["Вопрос2Ответ1", "Вопрос2Ответ2", "Вопрос2Ответ3", "Вопрос2Ответ4"]
    },
    {
        "text": "Вопрос3",
        "answers": ["Вопрос3Ответ1", "Вопрос3Ответ2", "Вопрос3Ответ3", "Вопрос3Ответ4"]
    },
]
 
 
def get_question_text_by_id(id):
    return questions[id].get("text")
 
 
def generate_answers_markup_by_id(id):
    markup = InlineKeyboardMarkup()
    for i in range(len(questions[id].get("answers"))):
        answer_text = questions[id].get("answers")[i]
        markup.add(InlineKeyboardButton(answer_text, callback_data=f"answer_{i}"))
    return markup
 
@dp.message_handler(commands=['start', 'help'], state="*")
async def send_welcome(message: types.Message):
    markup = ReplyKeyboardMarkup().add(
        KeyboardButton("Начать"),
        KeyboardButton("Позже"),
    )
 
    await message.reply("Здравствуйте! Я предлагаю вам пройти опрос!", reply_markup=markup)
 
    logging.info(f"{message.from_user.username}: {message.text}")
 
 
@dp.message_handler(text='Начать', state="*")
async def start_questions(message: types.Message, state: FSMContext):
    await StateMachine.questions_started.set()
 
    async with state.proxy() as data:
        data["current_question"] = 0
        data["answers"] = []
 
    await message.answer(get_question_text_by_id(0), reply_markup=generate_answers_markup_by_id(0))
 
 
@dp.callback_query_handler(text_startswith="answer_", state=StateMachine.questions_started)
async def but_pressed(call: types.CallbackQuery, state: FSMContext):
    answer = int(call.data.split('_')[1])
 
    await call.message.edit_reply_markup(InlineKeyboardMarkup())
 
    async with state.proxy() as data:
        data["current_question"] += 1
        current_question = data["current_question"]
        data["answers"].append(answer)
 
    await call.message.answer(get_question_text_by_id(current_question), reply_markup=generate_answers_markup_by_id(current_question))
 
if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)
 

Comments