diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 6a7811c4..7c377681 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -144,8 +144,6 @@ def pyrogram_api(): stop_transmission export_session_string set_parse_mode - ask - listen """, messages=""" Messages diff --git a/pyrogram/client.py b/pyrogram/client.py index e9ed4438..9c98bedd 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -275,8 +275,6 @@ class Client(Methods): self.loop = asyncio.get_event_loop() - self.listening = {} - def __enter__(self): return self.start() diff --git a/pyrogram/handlers/message_handler.py b/pyrogram/handlers/message_handler.py index 49c45e8a..f5a35b55 100644 --- a/pyrogram/handlers/message_handler.py +++ b/pyrogram/handlers/message_handler.py @@ -46,27 +46,4 @@ class MessageHandler(Handler): """ def __init__(self, callback: Callable, filters=None): - #super().__init__(callback, filters) - self.user_callback = callback - super().__init__(self.resolve_listener, filters) - - async def resolve_listener(self, client, message, *args): - listener = client.listening.get(message.chat.id) - if listener and not listener['future'].done(): - listener['future'].set_result(message) - else: - if listener and listener['future'].done(): - client.clear_listener(message.chat.id, listener['future']) - await self.user_callback(client, message, *args) - - async def check(self, client, update): - listener = client.listening.get(update.chat.id) - - if listener and not listener['future'].done(): - return await listener['filters'](client, update) if callable(listener['filters']) else True - - return ( - await self.filters(client, update) - if callable(self.filters) - else True - ) + super().__init__(callback, filters) diff --git a/pyrogram/methods/utilities/__init__.py b/pyrogram/methods/utilities/__init__.py index 84802954..80a5f741 100644 --- a/pyrogram/methods/utilities/__init__.py +++ b/pyrogram/methods/utilities/__init__.py @@ -17,9 +17,7 @@ # along with Pyrogram. If not, see . from .add_handler import AddHandler -from .ask import Ask from .export_session_string import ExportSessionString -from .listen import Listen from .remove_handler import RemoveHandler from .restart import Restart from .run import Run @@ -30,9 +28,7 @@ from .stop_transmission import StopTransmission class Utilities( AddHandler, - Ask, ExportSessionString, - Listen, RemoveHandler, Restart, Run, diff --git a/pyrogram/methods/utilities/ask.py b/pyrogram/methods/utilities/ask.py deleted file mode 100644 index 942c545b..00000000 --- a/pyrogram/methods/utilities/ask.py +++ /dev/null @@ -1,68 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-present Dan -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -import logging - -import pyrogram - -from typing import Union - -log = logging.getLogger(__name__) - - -class Ask: - async def ask( - self: "pyrogram.Client", - chat_id: Union[str, int], - text: str, - filters=None, - timeout: int = None, - *args, - **kwargs - ): - """Send message then awaits for a new message in the specified chat. - - Parameters: - chat_id (``int`` | ``str``): - Unique identifier (int) or username (str) of the target chat. - 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). - - text (``str``): - Text of the message to be sent. - - filters (:obj:`~pyrogram.filters`, *optional*): - Pass one or more filters to allow only a subset of messages to be passed - in your function. - - timeout (``int``, *optional*): - The waiting timeout. - - Returns: - :obj:`~pyrogram.types.Message`: On success, text message is returned. - - Example: - .. code-block:: python - - answer = await Client.listen(chat_id, "Your name:") - name = answer.text - """ - request = await self.send_message(chat_id, text, *args, **kwargs) - response = await self.listen(chat_id, filters, timeout) - response.request = request - return response diff --git a/pyrogram/methods/utilities/listen.py b/pyrogram/methods/utilities/listen.py deleted file mode 100644 index b4ad75c2..00000000 --- a/pyrogram/methods/utilities/listen.py +++ /dev/null @@ -1,92 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-present Dan -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -import asyncio -import functools -import logging - -import pyrogram - -from typing import Union - -log = logging.getLogger(__name__) - - -class Listen: - async def listen( - self: "pyrogram.Client", - chat_id: Union[str, int], - filters=None, - timeout: int = None - ): - """Awaits for a new message in the specified chat. - - Parameters: - chat_id (``int`` | ``str``): - Unique identifier (int) or username (str) of the target chat. - 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). - - filters (:obj:`~pyrogram.filters`, *optional*): - Pass one or more filters to allow only a subset of messages to be passed - in your function. - - timeout (``int``, *optional*): - The waiting timeout. - - Returns: - :obj:`~pyrogram.types.Message`: On success, text message is returned. - - Example: - .. code-block:: python - - await Client.send_message(chat_id, "Your name:") - answer = await Client.listen(chat_id) - name = answer.text - """ - if type(chat_id) != int: - chat = await self.get_chat(chat_id) - chat_id = chat.id - - future = self.loop.create_future() - future.add_done_callback( - functools.partial(self.clear_listener, chat_id) - ) - self.listening.update({ - chat_id: {"future": future, "filters": filters} - }) - return await asyncio.wait_for(future, timeout) - - def clear_listener( - self: "pyrogram.Client", - chat_id: Union[str, int], - future - ): - if chat_id in self.listening and future == self.listening[chat_id]["future"]: - self.listening.pop(chat_id, None) - - def cancel_listener( - self: "pyrogram.Client", - chat_id: Union[str, int] - ): - listener = self.listening.get(chat_id) - if not listener or listener['future'].done(): - return - - listener['future'].set_exception(ListenerCanceled()) - self.clear_listener(chat_id, listener['future']) diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index ac4c65c2..5b4114f6 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -967,12 +967,3 @@ class Chat(Object): """ return await self._client.unpin_all_chat_messages(self.id) - - def listen(self, *args, **kwargs): - return self._client.listen(self.id, *args, **kwargs) - - def ask(self, *args, **kwargs): - return self._client.ask(self.id, *args, **kwargs) - - def cancel_listener(self): - return self._client.cancel_listener(self.id) diff --git a/pyrogram/types/user_and_chats/user.py b/pyrogram/types/user_and_chats/user.py index cb16b067..e9813578 100644 --- a/pyrogram/types/user_and_chats/user.py +++ b/pyrogram/types/user_and_chats/user.py @@ -397,12 +397,3 @@ class User(Object, Update): """ return self._client.get_common_chats(self.id) - - def listen(self, *args, **kwargs): - return self._client.listen(self.id, *args, **kwargs) - - def ask(self, *args, **kwargs): - return self._client.ask(self.id, *args, **kwargs) - - def cancel_listener(self): - return self._client.cancel_listener(self.id)