From 91beb214e9cd3ff4cf71425f245b3fd327536bbb Mon Sep 17 00:00:00 2001 From: Furoin Date: Tue, 6 Nov 2018 17:11:35 +0300 Subject: [PATCH 01/10] added message.mentioned --- pyrogram/client/ext/utils.py | 3 +++ pyrogram/client/types/messages_and_media/message.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index bc908165..0d0c5abf 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -591,6 +591,8 @@ def parse_messages( else: reply_markup = None + mentioned = message.mentioned + m = pyrogram_types.Message( message_id=message.id, date=message.date, @@ -606,6 +608,7 @@ def parse_messages( forward_from_message_id=forward_from_message_id, forward_signature=forward_signature, forward_date=forward_date, + mentioned=mentioned, edit_date=message.edit_date, media_group_id=message.grouped_id, photo=photo, diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 86bb57bc..59776859 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -206,6 +206,7 @@ class Message(Object): forward_signature: str = None, forward_date: int = None, reply_to_message=None, + mentioned=None, edit_date: int = None, media_group_id: str = None, author_signature: str = None, @@ -253,6 +254,7 @@ class Message(Object): self.forward_signature = forward_signature # flags.4?string self.forward_date = forward_date # flags.5?int self.reply_to_message = reply_to_message # flags.6?Message + self.mentioned = mentioned self.edit_date = edit_date # flags.7?int self.media_group_id = media_group_id # flags.8?string self.author_signature = author_signature # flags.9?string From 2d0ffcb0f48117af882213da8f056056f2cfa59b Mon Sep 17 00:00:00 2001 From: Furoin Date: Tue, 6 Nov 2018 17:13:37 +0300 Subject: [PATCH 02/10] added Filters.mentioned --- pyrogram/client/filters/filters.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index aeb730a7..6b9e9fd2 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -168,6 +168,8 @@ class Filters: dan = create("Dan", lambda _, m: bool(m.from_user and m.from_user.id == 23122162)) + mentioned = create("Mentioned", lambda _, m: bool(m.mentioned)) + @staticmethod def command(command: str or list, prefix: str or list = "/", From 1adc8121080ff71f247cee6afac52647cf9c42d9 Mon Sep 17 00:00:00 2001 From: Furoin Date: Tue, 6 Nov 2018 18:36:40 +0300 Subject: [PATCH 03/10] added Filters.chat("me") --- pyrogram/client/filters/filters.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index aeb730a7..037e6bb9 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -287,9 +287,9 @@ class Filters: def __init__(self, chats: int or str or list = None): chats = [] if chats is None else chats if type(chats) is list else [chats] super().__init__( - {i.lower().strip("@") if type(i) is str else i for i in chats} + {"me" if i in ["me", "self"] else i.lower().strip("@") if type(i) is str else i for i in chats} if type(chats) is list else - {chats.lower().strip("@") if type(chats) is str else chats} + {"me" if chats in ["me", "self"] else chats.lower().strip("@") if type(chats) is str else chats} ) def __call__(self, message): @@ -297,7 +297,10 @@ class Filters: message.chat and (message.chat.id in self or (message.chat.username - and message.chat.username.lower() in self)) + and message.chat.username.lower() in self) + or ("me" in self and message.from_user + and message.from_user.is_self + and not message.outgoing)) ) service = create( From 5da5cabf4ca708f5aeb067f76f9528ff6acc3131 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 6 Nov 2018 17:31:04 +0100 Subject: [PATCH 04/10] Remove useless variable --- pyrogram/client/ext/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index 0d0c5abf..6fc2552a 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -591,8 +591,6 @@ def parse_messages( else: reply_markup = None - mentioned = message.mentioned - m = pyrogram_types.Message( message_id=message.id, date=message.date, @@ -608,7 +606,7 @@ def parse_messages( forward_from_message_id=forward_from_message_id, forward_signature=forward_signature, forward_date=forward_date, - mentioned=mentioned, + mentioned=message.mentioned, edit_date=message.edit_date, media_group_id=message.grouped_id, photo=photo, From 0943761a911ae2a0f29127f1b29860ff8b308ddd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 6 Nov 2018 17:32:46 +0100 Subject: [PATCH 05/10] Update filters.py --- pyrogram/client/filters/filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 6b9e9fd2..48f0965d 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -169,6 +169,7 @@ class Filters: dan = create("Dan", lambda _, m: bool(m.from_user and m.from_user.id == 23122162)) mentioned = create("Mentioned", lambda _, m: bool(m.mentioned)) + """Filter messages containing mentions""" @staticmethod def command(command: str or list, From 5571888143dc310168b3383a8718209510f103e0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 6 Nov 2018 17:37:32 +0100 Subject: [PATCH 06/10] Add mentioned docstrings --- pyrogram/client/types/messages_and_media/message.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 59776859..d224a9c6 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -55,6 +55,9 @@ class Message(Object): For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply. + mentioned (``bool``, *optional*): + The message contains a mention. + edit_date (``int``, *optional*): Date the message was last edited in Unix time. From 5efd608487f454efaf7a6939355eefd3f49acc10 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 6 Nov 2018 17:40:37 +0100 Subject: [PATCH 07/10] Update Filters.chat docstrings --- pyrogram/client/filters/filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 037e6bb9..77ebb771 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -281,6 +281,7 @@ class Filters: Args: chats (``int`` | ``str`` | ``list``): Pass one or more chat ids/usernames to filter chats. + For your personal cloud (Saved Messages) you can simply use “me” or “self”. Defaults to None (no chats). """ From b3737fc6ef2d9047a7054c87fb7dec679de883ad Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 6 Nov 2018 17:42:01 +0100 Subject: [PATCH 08/10] Add MESSAGE_DELETE_FORBIDDEN error --- compiler/error/source/403_FORBIDDEN.tsv | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/error/source/403_FORBIDDEN.tsv b/compiler/error/source/403_FORBIDDEN.tsv index a2d06832..34433da7 100644 --- a/compiler/error/source/403_FORBIDDEN.tsv +++ b/compiler/error/source/403_FORBIDDEN.tsv @@ -1,4 +1,5 @@ id message CHAT_WRITE_FORBIDDEN You don't have rights to send messages in this chat RIGHT_FORBIDDEN One or more admin rights can't be applied to this kind of chat (channel/supergroup) -CHAT_ADMIN_INVITE_REQUIRED You don't have rights to invite other users \ No newline at end of file +CHAT_ADMIN_INVITE_REQUIRED You don't have rights to invite other users +MESSAGE_DELETE_FORBIDDEN You don't have rights to delete messages in this chat \ No newline at end of file From 648eb809287e526ae3b17e936bbacac06e12e7e3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 7 Nov 2018 12:03:57 +0100 Subject: [PATCH 09/10] Add Message.edit() bound method --- .../types/messages_and_media/message.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index d224a9c6..2dce5dfb 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -369,6 +369,54 @@ class Message(Object): reply_markup=reply_markup ) + def edit(self, text: str, parse_mode: str = "", disable_web_page_preview: bool = None, reply_markup=None): + """Bound method *edit* of :obj:`Message + + Use as a shortcut for: + + .. code-block:: python + + client.edit_message_text( + chat_id=message.chat.id, + message_id=message.message_id, + text="hello", + ) + + Example: + .. code-block:: python + + message.edit("hello") + + Args: + text (``str``): + New text of the message. + + parse_mode (``str``, *optional*): + Use :obj:`MARKDOWN ` or :obj:`HTML ` + if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your message. + Defaults to Markdown. + + disable_web_page_preview (``bool``, *optional*): + Disables link previews for links in this message. + + reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): + An InlineKeyboardMarkup object. + + Returns: + On success, the edited :obj:`Message ` is returned. + + Raises: + :class:`Error ` in case of a Telegram RPC error. + """ + return self._client.edit_message_text( + chat_id=self.chat.id, + message_id=self.message_id, + text=text, + parse_mode=parse_mode, + disable_web_page_preview=disable_web_page_preview, + reply_markup=reply_markup + ) + def forward(self, chat_id: int or str, disable_notification: bool = None): From f8844d60ab43707d0d924207cadd39bc83643ee4 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 7 Nov 2018 13:11:33 +0100 Subject: [PATCH 10/10] Handle possible MESSAGE_IDS_EMPTY errors in case of pinned messages --- pyrogram/client/ext/utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index 6fc2552a..123dfabf 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -641,7 +641,7 @@ def parse_messages( replies=replies - 1 ) except MessageIdsEmpty: - m.reply_to_message = None + pass elif isinstance(message, types.MessageService): action = message.action @@ -742,11 +742,14 @@ def parse_messages( ) if isinstance(action, types.MessageActionPinMessage): - m.pinned_message = client.get_messages( - m.chat.id, - reply_to_message_ids=message.id, - replies=0 - ) + try: + m.pinned_message = client.get_messages( + m.chat.id, + reply_to_message_ids=message.id, + replies=0 + ) + except MessageIdsEmpty: + pass else: m = pyrogram_types.Message(message_id=message.id, client=proxy(client))