Skip to content

Commit 758f2d2

Browse files
authored
Rewrite get_messages method (#145)
Really Closes #116
1 parent 7516f4e commit 758f2d2

File tree

5 files changed

+41
-56
lines changed

5 files changed

+41
-56
lines changed

docs/source/releases/changes-in-this-fork.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Changes in this Fork
2626
| Scheme layer used: 198 |
2727
+------------------------+
2828

29+
- Added the parameter ``pinned`` and made the parameter ``chat_id`` optional in :meth:`~pyrogram.Client.get_messages`. **NOTE**: Please be aware about using the correct :doc:`Message Identifiers <../../topics/message-identifiers>`, when using this method.
2930
- Added the ``cover`` and ``start_timestamp`` parameters in :meth:`~pyrogram.Client.send_video` and :obj:`~pyrogram.types.InputPaidMediaVideo`.
3031
- Added the ``new_video_start_timestamp`` and renamed the ``send_copy`` and ``remove_caption`` parameters in :meth:`~pyrogram.Client.forward_messages` and :meth:`~pyrogram.types.Message.forward`.
3132
- Added the ``gift_count`` to the :obj:`~pyrogram.types.Chat`.

pyrogram/methods/messages/get_messages.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async def get_messages(
3333
chat_id: Union[int, str] = None,
3434
message_ids: Union[int, Iterable[int]] = None,
3535
reply_to_message_ids: Union[int, Iterable[int]] = None,
36+
pinned: bool = False,
3637
replies: int = 1,
3738
is_scheduled: bool = False,
3839
link: str = None,
@@ -45,7 +46,7 @@ async def get_messages(
4546
4647
.. include:: /_includes/usable-by/users-bots.rst
4748
48-
You must use exactly one of ``message_ids`` OR (``chat_id``, ``message_ids``) OR (``chat_id``, ``reply_to_message_ids``) OR ``link``.
49+
You must use exactly one of ``message_ids`` OR ``reply_to_message_ids`` OR (``chat_id``, ``message_ids``) OR (``chat_id``, ``reply_to_message_ids``) OR (``chat_id``, ``pinned``) OR ``link``.
4950
5051
Parameters:
5152
chat_id (``int`` | ``str``, *optional*):
@@ -60,15 +61,20 @@ async def get_messages(
6061
reply_to_message_ids (``int`` | Iterable of ``int``, *optional*):
6162
Pass a single message identifier or an iterable of message ids (as integers) to get the content of
6263
the previous message you replied to using this message.
63-
If *message_ids* is set, this argument will be ignored.
64+
65+
pinned (``bool``, *optional*):
66+
Returns information about the newest pinned message in the specified ``chat_id``. Other parameters are ignored when this is set.
67+
Use :meth:`~pyrogram.Client.search_messages` to return all the pinned messages.
6468
6569
replies (``int``, *optional*):
6670
The number of subsequent replies to get for each message.
6771
Pass 0 for no reply at all or -1 for unlimited replies.
6872
Defaults to 1.
73+
Is ignored if ``is_scheduled`` parameter is set.
6974
7075
is_scheduled (``bool``, *optional*):
7176
Whether to get scheduled messages. Defaults to False.
77+
Only supported if both ``chat_id`` and ``message_ids`` are passed. Other parameters are ignored when this is set.
7278
7379
link (``str``):
7480
A link of the message, usually can be copied using ``Copy Link`` functionality OR obtained using :obj:`~pyrogram.raw.types.Message.link` OR :obj:`~pyrogram.raw.functions.channels.ExportMessageLink`
@@ -99,46 +105,29 @@ async def get_messages(
99105
ValueError: In case of invalid arguments.
100106
"""
101107

102-
if not chat_id and message_ids:
103-
is_iterable = not isinstance(message_ids, int)
104-
ids = list(message_ids) if is_iterable else [message_ids]
105-
ids = [raw.types.InputMessageID(id=i) for i in ids]
106-
rpc = raw.functions.messages.GetMessages(id=ids)
107-
r = await self.invoke(rpc, sleep_threshold=-1)
108-
messages = await utils.parse_messages(
109-
self,
110-
r,
111-
is_scheduled=is_scheduled,
112-
replies=replies
113-
)
114-
return messages if is_iterable else messages[0] if messages else None
115-
116-
if chat_id:
108+
if message_ids or reply_to_message_ids:
117109
ids, ids_type = (
118110
(message_ids, raw.types.InputMessageID) if message_ids
119111
else (reply_to_message_ids, raw.types.InputMessageReplyTo) if reply_to_message_ids
120112
else (None, None)
121113
)
122114

123-
if ids is None:
124-
raise ValueError("No argument supplied. Either pass message_ids or reply_to_message_ids")
125-
126-
peer = await self.resolve_peer(chat_id)
127-
128115
is_iterable = not isinstance(ids, int)
129116
ids = list(ids) if is_iterable else [ids]
130117

131118
if replies < 0:
132119
replies = (1 << 31) - 1
133120

134-
if is_scheduled:
121+
peer = await self.resolve_peer(chat_id) if chat_id else None
122+
123+
if chat_id and is_scheduled:
135124
rpc = raw.functions.messages.GetScheduledMessages(
136125
peer=peer,
137126
id=ids
138127
)
139128
else:
140129
ids = [ids_type(id=i) for i in ids]
141-
if isinstance(peer, raw.types.InputPeerChannel):
130+
if chat_id and isinstance(peer, raw.types.InputPeerChannel):
142131
rpc = raw.functions.channels.GetMessages(channel=peer, id=ids)
143132
else:
144133
rpc = raw.functions.messages.GetMessages(id=ids)
@@ -154,6 +143,18 @@ async def get_messages(
154143

155144
return messages if is_iterable else messages[0] if messages else None
156145

146+
if chat_id and pinned:
147+
peer = await self.resolve_peer(chat_id)
148+
rpc = raw.functions.channels.GetMessages(channel=peer, id=[raw.types.InputMessagePinned()])
149+
r = await self.invoke(rpc, sleep_threshold=-1)
150+
messages = await utils.parse_messages(
151+
self,
152+
r,
153+
is_scheduled=False,
154+
replies=replies
155+
)
156+
return messages[0] if messages else None
157+
157158
if link:
158159
linkps = link.split("/")
159160
raw_chat_id, message_thread_id, message_id = None, None, None

pyrogram/types/business/successful_payment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
class SuccessfulPayment(Object):
29-
"""This object contains basic information about a successful payment.
29+
"""This object contains basic information about a successful payment. Note that if the buyer initiates a chargeback with the relevant payment provider following this transaction, the funds may be debited from your balance. This is outside of Telegram's control.
3030
3131
Parameters:
3232
currency (``str``):

pyrogram/types/messages_and_media/message.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,15 +1066,12 @@ async def _parse(
10661066
)
10671067

10681068
if isinstance(action, raw.types.MessageActionPinMessage):
1069-
try:
1070-
parsed_message.pinned_message = await client.get_messages(
1071-
chat_id=parsed_message.chat.id,
1072-
reply_to_message_ids=message.id,
1073-
replies=0
1074-
)
1075-
parsed_message.service = enums.MessageServiceType.PINNED_MESSAGE
1076-
except MessageIdsEmpty:
1077-
pass
1069+
parsed_message.pinned_message = await client.get_messages(
1070+
chat_id=parsed_message.chat.id,
1071+
pinned=True,
1072+
replies=0
1073+
)
1074+
parsed_message.service = enums.MessageServiceType.PINNED_MESSAGE
10781075

10791076
if isinstance(action, raw.types.MessageActionGameScore):
10801077
parsed_message.game_high_score = types.GameHighScore._parse_action(client, message, users)

pyrogram/types/user_and_chats/chat.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -588,17 +588,10 @@ async def _parse_full(client, chat_full: Union[raw.types.messages.ChatFull, raw.
588588
)
589589

590590
if full_user.pinned_msg_id:
591-
try:
592-
parsed_chat.pinned_message = await client.get_messages(
593-
chat_id=parsed_chat.id,
594-
message_ids=full_user.pinned_msg_id
595-
)
596-
except MessageIdsEmpty:
597-
parsed_chat.pinned_message = types.Message(
598-
id=full_user.pinned_msg_id,
599-
empty=True,
600-
client=client
601-
)
591+
parsed_chat.pinned_message = await client.get_messages(
592+
chat_id=parsed_chat.id,
593+
pinned=True
594+
)
602595

603596
if getattr(full_user, "birthday", None):
604597
parsed_chat.birthdate = types.Birthdate._parse(
@@ -679,17 +672,10 @@ async def _parse_full(client, chat_full: Union[raw.types.messages.ChatFull, raw.
679672
parsed_chat.message_auto_delete_time = getattr(full_chat, "ttl_period")
680673

681674
if full_chat.pinned_msg_id:
682-
try:
683-
parsed_chat.pinned_message = await client.get_messages(
684-
chat_id=parsed_chat.id,
685-
message_ids=full_chat.pinned_msg_id
686-
)
687-
except MessageIdsEmpty:
688-
parsed_chat.pinned_message = types.Message(
689-
id=full_chat.pinned_msg_id,
690-
empty=True,
691-
client=client
692-
)
675+
parsed_chat.pinned_message = await client.get_messages(
676+
chat_id=parsed_chat.id,
677+
pinned=True
678+
)
693679

694680
if isinstance(full_chat.exported_invite, raw.types.ChatInviteExported):
695681
parsed_chat.invite_link = full_chat.exported_invite.link

0 commit comments

Comments
 (0)