mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Add back the ability to pass iterators to some methods
This commit is contained in:
parent
427738d02a
commit
5681ccefe1
@ -16,7 +16,7 @@
|
|||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from typing import Union, List
|
from typing import Union, Iterable
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw
|
from pyrogram import raw
|
||||||
@ -26,7 +26,7 @@ class DeleteMessages:
|
|||||||
async def delete_messages(
|
async def delete_messages(
|
||||||
self: "pyrogram.Client",
|
self: "pyrogram.Client",
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
message_ids: Union[int, List[int]],
|
message_ids: Union[int, Iterable[int]],
|
||||||
revoke: bool = True
|
revoke: bool = True
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Delete messages, including service messages.
|
"""Delete messages, including service messages.
|
||||||
@ -37,8 +37,8 @@ class DeleteMessages:
|
|||||||
For your personal cloud (Saved Messages) you can simply use "me" or "self".
|
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).
|
For a contact that exists in your Telegram address book you can use his phone number (str).
|
||||||
|
|
||||||
message_ids (``int`` | List of ``int``):
|
message_ids (``int`` | Iterable of ``int``):
|
||||||
A list of Message identifiers to delete (integers) or a single message id.
|
An iterable of message identifiers to delete (integers) or a single message id.
|
||||||
|
|
||||||
revoke (``bool``, *optional*):
|
revoke (``bool``, *optional*):
|
||||||
Deletes messages on both parts.
|
Deletes messages on both parts.
|
||||||
@ -62,9 +62,7 @@ class DeleteMessages:
|
|||||||
await app.delete_messages(chat_id, message_id, revoke=False)
|
await app.delete_messages(chat_id, message_id, revoke=False)
|
||||||
"""
|
"""
|
||||||
peer = await self.resolve_peer(chat_id)
|
peer = await self.resolve_peer(chat_id)
|
||||||
# Follow type annotation of the raw function "DeleteMessage".
|
message_ids = list(message_ids) if not isinstance(message_ids, int) else [message_ids]
|
||||||
if not isinstance(message_ids, list):
|
|
||||||
message_ids = [message_ids]
|
|
||||||
|
|
||||||
if isinstance(peer, raw.types.InputPeerChannel):
|
if isinstance(peer, raw.types.InputPeerChannel):
|
||||||
r = await self.invoke(
|
r = await self.invoke(
|
||||||
@ -77,10 +75,8 @@ class DeleteMessages:
|
|||||||
r = await self.invoke(
|
r = await self.invoke(
|
||||||
raw.functions.messages.DeleteMessages(
|
raw.functions.messages.DeleteMessages(
|
||||||
id=message_ids,
|
id=message_ids,
|
||||||
revoke=revoke or None # Follow the type annotation.
|
revoke=revoke
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Deleting messages you don't have right onto won't raise any error.
|
|
||||||
# Check for pts_count, which is 0 in case deletes fail.
|
|
||||||
return r.pts_count
|
return r.pts_count
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Union, List
|
from typing import Union, List, Iterable
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw, utils
|
from pyrogram import raw, utils
|
||||||
@ -29,7 +29,7 @@ class ForwardMessages:
|
|||||||
self: "pyrogram.Client",
|
self: "pyrogram.Client",
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
from_chat_id: Union[int, str],
|
from_chat_id: Union[int, str],
|
||||||
message_ids: Union[int, List[int]],
|
message_ids: Union[int, Iterable[int]],
|
||||||
disable_notification: bool = None,
|
disable_notification: bool = None,
|
||||||
schedule_date: datetime = None,
|
schedule_date: datetime = None,
|
||||||
protect_content: bool = None
|
protect_content: bool = None
|
||||||
@ -47,8 +47,8 @@ class ForwardMessages:
|
|||||||
For your personal cloud (Saved Messages) you can simply use "me" or "self".
|
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).
|
For a contact that exists in your Telegram address book you can use his phone number (str).
|
||||||
|
|
||||||
message_ids (``int`` | List of ``int``):
|
message_ids (``int`` | Iterable of ``int``):
|
||||||
A list of Message identifiers in the chat specified in *from_chat_id* or a single message id.
|
An iterable of message identifiers in the chat specified in *from_chat_id* or a single message id.
|
||||||
|
|
||||||
disable_notification (``bool``, *optional*):
|
disable_notification (``bool``, *optional*):
|
||||||
Sends the message silently.
|
Sends the message silently.
|
||||||
@ -74,9 +74,8 @@ class ForwardMessages:
|
|||||||
await app.forward_messages(to_chat, from_chat, [1, 2, 3])
|
await app.forward_messages(to_chat, from_chat, [1, 2, 3])
|
||||||
"""
|
"""
|
||||||
|
|
||||||
is_list = isinstance(message_ids, list)
|
is_iterable = not isinstance(message_ids, int)
|
||||||
if not is_list:
|
message_ids = list(message_ids) if is_iterable else [message_ids]
|
||||||
message_ids = [message_ids]
|
|
||||||
|
|
||||||
r = await self.invoke(
|
r = await self.invoke(
|
||||||
raw.functions.messages.ForwardMessages(
|
raw.functions.messages.ForwardMessages(
|
||||||
@ -106,4 +105,4 @@ class ForwardMessages:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return types.List(forwarded_messages) if is_list else forwarded_messages[0]
|
return types.List(forwarded_messages) if is_iterable else forwarded_messages[0]
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Union, List
|
from typing import Union, List, Iterable
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw
|
from pyrogram import raw
|
||||||
@ -34,8 +34,8 @@ class GetMessages:
|
|||||||
async def get_messages(
|
async def get_messages(
|
||||||
self: "pyrogram.Client",
|
self: "pyrogram.Client",
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
message_ids: Union[int, List[int]] = None,
|
message_ids: Union[int, Iterable[int]] = None,
|
||||||
reply_to_message_ids: Union[int, List[int]] = None,
|
reply_to_message_ids: Union[int, Iterable[int]] = None,
|
||||||
replies: int = 1
|
replies: int = 1
|
||||||
) -> Union["types.Message", List["types.Message"]]:
|
) -> Union["types.Message", List["types.Message"]]:
|
||||||
"""Get one or more messages from a chat by using message identifiers.
|
"""Get one or more messages from a chat by using message identifiers.
|
||||||
@ -48,12 +48,12 @@ class GetMessages:
|
|||||||
For your personal cloud (Saved Messages) you can simply use "me" or "self".
|
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).
|
For a contact that exists in your Telegram address book you can use his phone number (str).
|
||||||
|
|
||||||
message_ids (``int`` | List of ``int``, *optional*):
|
message_ids (``int`` | Iterable of ``int``, *optional*):
|
||||||
Pass a single message identifier or a list of message ids (as integers) to get the content of the
|
Pass a single message identifier or an iterable of message ids (as integers) to get the content of the
|
||||||
message themselves.
|
message themselves.
|
||||||
|
|
||||||
reply_to_message_ids (``int`` | List of ``int``, *optional*):
|
reply_to_message_ids (``int`` | Iterable of ``int``, *optional*):
|
||||||
Pass a single message identifier or a list of message ids (as integers) to get the content of
|
Pass a single message identifier or an iterable of message ids (as integers) to get the content of
|
||||||
the previous message you replied to using this message.
|
the previous message you replied to using this message.
|
||||||
If *message_ids* is set, this argument will be ignored.
|
If *message_ids* is set, this argument will be ignored.
|
||||||
|
|
||||||
@ -98,10 +98,8 @@ class GetMessages:
|
|||||||
|
|
||||||
peer = await self.resolve_peer(chat_id)
|
peer = await self.resolve_peer(chat_id)
|
||||||
|
|
||||||
is_list = isinstance(ids, list)
|
is_iterable = not isinstance(ids, int)
|
||||||
if not is_list:
|
ids = list(ids) if is_iterable else [ids]
|
||||||
ids = [ids]
|
|
||||||
|
|
||||||
ids = [ids_type(id=i) for i in ids]
|
ids = [ids_type(id=i) for i in ids]
|
||||||
|
|
||||||
if replies < 0:
|
if replies < 0:
|
||||||
@ -116,4 +114,4 @@ class GetMessages:
|
|||||||
|
|
||||||
messages = await utils.parse_messages(self, r, replies=replies)
|
messages = await utils.parse_messages(self, r, replies=replies)
|
||||||
|
|
||||||
return messages if is_list else messages[0] if messages else None
|
return messages if is_iterable else messages[0] if messages else None
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import Union, List
|
from typing import Union, List, Iterable
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw
|
from pyrogram import raw
|
||||||
@ -27,13 +27,13 @@ from pyrogram import types
|
|||||||
class GetUsers:
|
class GetUsers:
|
||||||
async def get_users(
|
async def get_users(
|
||||||
self: "pyrogram.Client",
|
self: "pyrogram.Client",
|
||||||
user_ids: Union[int, str, List[Union[int, str]]]
|
user_ids: Union[int, str, Iterable[Union[int, str]]]
|
||||||
) -> Union["types.User", List["types.User"]]:
|
) -> Union["types.User", List["types.User"]]:
|
||||||
"""Get information about a user.
|
"""Get information about a user.
|
||||||
You can retrieve up to 200 users at once.
|
You can retrieve up to 200 users at once.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
user_ids (``int`` | ``str`` | List of ``int`` or ``str``):
|
user_ids (``int`` | ``str`` | Iterable of ``int`` or ``str``):
|
||||||
A list of User identifiers (id or username) or a single user id/username.
|
A list of User identifiers (id or username) or a single user id/username.
|
||||||
For a contact that exists in your Telegram address book you can use his phone number (str).
|
For a contact that exists in your Telegram address book you can use his phone number (str).
|
||||||
|
|
||||||
@ -50,10 +50,9 @@ class GetUsers:
|
|||||||
# Get information about multiple users at once
|
# Get information about multiple users at once
|
||||||
await app.get_users([user_id1, user_id2, user_id3])
|
await app.get_users([user_id1, user_id2, user_id3])
|
||||||
"""
|
"""
|
||||||
is_list = isinstance(user_ids, list)
|
|
||||||
if not is_list:
|
|
||||||
user_ids = [user_ids]
|
|
||||||
|
|
||||||
|
is_iterable = not isinstance(user_ids, (int, str))
|
||||||
|
user_ids = list(user_ids) if is_iterable else [user_ids]
|
||||||
user_ids = await asyncio.gather(*[self.resolve_peer(i) for i in user_ids])
|
user_ids = await asyncio.gather(*[self.resolve_peer(i) for i in user_ids])
|
||||||
|
|
||||||
r = await self.invoke(
|
r = await self.invoke(
|
||||||
@ -67,4 +66,4 @@ class GetUsers:
|
|||||||
for i in r:
|
for i in r:
|
||||||
users.append(types.User._parse(self, i))
|
users.append(types.User._parse(self, i))
|
||||||
|
|
||||||
return users if is_list else users[0]
|
return users if is_iterable else users[0]
|
||||||
|
@ -113,7 +113,7 @@ async def parse_messages(client, messages: "raw.types.messages.Messages", replie
|
|||||||
|
|
||||||
reply_messages = await client.get_messages(
|
reply_messages = await client.get_messages(
|
||||||
chat_id,
|
chat_id,
|
||||||
reply_to_message_ids=list(messages_with_replies.keys()),
|
reply_to_message_ids=messages_with_replies.keys(),
|
||||||
replies=replies - 1
|
replies=replies - 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user