wrapper.py 1009 B

12345678910111213141516171819202122232425262728
  1. from aiogram import types
  2. from aiogram.dispatcher.middlewares import BaseMiddleware
  3. from aiogram.dispatcher.storage import FSMContext
  4. from app.models.user import User
  5. from app.utils.wrapper_vshkole import WrapperForBot, create_wrapper_for_bot
  6. class WrapperMiddleware(BaseMiddleware):
  7. async def setup_wrapper(
  8. self, data: dict, command, user: User, state: FSMContext, keyboard: dict,
  9. ):
  10. if command is None or (command is not None and command.command != "start"):
  11. w = await create_wrapper_for_bot(user, state)
  12. data["wrapper"] = w
  13. data["keyboard"] = keyboard
  14. async def on_process_message(self, message: types.Message, data: dict):
  15. command = data.get("command")
  16. user = data.get("user")
  17. state: FSMContext = data.get("state")
  18. state_data: dict = await state.get_data()
  19. keyboard: dict = state_data.get("Keyboard")
  20. await self.setup_wrapper(
  21. data, command, user, state, keyboard,
  22. )