|
| 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