-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Steal Star Gifts changes from KurimuzonAkuma fork (#100)
Co-authored-by: KurimuzonAkuma <[email protected]>
- Loading branch information
1 parent
c8cb9b5
commit 58e1f55
Showing
18 changed files
with
749 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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 typing import List | ||
|
||
import pyrogram | ||
from pyrogram import raw, types | ||
|
||
|
||
class GetAvailableGifts: | ||
async def get_available_gifts( | ||
self: "pyrogram.Client", | ||
) -> List["types.Gift"]: | ||
"""Get all gifts that can be sent to other users. | ||
.. include:: /_includes/usable-by/users.rst | ||
Returns: | ||
List of :obj:`~pyrogram.types.Gift`: On success, a list of star gifts is returned. | ||
Example: | ||
.. code-block:: python | ||
app.get_available_gifts() | ||
""" | ||
r = await self.invoke( | ||
raw.functions.payments.GetStarGifts(hash=0) | ||
) | ||
|
||
return types.List([await types.Gift._parse(self, gift) for gift in r.gifts]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# 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 typing import Union, Optional, AsyncGenerator | ||
|
||
import pyrogram | ||
from pyrogram import raw, types | ||
|
||
|
||
class GetUserGifts: | ||
async def get_user_gifts( | ||
self: "pyrogram.Client", | ||
user_id: Union[int, str], | ||
offset: str = "", | ||
limit: int = 0, | ||
) -> Optional[AsyncGenerator["types.UserGift", None]]: | ||
"""Get gifts saved to profile by the given user. | ||
.. include:: /_includes/usable-by/users.rst | ||
Parameters: | ||
user_id (``int`` | ``str``): | ||
Unique identifier (int) or username (str) of the target user. | ||
For your personal cloud (Saved Messages) you can simply use "me" or "self". | ||
For a contact that exists in your Telegram address book you can use his phone number (str). | ||
offset (``str``, *optional*): | ||
Offset of the first entry to return as received from the previous request; use empty string to get the first chunk of results. | ||
limit (``int``, *optional*): | ||
The maximum number of gifts to be returned; must be positive and can't be greater than 100. For optimal performance, the number of returned objects is chosen by Telegram Server and can be smaller than the specified limit. | ||
Returns: | ||
``Generator``: A generator yielding :obj:`~pyrogram.types.UserGift` objects. | ||
Example: | ||
.. code-block:: python | ||
async for user_gift in app.get_user_gifts(user_id): | ||
print(user_gift) | ||
""" | ||
peer = await self.resolve_peer(user_id) | ||
|
||
if not isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerSelf)): | ||
raise ValueError("user_id must belong to a user.") | ||
|
||
current = 0 | ||
total = abs(limit) or (1 << 31) - 1 | ||
limit = min(100, total) | ||
|
||
while True: | ||
r = await self.invoke( | ||
raw.functions.payments.GetUserStarGifts( | ||
user_id=peer, | ||
offset=offset, | ||
limit=limit | ||
), | ||
sleep_threshold=max(60, self.sleep_threshold) | ||
) | ||
|
||
users = {u.id: u for u in r.users} | ||
|
||
user_gifts = [ | ||
await types.UserGift._parse(self, gift, users) | ||
for gift in r.gifts | ||
] | ||
|
||
if not user_gifts: | ||
return | ||
|
||
for user_gift in user_gifts: | ||
yield user_gift | ||
|
||
current += 1 | ||
|
||
if current >= total: | ||
return | ||
|
||
offset = r.next_offset | ||
|
||
if not offset: | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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 typing import Union | ||
|
||
import pyrogram | ||
from pyrogram import raw | ||
|
||
|
||
class SellGift: | ||
async def sell_gift( | ||
self: "pyrogram.Client", | ||
sender_user_id: Union[int, str], | ||
message_id: int | ||
) -> bool: | ||
"""Sells a gift received by the current user for Telegram Stars. | ||
.. include:: /_includes/usable-by/users.rst | ||
Parameters: | ||
sender_user_id (``int`` | ``str``): | ||
Unique identifier (int) or username (str) of the user that sent the gift. | ||
For your personal cloud (Saved Messages) you can simply use "me" or "self". | ||
For a contact that exists in your Telegram address book you can use his phone number (str). | ||
message_id (``int``): | ||
Unique identifier of the message with the gift in the chat with the user. | ||
Returns: | ||
``bool``: On success, True is returned. | ||
Example: | ||
.. code-block:: python | ||
# Convert gift | ||
app.sell_gift(sender_user_id=user_id, message_id=123) | ||
""" | ||
peer = await self.resolve_peer(sender_user_id) | ||
|
||
if not isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerSelf)): | ||
raise ValueError("sender_user_id must belong to a user.") | ||
|
||
r = await self.invoke( | ||
raw.functions.payments.ConvertStarGift( | ||
user_id=peer, | ||
msg_id=message_id | ||
) | ||
) | ||
|
||
return r |
Oops, something went wrong.