From a585fb08ded9fe9168e35305e536b79c42997a8e Mon Sep 17 00:00:00 2001 From: Kostiantyn Kriuchkov <36363097+Latand@users.noreply.github.com> Date: Mon, 26 Feb 2024 02:13:32 +0200 Subject: [PATCH] Add missing Telegram WebApp API classes and fields (#1424) (#1425) * Add missing Telegram WebApp API classes and fields (#1424) * Implemented `WebAppChat` class with fields `id`, `type`, `title`, `username`, and `photo_url` as per Telegram documentation. * Modified `WebAppUser` class by adding `is_premium`, `added_to_attachment_menu`, and `allows_write_to_pm` fields. * Modified `WebAppInitData` class to include `chat`, `chat_type`, `chat_instance` fields for full API support. * fix changelog file name * fix line too long --- CHANGES/1424.bugfix.rst | 5 ++++ aiogram/utils/web_app.py | 43 ++++++++++++++++++++++++++++++++ tests/test_utils/test_web_app.py | 8 ++++++ 3 files changed, 56 insertions(+) create mode 100644 CHANGES/1424.bugfix.rst diff --git a/CHANGES/1424.bugfix.rst b/CHANGES/1424.bugfix.rst new file mode 100644 index 00000000..3055f50d --- /dev/null +++ b/CHANGES/1424.bugfix.rst @@ -0,0 +1,5 @@ +- **WebAppUser Class Fields**: Added missing `is_premium`, `added_to_attachment_menu`, and `allows_write_to_pm` fields to `WebAppUser` class to align with the Telegram API. + +- **WebAppChat Class Implementation**: Introduced the `WebAppChat` class with all its fields (`id`, `type`, `title`, `username`, and `photo_url`) as specified in the Telegram API, which was previously missing from the library. + +- **WebAppInitData Class Fields**: Included previously omitted fields in the `WebAppInitData` class: `chat`, `chat_type`, `chat_instance`, to match the official documentation for a complete Telegram Web Apps support. diff --git a/aiogram/utils/web_app.py b/aiogram/utils/web_app.py index d21316f9..192776fd 100644 --- a/aiogram/utils/web_app.py +++ b/aiogram/utils/web_app.py @@ -9,6 +9,29 @@ from urllib.parse import parse_qsl from aiogram.types import TelegramObject +class WebAppChat(TelegramObject): + """ + This object represents a chat. + + Source: https://core.telegram.org/bots/webapps#webappchat + """ + + id: int + """Unique identifier for this chat. This number may have more than 32 significant bits + and some programming languages may have difficulty/silent defects in interpreting it. + But it has at most 52 significant bits, so a signed 64-bit integer or double-precision + float type are safe for storing this identifier.""" + type: str + """Type of chat, can be either “group”, “supergroup” or “channel”""" + title: str + """Title of the chat""" + username: Optional[str] = None + """Username of the chat""" + photo_url: Optional[str] = None + """URL of the chat’s photo. The photo can be in .jpeg or .svg formats. + Only returned for Web Apps launched from the attachment menu.""" + + class WebAppUser(TelegramObject): """ This object contains the data of the Web App user. @@ -31,6 +54,12 @@ class WebAppUser(TelegramObject): """Username of the user or bot.""" language_code: Optional[str] = None """IETF language tag of the user's language. Returns in user field only.""" + is_premium: Optional[bool] = None + """True, if this user is a Telegram Premium user.""" + added_to_attachment_menu: Optional[bool] = None + """True, if this user added the bot to the attachment menu.""" + allows_write_to_pm: Optional[bool] = None + """True, if this user allowed the bot to message them.""" photo_url: Optional[str] = None """URL of the user’s profile photo. The photo can be in .jpeg or .svg formats. Only returned for Web Apps launched from the attachment menu.""" @@ -53,11 +82,25 @@ class WebAppInitData(TelegramObject): """An object containing data about the chat partner of the current user in the chat where the bot was launched via the attachment menu. Returned only for Web Apps launched via the attachment menu.""" + chat: Optional[WebAppChat] = None + """An object containing data about the chat where the bot was launched via the attachment menu. + Returned for supergroups, channels, and group chats – only for Web Apps launched via the + attachment menu.""" + chat_type: Optional[str] = None + """Type of the chat from which the Web App was opened. + Can be either “sender” for a private chat with the user opening the link, + “private”, “group”, “supergroup”, or “channel”. + Returned only for Web Apps launched from direct links.""" + chat_instance: Optional[str] = None + """Global identifier, uniquely corresponding to the chat from which the Web App was opened. + Returned only for Web Apps launched from a direct link.""" start_param: Optional[str] = None """The value of the startattach parameter, passed via link. Only returned for Web Apps when launched from the attachment menu via link. The value of the start_param parameter will also be passed in the GET-parameter tgWebAppStartParam, so the Web App can load the correct interface right away.""" + can_send_after: Optional[int] = None + """Time in seconds, after which a message can be sent via the answerWebAppQuery method.""" auth_date: datetime """Unix time when the form was opened.""" hash: str diff --git a/tests/test_utils/test_web_app.py b/tests/test_utils/test_web_app.py index abebd909..4b7c94c2 100644 --- a/tests/test_utils/test_web_app.py +++ b/tests/test_utils/test_web_app.py @@ -20,6 +20,14 @@ class TestWebApp: "&hash=46d2ea5e32911ec8d30999b56247654460c0d20949b6277af519e76271182803", True, ], + [ + "42:TEST", + "auth_date=1650385342" + "&user=%7B%22id%22%3A+%22123456789%22%2C+%22first_name%22%3A+%22PlaceholderFirstName%22%2C+%22last_name%22%3A+%22PlaceholderLastName+%5Cud83c%5Cuddfa%5Cud83c%5Cudde6%22%2C+%22username%22%3A+%22Latand%22%2C+%22language_code%22%3A+%22en%22%2C+%22is_premium%22%3A+%22true%22%2C+%22allows_write_to_pm%22%3A+%22true%22%7D" + "&query_id=test" + "&hash=b3c8b293f14ad0f7f0abcf769aea1209a72295d30a87eb0e74df855d32e53bfe", + True, + ], [ "42:INVALID", "auth_date=1650385342"