From 1cb36adf21665a819d5d27fd228ed29c955a9a8d Mon Sep 17 00:00:00 2001 From: JRoot Junior Date: Sun, 8 Sep 2024 03:51:37 +0300 Subject: [PATCH] Add support for purchased paid media events Enhanced the router to include `purchased_paid_media` handling. Added corresponding test cases to verify the new functionality and updated middleware to return the appropriate `EventContext`. --- aiogram/dispatcher/middlewares/user_context.py | 4 ++++ aiogram/dispatcher/router.py | 1 + tests/test_dispatcher/test_dispatcher.py | 13 +++++++++++++ 3 files changed, 18 insertions(+) diff --git a/aiogram/dispatcher/middlewares/user_context.py b/aiogram/dispatcher/middlewares/user_context.py index aa03ee60..68e4b4ab 100644 --- a/aiogram/dispatcher/middlewares/user_context.py +++ b/aiogram/dispatcher/middlewares/user_context.py @@ -175,4 +175,8 @@ class UserContextMiddleware(BaseMiddleware): ), business_connection_id=event.edited_business_message.business_connection_id, ) + if event.purchased_paid_media: + return EventContext( + user=event.purchased_paid_media.from_user, + ) return EventContext() diff --git a/aiogram/dispatcher/router.py b/aiogram/dispatcher/router.py index da6d7099..9a2796ba 100644 --- a/aiogram/dispatcher/router.py +++ b/aiogram/dispatcher/router.py @@ -102,6 +102,7 @@ class Router: "business_connection": self.business_connection, "edited_business_message": self.edited_business_message, "business_message": self.business_message, + "purchased_paid_media": self.purchased_paid_media, "error": self.errors, } diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py index 98467934..d1bb6f91 100644 --- a/tests/test_dispatcher/test_dispatcher.py +++ b/tests/test_dispatcher/test_dispatcher.py @@ -42,6 +42,7 @@ from aiogram.types import ( ShippingQuery, Update, User, + PaidMediaPurchased, ) from aiogram.types.error_event import ErrorEvent from tests.mocked_bot import MockedBot @@ -586,6 +587,18 @@ class TestDispatcher: True, True, ), + pytest.param( + "purchased_paid_media", + Update( + update_id=42, + purchased_paid_media=PaidMediaPurchased( + paid_media_payload="payload", + from_user=User(id=42, is_bot=False, first_name="Test"), + ), + ), + False, + True, + ), ], ) async def test_listen_update(