From 6e686fdd894af026a6de8a92e1ac13ff5f73d460 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 22 Dec 2018 14:18:23 +0100 Subject: [PATCH 1/5] Fix docs not using bytes when showing examples dealing with callback queries --- docs/source/resources/UsingFilters.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/source/resources/UsingFilters.rst b/docs/source/resources/UsingFilters.rst index 79ecd24f..d70005a5 100644 --- a/docs/source/resources/UsingFilters.rst +++ b/docs/source/resources/UsingFilters.rst @@ -162,7 +162,7 @@ yourself. This allows you to test your filter by pressing the inline button: "username", # Change this to your username or id "Pyrogram's custom filter test", reply_markup=InlineKeyboardMarkup( - [[InlineKeyboardButton("Press me", "pyrogram")]] + [[InlineKeyboardButton("Press me", b"pyrogram")]] ) ) @@ -178,7 +178,7 @@ containing "pyrogram" as data: hardcoded_data = Filters.create( name="HardcodedData", - func=lambda filter, callback_query: callback_query.data == "pyrogram" + func=lambda filter, callback_query: callback_query.data == b"pyrogram" ) The ``lambda`` operator in python is used to create small anonymous functions and is perfect for this example, the same @@ -187,7 +187,7 @@ could be achieved with a normal function, but we don't really need it as it make .. code-block:: python def func(filter, callback_query): - return callback_query.data == "pyrogram" + return callback_query.data == b"pyrogram" hardcoded_data = Filters.create( name="HardcodedData", @@ -223,6 +223,6 @@ And its usage: .. code-block:: python - @app.on_callback_query(dynamic_data("pyrogram")) + @app.on_callback_query(dynamic_data(b"pyrogram")) def pyrogram_data(client, callback_query): client.answer_callback_query(callback_query.id, "it works!") \ No newline at end of file From 9fe04ff7cdb0fe9362e66a066f5310a3b080740c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 23 Dec 2018 19:51:45 +0100 Subject: [PATCH 2/5] Fix filter links to Objects --- pyrogram/client/filters/filters.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index fa9eace3..69d3fad3 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -86,37 +86,37 @@ class Filters: """Filter edited messages.""" audio = create("Audio", lambda _, m: bool(m.audio)) - """Filter messages that contain :obj:`Audio ` objects.""" + """Filter messages that contain :obj:`Audio ` objects.""" document = create("Document", lambda _, m: bool(m.document)) - """Filter messages that contain :obj:`Document ` objects.""" + """Filter messages that contain :obj:`Document ` objects.""" photo = create("Photo", lambda _, m: bool(m.photo)) - """Filter messages that contain :obj:`Photo ` objects.""" + """Filter messages that contain :obj:`Photo ` objects.""" sticker = create("Sticker", lambda _, m: bool(m.sticker)) - """Filter messages that contain :obj:`Sticker ` objects.""" + """Filter messages that contain :obj:`Sticker ` objects.""" animation = create("GIF", lambda _, m: bool(m.animation)) - """Filter messages that contain :obj:`Animation ` objects.""" + """Filter messages that contain :obj:`Animation ` objects.""" video = create("Video", lambda _, m: bool(m.video)) - """Filter messages that contain :obj:`Video ` objects.""" + """Filter messages that contain :obj:`Video ` objects.""" voice = create("Voice", lambda _, m: bool(m.voice)) - """Filter messages that contain :obj:`Voice ` note objects.""" + """Filter messages that contain :obj:`Voice ` note objects.""" video_note = create("Voice", lambda _, m: bool(m.video_note)) - """Filter messages that contain :obj:`VideoNote ` objects.""" + """Filter messages that contain :obj:`VideoNote ` objects.""" contact = create("Contact", lambda _, m: bool(m.contact)) - """Filter messages that contain :obj:`Contact ` objects.""" + """Filter messages that contain :obj:`Contact ` objects.""" location = create("Location", lambda _, m: bool(m.location)) - """Filter messages that contain :obj:`Location ` objects.""" + """Filter messages that contain :obj:`Location ` objects.""" venue = create("Venue", lambda _, m: bool(m.venue)) - """Filter messages that contain :obj:`Venue ` objects.""" + """Filter messages that contain :obj:`Venue ` objects.""" web_page = create("WebPage", lambda _, m: m.web_page) """Filter messages sent with a webpage preview.""" From 7881320a88c899b2534481732220c78016a95abc Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 23 Dec 2018 19:53:22 +0100 Subject: [PATCH 3/5] Update develop branch version --- pyrogram/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 6ea6aa5c..d5a74409 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -23,7 +23,7 @@ __copyright__ = "Copyright (C) 2017-2018 Dan Tès Date: Mon, 24 Dec 2018 14:50:04 +0100 Subject: [PATCH 4/5] Type hint on_message decorator --- pyrogram/client/methods/decorators/on_message.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/methods/decorators/on_message.py b/pyrogram/client/methods/decorators/on_message.py index 68ed1fab..a252c6ba 100644 --- a/pyrogram/client/methods/decorators/on_message.py +++ b/pyrogram/client/methods/decorators/on_message.py @@ -16,8 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from typing import Tuple + import pyrogram from pyrogram.client.filters.filter import Filter +from pyrogram.client.handlers.handler import Handler from ...ext import BaseClient @@ -45,7 +48,7 @@ class OnMessage(BaseClient): The group identifier, defaults to 0. """ - def decorator(func): + def decorator(func: callable) -> Tuple[Handler, int]: if isinstance(func, tuple): func = func[0].callback From 00e4e385aa676afe2b67f84ff97112f3a67d9c81 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 24 Dec 2018 22:31:45 +0100 Subject: [PATCH 5/5] Allow bots to message old chats even if they don't exist in the session --- pyrogram/client/client.py | 75 +++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index f5aa33ea..3dfd733c 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1104,35 +1104,58 @@ class Client(Methods, BaseClient): :class:`Error ` in case of a Telegram RPC error. ``KeyError`` in case the peer doesn't exist in the internal database. """ - if type(peer_id) is str: - if peer_id in ("self", "me"): - return types.InputPeerSelf() - - peer_id = re.sub(r"[@+\s]", "", peer_id.lower()) - - try: - int(peer_id) - except ValueError: - if peer_id not in self.peers_by_username: - self.send(functions.contacts.ResolveUsername(peer_id)) - - return self.peers_by_username[peer_id] - else: - try: - return self.peers_by_phone[peer_id] - except KeyError: - raise PeerIdInvalid - - try: # User + try: return self.peers_by_id[peer_id] except KeyError: - try: # Chat - return self.peers_by_id[-peer_id] + if type(peer_id) is str: + if peer_id in ("self", "me"): + return types.InputPeerSelf() + + peer_id = re.sub(r"[@+\s]", "", peer_id.lower()) + + try: + int(peer_id) + except ValueError: + if peer_id not in self.peers_by_username: + self.send( + functions.contacts.ResolveUsername( + username=peer_id + ) + ) + + return self.peers_by_username[peer_id] + else: + try: + return self.peers_by_phone[peer_id] + except KeyError: + raise PeerIdInvalid + + if peer_id > 0: + self.fetch_peers( + self.send( + functions.users.GetUsers( + id=[types.InputUser(peer_id, 0)] + ) + ) + ) + else: + if str(peer_id).startswith("-100"): + self.send( + functions.channels.GetChannels( + id=[types.InputChannel(int(str(peer_id)[4:]), 0)] + ) + ) + else: + self.send( + functions.messages.GetChats( + id=[-peer_id] + ) + ) + + try: + return self.peers_by_id[peer_id] except KeyError: - try: # Channel - return self.peers_by_id[int("-100" + str(peer_id))] - except (KeyError, ValueError): - raise PeerIdInvalid + raise PeerIdInvalid def save_file(self, path: str,