From 715b2c3e4dccb4e4533a58f12d17774e8ac69d2d Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Fri, 19 Apr 2024 19:05:04 +0200 Subject: [PATCH] Add gifted_premium to Message --- compiler/docs/compiler.py | 5 + docs/source/releases/changes-in-this-fork.rst | 1 + pyrogram/enums/message_service_type.py | 3 + pyrogram/types/messages_and_media/__init__.py | 2 + .../messages_and_media/gifted_premium.py | 92 +++++++++++++++++++ pyrogram/types/messages_and_media/message.py | 13 +++ 6 files changed, 116 insertions(+) create mode 100644 pyrogram/types/messages_and_media/gifted_premium.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index d96f884cd..5ff86f766 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -330,6 +330,10 @@ def get_title_list(s: str) -> list: change_cloud_password remove_cloud_password """, + stickers=""" + Stickers + get_stickers + """, users=""" Users get_me @@ -493,6 +497,7 @@ def get_title_list(s: str) -> list: Document Game GiftCode + GiftedPremium Giveaway GiveawayCompleted GiveawayWinners diff --git a/docs/source/releases/changes-in-this-fork.rst b/docs/source/releases/changes-in-this-fork.rst index 73871f523..44549f67d 100644 --- a/docs/source/releases/changes-in-this-fork.rst +++ b/docs/source/releases/changes-in-this-fork.rst @@ -12,6 +12,7 @@ it can take advantage of new goodies! | Scheme layer used: 177 | +------------------------+ +- Added ``gifted_premium`` service message to :obj:`~pyrogram.types.Message`. - Added :meth:`~pyrogram.Client.get_stickers`. - Added ``filters.users_shared`` and ``filters.chat_shared``. - Added the field ``origin`` of type :obj:`~pyrogram.types.MessageOrigin`` in the class :obj:`~pyrogram.types.ExternalReplyInfo`. diff --git a/pyrogram/enums/message_service_type.py b/pyrogram/enums/message_service_type.py index f262f526c..ead595109 100644 --- a/pyrogram/enums/message_service_type.py +++ b/pyrogram/enums/message_service_type.py @@ -69,6 +69,9 @@ class MessageServiceType(AutoName): GIFT_CODE = auto() "Gift code" + GIFTED_PREMIUM = auto() + "Gifted Premium" + VIDEO_CHAT_STARTED = auto() "Video chat started" diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index c07bad8c0..b1f3882b3 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -53,6 +53,7 @@ from .giveaway_completed import GiveawayCompleted from .giveaway_winners import GiveawayWinners from .gift_code import GiftCode +from .gifted_premium import GiftedPremium __all__ = [ "Animation", @@ -63,6 +64,7 @@ "Document", "Game", "GiftCode", + "GiftedPremium", "Giveaway", "GiveawayCompleted", "GiveawayWinners", diff --git a/pyrogram/types/messages_and_media/gifted_premium.py b/pyrogram/types/messages_and_media/gifted_premium.py new file mode 100644 index 000000000..b742b3351 --- /dev/null +++ b/pyrogram/types/messages_and_media/gifted_premium.py @@ -0,0 +1,92 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from random import choice + +from pyrogram import raw, types +from ..object import Object + + +class GiftedPremium(Object): + """Telegram Premium was gifted to the user + + Parameters: + gifter_user_id (``int``): + The identifier of a user that gifted Telegram Premium; 0 if the gift was anonymous + + currency (``str``): + Currency for the paid amount + + amount (``int``): + The paid amount, in the smallest units of the currency + + cryptocurrency (``str``): + Cryptocurrency used to pay for the gift; may be empty if none + + cryptocurrency_amount (``int``): + The paid amount, in the smallest units of the cryptocurrency; 0 if none + + month_count (``int``): + Number of months the Telegram Premium subscription will be active + + sticker (:obj:`~pyrogram.types.Sticker`): + A sticker to be shown in the message; may be null if unknown + + """ + + def __init__( + self, + *, + gifter_user_id: int = None, + currency: str = None, + amount: int = None, + cryptocurrency: str = None, + cryptocurrency_amount: int = None, + month_count: int = None, + sticker: "types.Sticker" = None, + ): + super().__init__() + + self.gifter_user_id = gifter_user_id + self.currency = currency + self.amount = amount + self.cryptocurrency = cryptocurrency + self.cryptocurrency_amount = cryptocurrency_amount + self.month_count = month_count + self.sticker = sticker + + @staticmethod + async def _parse( + client, + gifted_premium: "raw.types.MessageActionGiftPremium", + gifter_user_id: int + ) -> "GiftedPremium": + sticker = None + stickers, _ = await client._get_raw_stickers( + raw.types.InputStickerSetPremiumGifts() + ) + sticker = choice(stickers) + return GiftedPremium( + gifter_user_id=gifter_user_id, + currency=gifted_premium.currency, + amount=gifted_premium.amount, + cryptocurrency=getattr(gifted_premium, "crypto_currency", None), + cryptocurrency_amount=getattr(gifted_premium, "crypto_amount", None), + month_count=gifted_premium.months, + sticker=sticker + ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 369fa7617..4c1e5e24c 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -380,6 +380,12 @@ class Message(Object, Update): custom_action (``str``, *optional*): Custom action (most likely not supported by the current layer, an upgrade might be needed) + gift_code (:obj:`~pyrogram.types.GiftCode`, *optional*): + Contains a `Telegram Premium giftcode link `_. + + gifted_premium (:obj:`~pyrogram.types.GiftedPremium`, *optional*): + Info about a gifted Telegram Premium subscription + link (``str``, *property*): Generate a link to this message, only for groups and channels. @@ -481,6 +487,7 @@ def __init__( ] = None, gift_code: "types.GiftCode" = None, + gifted_premium: "types.GiftedPremium" = None, chat_ttl_period: int = None, chat_ttl_setting_from: "types.User" = None, empty: bool = None, @@ -584,6 +591,7 @@ def __init__( self.giveaway_completed = giveaway_completed self.giveaway_winners = giveaway_winners self.gift_code = gift_code + self.gifted_premium = gifted_premium self.forum_topic_created = forum_topic_created self.forum_topic_edited = forum_topic_edited self.forum_topic_closed = forum_topic_closed @@ -658,6 +666,7 @@ async def _parse( video_chat_participants_invited = None web_app_data = None gift_code = None + gifted_premium = None giveaway_created = None users_shared = None chat_shared = None @@ -735,6 +744,9 @@ async def _parse( elif isinstance(action, raw.types.MessageActionGiftCode): gift_code = types.GiftCode._parse(client, action, chats) service_type = enums.MessageServiceType.GIFT_CODE + elif isinstance(action, raw.types.MessageActionGiftPremium): + gifted_premium = await types.GiftedPremium._parse(client, action, from_user.id) + service_type = enums.MessageServiceType.GIFTED_PREMIUM elif ( isinstance(action, raw.types.MessageActionRequestedPeer) or @@ -910,6 +922,7 @@ async def _parse( giveaway_created=giveaway_created, giveaway_completed=giveaway_completed, gift_code=gift_code, + gifted_premium=gifted_premium, users_shared=users_shared, chat_shared=chat_shared, chat_ttl_period=chat_ttl_period,