-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wulan17 <[email protected]>
- Loading branch information
1 parent
9e8b3c3
commit f7b14d7
Showing
11 changed files
with
274 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# 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, types | ||
|
||
|
||
class GetUserStarGifts: | ||
async def get_user_star_gifts( | ||
self: "pyrogram.Client", | ||
chat_id: Union[int, str], | ||
limit: int = 0, | ||
offset: str = "" | ||
): | ||
"""Get user star gifts. | ||
.. include:: /_includes/usable-by/users.rst | ||
Parameters: | ||
chat_id (``int`` | ``str``): | ||
Unique identifier (int) or username (str) of the target chat. | ||
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 results to be returned. | ||
limit (``int``, *optional*): | ||
Maximum amount of star gifts to be returned. | ||
Returns: | ||
``Generator``: A generator yielding :obj:`~pyrogram.types.UserStarGift` objects. | ||
Example: | ||
.. code-block:: python | ||
async for gift in app.get_user_star_gifts(chat_id): | ||
print(gift) | ||
""" | ||
peer = await self.resolve_peer(chat_id) | ||
|
||
if not isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerSelf)): | ||
raise ValueError("chat_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=60 | ||
) | ||
|
||
users = {u.id: u for u in r.users} | ||
|
||
user_star_gifts = [ | ||
await types.UserStarGift._parse(self, gift, users) | ||
for gift in r.gifts | ||
] | ||
|
||
if not user_star_gifts: | ||
return | ||
|
||
offset = r.next_offset | ||
|
||
for gift in user_star_gifts: | ||
yield gift | ||
|
||
current += 1 | ||
|
||
if current >= total: | ||
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,64 @@ | ||
# 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/>. | ||
|
||
import logging | ||
from typing import Union | ||
|
||
import pyrogram | ||
from pyrogram import raw | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class GetUserStarGiftsCount: | ||
async def get_user_star_gifts_count( | ||
self: "pyrogram.Client", | ||
chat_id: Union[int, str] | ||
) -> int: | ||
"""Get the total count of star gifts of specified user. | ||
.. include:: /_includes/usable-by/users.rst | ||
Parameters: | ||
chat_id (``int`` | ``str``): | ||
Unique identifier (int) or username (str) of the target chat. | ||
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). | ||
Returns: | ||
``int``: On success, the star gifts count is returned. | ||
Example: | ||
.. code-block:: python | ||
await app.get_user_star_gifts_count(chat_id) | ||
""" | ||
peer = await self.resolve_peer(chat_id) | ||
|
||
if not isinstance(peer, (raw.types.InputPeerUser, raw.types.InputPeerSelf)): | ||
raise ValueError("chat_id must belong to a user.") | ||
|
||
r = await self.invoke( | ||
raw.functions.payments.GetUserStarGifts( | ||
user_id=peer, | ||
offset="", | ||
limit=1 | ||
) | ||
) | ||
|
||
return r.count |
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,99 @@ | ||
# 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 datetime import datetime | ||
from typing import Optional | ||
|
||
import pyrogram | ||
from pyrogram import raw | ||
from pyrogram import types | ||
from pyrogram import utils | ||
from ..object import Object | ||
|
||
|
||
class UserStarGift(Object): | ||
"""A user star gift. | ||
Parameters: | ||
date (``datetime``): | ||
Date when the star gift was received. | ||
star_gift (:obj:`~pyrogram.types.StarGift`, *optional*): | ||
Information about the star gift. | ||
is_name_hidden (``bool``, *optional*): | ||
True, if the sender's name is hidden. | ||
is_saved (``bool``, *optional*): | ||
True, if the star gift is saved in profile. | ||
from_user (:obj:`~pyrogram.types.User`, *optional*): | ||
User who sent the star gift. | ||
text (``str``, *optional*): | ||
Text message. | ||
message_id (``int``, *optional*): | ||
Unique message identifier. | ||
convert_price (``int``, *optional*): | ||
The number of stars you get if you convert this gift. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
client: "pyrogram.Client" = None, | ||
date: datetime, | ||
star_gift: "types.StarGift", | ||
is_name_hidden: Optional[bool] = None, | ||
is_saved: Optional[bool] = None, | ||
from_user: Optional["types.User"] = None, | ||
text: Optional[str] = None, | ||
message_id: Optional[int] = None, | ||
convert_price: Optional[int] = None | ||
): | ||
super().__init__(client) | ||
|
||
self.date = date | ||
self.star_gift = star_gift | ||
self.is_name_hidden = is_name_hidden | ||
self.is_saved = is_saved | ||
self.from_user = from_user | ||
self.text = text | ||
self.message_id = message_id | ||
self.convert_price = convert_price | ||
|
||
@staticmethod | ||
async def _parse( | ||
client, | ||
user_star_gift: "raw.types.UserStarGift", | ||
users: dict | ||
) -> "UserStarGift": | ||
# TODO: Add entities support | ||
return UserStarGift( | ||
date=utils.timestamp_to_datetime(user_star_gift.date), | ||
star_gift=await types.StarGift._parse(client, user_star_gift.gift), | ||
is_name_hidden=getattr(user_star_gift, "name_hidden", None), | ||
is_saved=not user_star_gift.unsaved if getattr(user_star_gift, "unsaved", None) else None, | ||
from_user=types.User._parse(client, users.get(user_star_gift.from_id)) if getattr(user_star_gift, "from_id", None) else None, | ||
text=user_star_gift.message.text if getattr(user_star_gift, "message", None) else None, | ||
message_id=getattr(user_star_gift, "msg_id", None), | ||
convert_price=getattr(user_star_gift, "convert_stars", None), | ||
client=client | ||
) |