Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware does not work on aiogram 3.4.1 #28

Open
maksachve opened this issue Feb 25, 2024 · 0 comments
Open

Middleware does not work on aiogram 3.4.1 #28

maksachve opened this issue Feb 25, 2024 · 0 comments

Comments

@maksachve
Copy link

maksachve commented Feb 25, 2024

Creating a bot:

def bot(self) -> Bot:
        session: AiohttpSession = AiohttpSession(
            json_loads=mjson.decode,
            json_dumps=mjson.encode
        )
        session.middleware(AiogramSulgukMiddleware())
        session.middleware(RetryRequestMiddleware())
        
        bot: Bot = Bot(
            token=self.settings.bot_token.get_secret_value(),
            session=session,
            default=DefaultBotProperties(
                parse_mode=SULGUK_PARSE_MODE
            )
        )
        
        return bot

Sending a message:

async def cmd_start(
    event: types.Message
) -> None:
    await event.answer("Hello!")

Ошибка:

[25.02.2024 13:28:54] ERROR | asyncio:                                          Task exception was never retrieved                                              future: <Task finished name='Task-8' coro=<BaseRequestHandler._background_feed_update() done, defined at /home/FMR/application/webhook.py:75> exception=TelegramBadRequest('Telegram server says - Bad Request: unsupported parse_mode')>       Traceback (most recent call last):                                                File "/home/FMR/application/webhook.py", line 76, in _background_feed_update      result = await self.dp.feed_raw_update(bot=bot, update=update, **self.data)   File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py", line 189, in feed_raw_update                                                 return await self.feed_update(bot=bot, update=parsed_update, **kwargs)        File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py", line 158, in feed_update                                                     response = await self.update.wrap_outer_middleware(                           File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/middlewares/error.py", line 25, in __call__                                                  return await handler(event, data)                                             File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/middlewares/user_context.py", line 27, in __call__                                           return await handler(event, data)                                             File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/fsm/middleware.py", line 41, in __call__                                                                return await handler(event, data)                                             File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger                                                     return await wrapped_inner(event, kwargs)                                     File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/event/handler.py", line 43, in call
    return await wrapped()
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py", line 276, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/router.py", line 128, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/router.py", line 123, in _wrapped
    return await self._propagate_event(
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/router.py", line 156, in _propagate_event
    response = await router.propagate_event(update_type=update_type, event=event, **kwargs)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/router.py", line 128, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/router.py", line 123, in _wrapped
    return await self._propagate_event(
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/router.py", line 148, in _propagate_event
    response = await observer.trigger(event, **kwargs)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/dispatcher/event/handler.py", line 43, in call
    return await wrapped()
  File "/home/FMR/bot/handlers/start.py", line 9, in cmd_start
    await event.answer("Hello!")
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/methods/base.py", line 84, in emit
    return await bot(self)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/client/bot.py", line 492, in __call__
    return await self.session(self, method, timeout=request_timeout)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/client/session/base.py", line 254, in __call__
    return cast(TelegramType, await middleware(bot, method))
  File "/home/FMR/venv/lib/python3.10/site-packages/sulguk/aiogram_middleware.py", line 50, in __call__
    return await make_request(bot, method)
  File "/home/FMR/bot/middlewares/request/retry.py", line 51, in __call__
    return await make_request(bot, method)
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/client/session/aiohttp.py", line 178, in make_request
    response = self.check_response(
  File "/home/FMR/venv/lib/python3.10/site-packages/aiogram/client/session/base.py", line 120, in check_response
    raise TelegramBadRequest(method=method, message=description)
aiogram.exceptions.TelegramBadRequest: Telegram server says - Bad Request: unsupported parse_mode

If you additionally specify sulguk as the parse_mode of the message sending method, then there are no errors:

async def cmd_start(
    event: types.Message
) -> None:
    await event.answer("Hello!", parse_mode=SULGUK_PARSE_MODE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant