Skip to content

Commit

Permalink
Add missing Telegram WebApp API classes and fields (#1424) (#1425)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Latand committed Feb 26, 2024
1 parent a37deff commit a585fb0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES/1424.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
43 changes: 43 additions & 0 deletions aiogram/utils/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
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.
Expand All @@ -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."""
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_utils/test_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a585fb0

Please sign in to comment.