Skip to content

Commit

Permalink
Add gifted_premium to Message
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Apr 19, 2024
1 parent ec4866f commit 715b2c3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -493,6 +497,7 @@ def get_title_list(s: str) -> list:
Document
Game
GiftCode
GiftedPremium
Giveaway
GiveawayCompleted
GiveawayWinners
Expand Down
1 change: 1 addition & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 3 additions & 0 deletions pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class MessageServiceType(AutoName):
GIFT_CODE = auto()
"Gift code"

GIFTED_PREMIUM = auto()
"Gifted Premium"

VIDEO_CHAT_STARTED = auto()
"Video chat started"

Expand Down
2 changes: 2 additions & 0 deletions pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -63,6 +64,7 @@
"Document",
"Game",
"GiftCode",
"GiftedPremium",
"Giveaway",
"GiveawayCompleted",
"GiveawayWinners",
Expand Down
92 changes: 92 additions & 0 deletions pyrogram/types/messages_and_media/gifted_premium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

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
)
13 changes: 13 additions & 0 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://core.telegram.org/api/links#premium-giftcode-links>`_.
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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 715b2c3

Please sign in to comment.