# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from __future__ import annotations
import pyrogram
from pyrogram import enums, types
from pyrogram.errors import ListenerStopped
from .listen import UNSET, resolve_listener_ids
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pyrogram.filters import Filter
class Ask:
async def ask(
self: pyrogram.Client,
chat_id: int | str | list[int | str],
text: str,
filters: Filter | None = None,
listener_type: enums.ListenerTypes = enums.ListenerTypes.MESSAGE,
timeout: float | None = UNSET,
unallowed_click_alert: bool | str = True,
user_id: int | str | list[int | str] | None = None,
message_id: int | list[int] | None = None,
inline_message_id: str | list[str] | None = None,
**kwargs,
) -> types.Message | types.CallbackQuery:
"""Send a message and wait for the answer to it.
Shortcut for :meth:`~pyrogram.Client.send_message` followed by
:meth:`~pyrogram.Client.listen`. The returned update carries the prompt
as ``sent_message``.
.. include:: /_includes/usable-by/users-bots.rst
Parameters:
chat_id (``int`` | ``str`` | List of ``int`` | ``str``):
Chat to send the question to. When a list is given, the question
goes to the first one and any of them may answer.
text (``str``):
Text of the question.
filters (:obj:`~pyrogram.filters.Filter`, *optional*):
Extra filter the answer has to pass.
listener_type (:obj:`~pyrogram.enums.ListenerTypes`, *optional*):
Kind of update to wait for. Defaults to a new message.
timeout (``float``, *optional*):
Seconds to wait before raising ``ListenerTimeout``. Defaults to
the client's ``listener_timeout`` (300s).
unallowed_click_alert (``bool`` | ``str``, *optional*):
For callback query listeners, answer clicks coming from a user
this listener does not expect. Pass a string to set the text.
user_id (``int`` | ``str`` | List of ``int`` | ``str``, *optional*):
User the answer has to come from.
message_id (``int`` | List of ``int``, *optional*):
Message the answer has to belong to.
inline_message_id (``str`` | List of ``str``, *optional*):
Inline message the answer has to belong to.
kwargs:
Any other argument accepted by
:meth:`~pyrogram.Client.send_message`.
Returns:
:obj:`~pyrogram.types.Message` | :obj:`~pyrogram.types.CallbackQuery`:
The answer, with the prompt attached as ``sent_message``.
Raises:
ListenerTimeout: In case nobody answered in time.
ListenerStopped: In case the listener was stopped, or the client is.
ListenerLimitReached: In case no listener slot was available.
Example:
.. code-block:: python
answer = await app.ask(chat_id, "What is your name?", timeout=60)
await answer.reply(f"Hello {answer.text}")
"""
if self.no_updates:
raise ListenerStopped("Cannot listen for updates on a client started with no_updates")
if timeout is UNSET:
timeout = self.listener_timeout
target_chat_id = chat_id[0] if isinstance(chat_id, list) else chat_id
resolved_chat_id = await resolve_listener_ids(self, chat_id)
resolved_user_id = await resolve_listener_ids(self, user_id)
identifier = types.Identifier(
chat_id=resolved_chat_id,
user_id=resolved_user_id,
message_id=message_id,
inline_message_id=inline_message_id,
)
future = self.loop.create_future()
listener = types.Listener(
listener_type=listener_type,
identifier=identifier,
filters=filters,
future=future,
unallowed_click_alert=unallowed_click_alert,
)
self.listeners.add(listener, timeout)
parked = self.dispatcher.park()
try:
sent_message = await self.send_message(target_chat_id, text, **kwargs)
response = await future
if response is not None:
response.sent_message = sent_message
return response
finally:
if parked:
self.dispatcher.unpark()
self.listeners.remove(listener)