Skip to content

Commit 4f92dba

Browse files
Rename GiftCode to PremiumGiftCode, refactor and update to new layer
1 parent 1f3295f commit 4f92dba

5 files changed

Lines changed: 146 additions & 123 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ def get_title_list(s: str) -> list:
749749
Invoice
750750
LinkPreviewOptions
751751
GiftCollection
752-
GiftCode
752+
PremiumGiftCode
753753
GiftPurchaseLimit
754754
GiftResaleParameters
755755
GiftResalePrice

pyrogram/types/messages_and_media/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from .general_forum_topic_hidden import GeneralForumTopicHidden
5151
from .general_forum_topic_unhidden import GeneralForumTopicUnhidden
5252
from .gift_collection import GiftCollection
53-
from .gift_code import GiftCode
53+
from .premium_gift_code import PremiumGiftCode
5454
from .gift_purchase_limit import GiftPurchaseLimit
5555
from .gift_resale_parameters import GiftResaleParameters
5656
from .gift_resale_price import GiftResalePrice, GiftResalePriceStar, GiftResalePriceTon
@@ -171,7 +171,7 @@
171171
"GeneralForumTopicHidden",
172172
"GeneralForumTopicUnhidden",
173173
"GiftCollection",
174-
"GiftCode",
174+
"PremiumGiftCode",
175175
"GiftPurchaseLimit",
176176
"GiftResaleParameters",
177177
"GiftResalePrice",

pyrogram/types/messages_and_media/gift_code.py

Lines changed: 0 additions & 118 deletions
This file was deleted.

pyrogram/types/messages_and_media/message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def __init__(
669669
direct_message_price_changed: Optional["types.DirectMessagePriceChanged"] = None,
670670
checklist_tasks_done: Optional[List["types.ChecklistTasksDone"]] = None,
671671
checklist_tasks_added: Optional[List["types.ChecklistTasksAdded"]] = None,
672-
gift_code: Optional["types.GiftCode"] = None,
672+
gift_code: Optional["types.PremiumGiftCode"] = None,
673673
gifted_premium: Optional["types.GiftedPremium"] = None,
674674
gifted_stars: Optional["types.GiftedStars"] = None,
675675
gifted_ton: Optional["types.GiftedTon"] = None,
@@ -1042,7 +1042,7 @@ async def _parse_service(
10421042
proximity_alert_triggered = types.ProximityAlertTriggered._parse(client, action, users, chats)
10431043
elif isinstance(action, raw.types.MessageActionGiftCode):
10441044
service_type = enums.MessageServiceType.GIFT_CODE
1045-
gift_code = types.GiftCode._parse(client, action, users, chats)
1045+
gift_code = types.PremiumGiftCode._parse(client, action, users, chats)
10461046
elif isinstance(action, raw.types.MessageActionGiftPremium):
10471047
service_type = enums.MessageServiceType.GIFTED_PREMIUM
10481048
gifted_premium = await types.GiftedPremium._parse(
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import random
20+
from typing import Optional
21+
22+
from pyrogram import raw, types, utils
23+
from ..object import Object
24+
25+
26+
class PremiumGiftCode(Object):
27+
"""A Telegram Premium gift code was created for the user.
28+
29+
Parameters:
30+
creator (:obj:`~pyrogram.types.Chat`, *optional*):
31+
Identifier of a chat or a user that created the gift code.
32+
33+
text (:obj:`~pyrogram.types.FormattedText`, *optional*):
34+
Message added to the gift.
35+
36+
is_from_giveaway (``bool``, *optional*):
37+
True, if the gift code was created for a giveaway.
38+
39+
is_unclaimed (``bool``, *optional*):
40+
True, if the winner for the corresponding Telegram Premium subscription wasn't chosen.
41+
42+
currency (``str``, *optional*):
43+
Currency for the paid amount.
44+
45+
amount (``int``, *optional*):
46+
The paid amount, in the smallest units of the currency.
47+
48+
cryptocurrency (``str``, *optional*):
49+
Cryptocurrency used to pay for the gift.
50+
51+
cryptocurrency_amount (``int``, *optional*):
52+
The paid amount, in the smallest units of the cryptocurrency.
53+
54+
month_count (``int``):
55+
Number of months the Telegram Premium subscription will be active after code activation.
56+
57+
day_count (``int``):
58+
Number of days the Telegram Premium subscription will be active after code activation.
59+
60+
sticker (:obj:`~pyrogram.types.Sticker`, *optional*):
61+
A sticker to be shown in the message.
62+
63+
code (``str``):
64+
The gift code.
65+
66+
link (``str``, *property*):
67+
Generate a link to this gift code.
68+
"""
69+
70+
def __init__(
71+
self,
72+
*,
73+
creator: Optional["types.Chat"] = None,
74+
text: Optional["types.FormattedText"] = None,
75+
is_from_giveaway: Optional[bool] = None,
76+
is_unclaimed: Optional[bool] = None,
77+
currency: Optional[str] = None,
78+
amount: Optional[int] = None,
79+
cryptocurrency: Optional[str] = None,
80+
cryptocurrency_amount: Optional[int] = None,
81+
month_count: int,
82+
day_count: int,
83+
sticker: Optional["types.Sticker"] = None,
84+
code: str
85+
):
86+
super().__init__()
87+
88+
self.creator = creator
89+
self.text = text
90+
self.is_from_giveaway = is_from_giveaway
91+
self.is_unclaimed = is_unclaimed
92+
self.currency = currency
93+
self.amount = amount
94+
self.cryptocurrency = cryptocurrency
95+
self.cryptocurrency_amount = cryptocurrency_amount
96+
self.month_count = month_count
97+
self.day_count = day_count
98+
self.sticker = sticker
99+
self.code = code
100+
101+
@staticmethod
102+
async def _parse(client, giftcode: "raw.types.MessageActionGiftCode", users, chats):
103+
raw_peer_id = utils.get_raw_peer_id(giftcode.boost_peer)
104+
105+
raw_stickers = await client.invoke(
106+
raw.functions.messages.GetStickerSet(
107+
stickerset=raw.types.InputStickerSetPremiumGifts(),
108+
hash=0
109+
)
110+
)
111+
112+
return PremiumGiftCode(
113+
creator=types.Chat._parse_chat(client, users.get(raw_peer_id) or chats.get(raw_peer_id)),
114+
text=types.FormattedText._parse(client, giftcode.message),
115+
is_from_giveaway=giftcode.via_giveaway,
116+
is_unclaimed=giftcode.unclaimed,
117+
currency=giftcode.currency,
118+
amount=giftcode.amount,
119+
cryptocurrency=giftcode.crypto_currency,
120+
cryptocurrency_amount=giftcode.crypto_amount,
121+
month_count=utils.get_premium_duration_month_count(giftcode.days),
122+
day_count=giftcode.days,
123+
sticker=random.choice(
124+
types.List(
125+
[
126+
await types.Sticker._parse(
127+
client,
128+
doc,
129+
{
130+
type(i): i for i in doc.attributes
131+
}
132+
) for doc in raw_stickers.documents
133+
]
134+
)
135+
),
136+
code=giftcode.slug
137+
)
138+
139+
@property
140+
def link(self) -> str:
141+
return f"https://t.me/giftcode/{self.code}"

0 commit comments

Comments
 (0)