From c6d18fff5081f3fdf6b27af9e22de8bd5be588f2 Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Mon, 14 Oct 2024 14:19:55 +0300 Subject: [PATCH] Update API schema to Layer 190 --- compiler/api/source/main_api.tl | 8 ++--- .../types/messages_and_media/gift_code.py | 29 ++++++++++++++----- pyrogram/types/messages_and_media/message.py | 2 +- .../types/messages_and_media/star_gift.py | 22 ++------------ pyrogram/utils.py | 15 ++++++++++ 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/compiler/api/source/main_api.tl b/compiler/api/source/main_api.tl index 62d2302a0f..1c34daf883 100644 --- a/compiler/api/source/main_api.tl +++ b/compiler/api/source/main_api.tl @@ -177,13 +177,13 @@ messageActionSetChatTheme#aa786345 emoticon:string = MessageAction; messageActionChatJoinedByRequest#ebbca3cb = MessageAction; messageActionWebViewDataSentMe#47dd8079 text:string data:string = MessageAction; messageActionWebViewDataSent#b4c38cb5 text:string = MessageAction; -messageActionGiftPremium#c83d6aec flags:# currency:string amount:long months:int crypto_currency:flags.0?string crypto_amount:flags.0?long = MessageAction; +messageActionGiftPremium#6c6274fa flags:# currency:string amount:long months:int crypto_currency:flags.0?string crypto_amount:flags.0?long message:flags.1?TextWithEntities = MessageAction; messageActionTopicCreate#d999256 flags:# title:string icon_color:int icon_emoji_id:flags.0?long = MessageAction; messageActionTopicEdit#c0944820 flags:# title:flags.0?string icon_emoji_id:flags.1?long closed:flags.2?Bool hidden:flags.3?Bool = MessageAction; messageActionSuggestProfilePhoto#57de635e photo:Photo = MessageAction; messageActionRequestedPeer#31518e9b button_id:int peers:Vector = MessageAction; messageActionSetChatWallPaper#5060a3f4 flags:# same:flags.0?true for_both:flags.1?true wallpaper:WallPaper = MessageAction; -messageActionGiftCode#678c2e09 flags:# via_giveaway:flags.0?true unclaimed:flags.2?true boost_peer:flags.1?Peer months:int slug:string currency:flags.2?string amount:flags.2?long crypto_currency:flags.3?string crypto_amount:flags.3?long = MessageAction; +messageActionGiftCode#56d03994 flags:# via_giveaway:flags.0?true unclaimed:flags.2?true boost_peer:flags.1?Peer months:int slug:string currency:flags.2?string amount:flags.2?long crypto_currency:flags.3?string crypto_amount:flags.3?long message:flags.4?TextWithEntities = MessageAction; messageActionGiveawayLaunch#a80f51e4 flags:# stars:flags.0?long = MessageAction; messageActionGiveawayResults#87e2f155 flags:# stars:flags.0?true winners_count:int unclaimed_count:int = MessageAction; messageActionBoostApply#cc02aa6d boosts:int = MessageAction; @@ -1484,7 +1484,7 @@ help.premiumPromo#5334759c status_text:string status_entities:Vector boost_peer:flags.0?InputPeer currency:string amount:long = InputStorePaymentPurpose; +inputStorePaymentPremiumGiftCode#fb790393 flags:# users:Vector boost_peer:flags.0?InputPeer currency:string amount:long message:flags.1?TextWithEntities = InputStorePaymentPurpose; inputStorePaymentPremiumGiveaway#160544ca flags:# only_new_subscribers:flags.0?true winners_are_visible:flags.3?true boost_peer:InputPeer additional_peers:flags.1?Vector countries_iso2:flags.2?Vector prize_description:flags.4?string random_id:long until_date:int currency:string amount:long = InputStorePaymentPurpose; inputStorePaymentStarsTopup#dddd0f56 stars:long currency:string amount:long = InputStorePaymentPurpose; inputStorePaymentStarsGift#1d741ef7 user_id:InputUser stars:long currency:string amount:long = InputStorePaymentPurpose; @@ -2573,4 +2573,4 @@ smsjobs.finishJob#4f1ebf24 flags:# job_id:string error:flags.0?string = Bool; fragment.getCollectibleInfo#be1e85ba collectible:InputCollectible = fragment.CollectibleInfo; -// LAYER 189 +// LAYER 190 diff --git a/pyrogram/types/messages_and_media/gift_code.py b/pyrogram/types/messages_and_media/gift_code.py index 78ff502257..100abe58be 100644 --- a/pyrogram/types/messages_and_media/gift_code.py +++ b/pyrogram/types/messages_and_media/gift_code.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Optional, List + from pyrogram import raw, types, utils from ..object import Object @@ -32,13 +34,19 @@ class GiftCode(Object): You can combine it with `t.me/giftcode/{slug}` to get link for this gift. - via_giveaway (``bool``): + text (``str``, *optional*): + Text message. + + entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*): + For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text. + + via_giveaway (``bool``, *optional*): True if the gift code is received via giveaway. - is_unclaimed (``bool``): + is_unclaimed (``bool``, *optional*): True if the winner for the corresponding Telegram Premium subscription wasn't chosen. - boosted_chat (:obj:`~pyrogram.types.Chat`): + boosted_chat (:obj:`~pyrogram.types.Chat`, *optional*): The channel where the gift code was won. link (``str``, *property*): @@ -50,20 +58,24 @@ def __init__( *, months: int, slug: str, - via_giveaway: bool = None, - is_unclaimed: bool = None, - boosted_chat: "types.Chat" = None + text: Optional[str] = None, + entities: List["types.MessageEntity"] = None, + via_giveaway: Optional[bool] = None, + is_unclaimed: Optional[bool] = None, + boosted_chat: Optional["types.Chat"] = None ): super().__init__() self.months = months self.slug = slug + self.text = text + self.entities = entities self.via_giveaway = via_giveaway self.is_unclaimed = is_unclaimed self.boosted_chat = boosted_chat @staticmethod - def _parse(client, giftcode: "raw.types.MessageActionGiftCode", chats): + def _parse(client, giftcode: "raw.types.MessageActionGiftCode", users, chats): peer = chats.get(utils.get_raw_peer_id(getattr(giftcode, "boost_peer"))) return GiftCode( @@ -71,7 +83,8 @@ def _parse(client, giftcode: "raw.types.MessageActionGiftCode", chats): slug=giftcode.slug, via_giveaway=getattr(giftcode, "via_giveaway"), is_unclaimed=getattr(giftcode, "unclaimed"), - boosted_chat=types.Chat._parse_chat(client, peer) if peer else None + boosted_chat=types.Chat._parse_chat(client, peer) if peer else None, + **utils.parse_text_with_entities(client, getattr(giftcode, "message", None), users) ) @property diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 7623acc74f..3848d18247 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -789,7 +789,7 @@ async def _parse( stars_amount = getattr(action, "stars", None) service_type = enums.MessageServiceType.GIVEAWAY_LAUNCH elif isinstance(action, raw.types.MessageActionGiftCode): - gift_code = types.GiftCode._parse(client, action, chats) + gift_code = types.GiftCode._parse(client, action, users, chats) service_type = enums.MessageServiceType.GIFT_CODE elif isinstance(action, (raw.types.MessageActionRequestedPeer, raw.types.MessageActionRequestedPeerSentMe)): requested_chats = types.RequestedChats._parse(client, action) diff --git a/pyrogram/types/messages_and_media/star_gift.py b/pyrogram/types/messages_and_media/star_gift.py index d049d54860..94d3a531a6 100644 --- a/pyrogram/types/messages_and_media/star_gift.py +++ b/pyrogram/types/messages_and_media/star_gift.py @@ -136,14 +136,6 @@ async def _parse_user_star_gift( user_star_gift: "raw.types.UserStarGift", users: dict ) -> "StarGift": - text = None - entities = None - - if getattr(user_star_gift, "message", None): - text = user_star_gift.message.text - entities = [types.MessageEntity._parse(client, entity, users) for entity in user_star_gift.message.entities] - entities = types.List(filter(lambda x: x is not None, entities)) - doc = user_star_gift.gift.sticker attributes = {type(i): i for i in doc.attributes} @@ -159,9 +151,8 @@ async def _parse_user_star_gift( is_name_hidden=getattr(user_star_gift, "name_hidden", None), is_saved=not user_star_gift.unsaved if getattr(user_star_gift, "unsaved", None) else None, from_user=types.User._parse(client, users.get(user_star_gift.from_id)) if getattr(user_star_gift, "from_id", None) else None, - text=text, - entities=entities, message_id=getattr(user_star_gift, "msg_id", None), + **utils.parse_text_with_entities(client, getattr(user_star_gift, "message", None), users), client=client ) @@ -173,14 +164,6 @@ async def _parse_action( ) -> "StarGift": action = message.action - text = None - entities = None - - if getattr(action, "message", None): - text = action.message.text - entities = [types.MessageEntity._parse(client, entity, users) for entity in action.message.entities] - entities = types.List(filter(lambda x: x is not None, entities)) - doc = action.gift.sticker attributes = {type(i): i for i in doc.attributes} @@ -196,9 +179,8 @@ async def _parse_action( is_name_hidden=getattr(action, "name_hidden", None), is_saved=getattr(action, "saved", None), from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.peer_id))), - text=text, - entities=entities, message_id=message.id, + **utils.parse_text_with_entities(client, getattr(action, "message", None), users), client=client ) diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 58c0a08fb3..9fe5f0a20d 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -498,3 +498,18 @@ def get_first_url(text): matches = re.findall(r"(https?):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])", text) return f"{matches[0][0]}://{matches[0][1]}{matches[0][2]}" if matches else None + + +def parse_text_with_entities(client, message: "raw.types.TextWithEntities", users): + return { + "text": getattr(message, "text", None), + "entities": types.List( + filter( + lambda x: x is not None, + [ + types.MessageEntity._parse(client, entity, users) + for entity in getattr(message, "entities", []) + ] + ) + ) or None + }