|
| 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 | + |
| 20 | +from typing import List, Optional, Union |
| 21 | + |
| 22 | +import pyrogram |
| 23 | +from pyrogram import enums, raw, types, utils |
| 24 | + |
| 25 | + |
| 26 | +class GiftPremiumWithStars: |
| 27 | + async def gift_premium_with_stars( |
| 28 | + self: "pyrogram.Client", |
| 29 | + user_id: Union[int, str], |
| 30 | + month_count: int, |
| 31 | + text: str = None, |
| 32 | + parse_mode: Optional["enums.ParseMode"] = None, |
| 33 | + entities: List["types.MessageEntity"] = None, |
| 34 | + star_count: int = None, |
| 35 | + ) -> Optional["types.Message"]: |
| 36 | + """Allows to buy a Telegram Premium subscription for another user with payment in Telegram Stars. |
| 37 | +
|
| 38 | + .. include:: /_includes/usable-by/users-bots.rst |
| 39 | +
|
| 40 | + Parameters: |
| 41 | + user_id (``int`` | ``str``): |
| 42 | + Unique identifier (int) or username (str) of the target chat you want to transfer the star gift to. |
| 43 | + For your personal cloud (Saved Messages) you can simply use "me" or "self". |
| 44 | + For a contact that exists in your Telegram address book you can use his phone number (str). |
| 45 | +
|
| 46 | + month_count (``int``): |
| 47 | + Number of months the Telegram Premium subscription will be active for the user. |
| 48 | +
|
| 49 | + star_count (``int``, *optional*): |
| 50 | + The number of Telegram Stars to pay for subscription. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + :obj:`~pyrogram.types.Message`: On success, the sent message is returned. |
| 54 | +
|
| 55 | + Example: |
| 56 | + .. code-block:: python |
| 57 | +
|
| 58 | + await app.gift_premium_with_stars(user_id=123, month_count=3) |
| 59 | + """ |
| 60 | + text, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values() |
| 61 | + entities = entities or [] |
| 62 | + |
| 63 | + invoice = raw.types.InputInvoicePremiumGiftStars( |
| 64 | + user_id=await self.resolve_peer(user_id), |
| 65 | + months=month_count, |
| 66 | + message=raw.types.TextWithEntities( |
| 67 | + text=text, |
| 68 | + entities=entities, |
| 69 | + ) if text else None |
| 70 | + ) |
| 71 | + |
| 72 | + form = await self.invoke( |
| 73 | + raw.functions.payments.GetPaymentForm( |
| 74 | + invoice=invoice |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + if star_count is not None: |
| 79 | + if star_count < 0: |
| 80 | + raise ValueError("Invalid amount of Telegram Stars specified.") |
| 81 | + |
| 82 | + if form.invoice.prices[0].amount > star_count: |
| 83 | + raise ValueError("Have not enough Telegram Stars.") |
| 84 | + |
| 85 | + r = await self.invoke( |
| 86 | + raw.functions.payments.SendStarsForm( |
| 87 | + form_id=form.form_id, |
| 88 | + invoice=invoice |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + messages = await utils.parse_messages( |
| 93 | + client=self, |
| 94 | + messages=r.updates if isinstance(r, raw.types.payments.PaymentResult) else r |
| 95 | + ) |
| 96 | + |
| 97 | + return messages[0] if messages else None |
0 commit comments