Skip to content

Commit efc9f7c

Browse files
Add gift_premium_with_stars method
1 parent 819f895 commit efc9f7c

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ def get_title_list(s: str) -> list:
434434
get_payment_form
435435
get_stars_balance
436436
get_upgraded_gift
437+
gift_premium_with_stars
437438
hide_gift
438439
search_gifts_for_resale
439440
send_gift

pyrogram/methods/payments/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .get_payment_form import GetPaymentForm
2727
from .get_stars_balance import GetStarsBalance
2828
from .get_upgraded_gift import GetUpgradedGift
29+
from .gift_premium_with_stars import GiftPremiumWithStars
2930
from .hide_gift import HideGift
3031
from .search_gifts_for_resale import SearchGiftsForResale
3132
from .send_gift import SendGift
@@ -49,6 +50,7 @@ class Payments(
4950
GetPaymentForm,
5051
GetStarsBalance,
5152
GetUpgradedGift,
53+
GiftPremiumWithStars,
5254
HideGift,
5355
SearchGiftsForResale,
5456
SendGift,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)