2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 12:57:52 +00:00

Add back the ability to pass iterators to some methods

This commit is contained in:
Dan 2022-05-15 14:24:59 +02:00
parent 427738d02a
commit 5681ccefe1
5 changed files with 30 additions and 38 deletions

View File

@ -16,7 +16,7 @@
# 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, List
from typing import Union, Iterable
import pyrogram
from pyrogram import raw
@ -26,7 +26,7 @@ class DeleteMessages:
async def delete_messages(
self: "pyrogram.Client",
chat_id: Union[int, str],
message_ids: Union[int, List[int]],
message_ids: Union[int, Iterable[int]],
revoke: bool = True
) -> int:
"""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 a contact that exists in your Telegram address book you can use his phone number (str).
message_ids (``int`` | List of ``int``):
A list of Message identifiers to delete (integers) or a single message id.
message_ids (``int`` | Iterable of ``int``):
An iterable of message identifiers to delete (integers) or a single message id.
revoke (``bool``, *optional*):
Deletes messages on both parts.
@ -62,9 +62,7 @@ class DeleteMessages:
await app.delete_messages(chat_id, message_id, revoke=False)
"""
peer = await self.resolve_peer(chat_id)
# Follow type annotation of the raw function "DeleteMessage".
if not isinstance(message_ids, list):
message_ids = [message_ids]
message_ids = list(message_ids) if not isinstance(message_ids, int) else [message_ids]
if isinstance(peer, raw.types.InputPeerChannel):
r = await self.invoke(
@ -77,10 +75,8 @@ class DeleteMessages:
r = await self.invoke(
raw.functions.messages.DeleteMessages(
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

View File

@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
from typing import Union, List
from typing import Union, List, Iterable
import pyrogram
from pyrogram import raw, utils
@ -29,7 +29,7 @@ class ForwardMessages:
self: "pyrogram.Client",
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,
schedule_date: datetime = 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 a contact that exists in your Telegram address book you can use his phone number (str).
message_ids (``int`` | List of ``int``):
A list of Message identifiers in the chat specified in *from_chat_id* or a single message id.
message_ids (``int`` | Iterable of ``int``):
An iterable of message identifiers in the chat specified in *from_chat_id* or a single message id.
disable_notification (``bool``, *optional*):
Sends the message silently.
@ -74,9 +74,8 @@ class ForwardMessages:
await app.forward_messages(to_chat, from_chat, [1, 2, 3])
"""
is_list = isinstance(message_ids, list)
if not is_list:
message_ids = [message_ids]
is_iterable = not isinstance(message_ids, int)
message_ids = list(message_ids) if is_iterable else [message_ids]
r = await self.invoke(
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]

View File

@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import logging
from typing import Union, List
from typing import Union, List, Iterable
import pyrogram
from pyrogram import raw
@ -34,8 +34,8 @@ class GetMessages:
async def get_messages(
self: "pyrogram.Client",
chat_id: Union[int, str],
message_ids: Union[int, List[int]] = None,
reply_to_message_ids: Union[int, List[int]] = None,
message_ids: Union[int, Iterable[int]] = None,
reply_to_message_ids: Union[int, Iterable[int]] = None,
replies: int = 1
) -> Union["types.Message", List["types.Message"]]:
"""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 a contact that exists in your Telegram address book you can use his phone number (str).
message_ids (``int`` | List of ``int``, *optional*):
Pass a single message identifier or a list of message ids (as integers) to get the content of the
message_ids (``int`` | Iterable of ``int``, *optional*):
Pass a single message identifier or an iterable of message ids (as integers) to get the content of the
message themselves.
reply_to_message_ids (``int`` | List of ``int``, *optional*):
Pass a single message identifier or a list of message ids (as integers) to get the content of
reply_to_message_ids (``int`` | Iterable of ``int``, *optional*):
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.
If *message_ids* is set, this argument will be ignored.
@ -98,10 +98,8 @@ class GetMessages:
peer = await self.resolve_peer(chat_id)
is_list = isinstance(ids, list)
if not is_list:
ids = [ids]
is_iterable = not isinstance(ids, int)
ids = list(ids) if is_iterable else [ids]
ids = [ids_type(id=i) for i in ids]
if replies < 0:
@ -116,4 +114,4 @@ class GetMessages:
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

View File

@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import asyncio
from typing import Union, List
from typing import Union, List, Iterable
import pyrogram
from pyrogram import raw
@ -27,13 +27,13 @@ from pyrogram import types
class GetUsers:
async def get_users(
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"]]:
"""Get information about a user.
You can retrieve up to 200 users at once.
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.
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
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])
r = await self.invoke(
@ -67,4 +66,4 @@ class GetUsers:
for i in r:
users.append(types.User._parse(self, i))
return users if is_list else users[0]
return users if is_iterable else users[0]

View File

@ -113,7 +113,7 @@ async def parse_messages(client, messages: "raw.types.messages.Messages", replie
reply_messages = await client.get_messages(
chat_id,
reply_to_message_ids=list(messages_with_replies.keys()),
reply_to_message_ids=messages_with_replies.keys(),
replies=replies - 1
)