Skip to content

Commit 0671877

Browse files
feat(pyromod): add Message.{listen,ask,stop_listening}
1 parent 7db2eed commit 0671877

File tree

1 file changed

+181
-1
lines changed

1 file changed

+181
-1
lines changed

hydrogram/types/messages_and_media/message.py

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
from typing import BinaryIO, Callable, Optional, Union
2525

2626
import hydrogram
27-
from hydrogram import enums, raw, types, utils
27+
from hydrogram import enums, filters, raw, types, utils
2828
from hydrogram.errors import MessageIdsEmpty, PeerIdInvalid
2929
from hydrogram.parser import Parser
3030
from hydrogram.parser import utils as parser_utils
3131
from hydrogram.types.object import Object
32+
from hydrogram.types.pyromod import ListenerTypes
3233
from hydrogram.types.update import Update
3334

3435
log = logging.getLogger(__name__)
@@ -1030,6 +1031,185 @@ async def _parse(
10301031
return parsed_message
10311032
return None
10321033

1034+
def listen(
1035+
self,
1036+
filters: Optional["filters.Filter"] = None,
1037+
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
1038+
timeout: Optional[int] = None,
1039+
unallowed_click_alert: bool = True,
1040+
message_id: Optional[Union[int, list[int]]] = None,
1041+
inline_message_id: Optional[Union[str, list[str]]] = None,
1042+
):
1043+
"""
1044+
Bound method *listen* of :obj:`~hydrogram.types.Chat`.
1045+
1046+
Use as a shortcut for:
1047+
1048+
.. code-block:: python
1049+
1050+
await client.listen(
1051+
chat_id=chat_id,
1052+
user_id=user_id
1053+
)
1054+
1055+
Example:
1056+
.. code-block:: python
1057+
1058+
await chat.listen()
1059+
1060+
Parameters:
1061+
filters (``Optional[filters.Filter]``):
1062+
A filter to check if the listener should be fulfilled.
1063+
1064+
listener_type (``ListenerTypes``):
1065+
The type of listener to create. Defaults to :attr:`hydrogram.types.ListenerTypes.MESSAGE`.
1066+
1067+
timeout (``Optional[int]``):
1068+
The maximum amount of time to wait for the listener to be fulfilled. Defaults to ``None``.
1069+
1070+
unallowed_click_alert (``bool``):
1071+
Whether to alert the user if they click on a button that is not intended for them. Defaults to ``True``.
1072+
1073+
message_id (``Optional[Union[int, List[int]]]``):
1074+
The message ID(s) to listen for. Defaults to ``None``.
1075+
1076+
inline_message_id (``Optional[Union[str, List[str]]]``):
1077+
The inline message ID(s) to listen for. Defaults to ``None``.
1078+
1079+
Returns:
1080+
Union[:obj:`~hydrogram.types.Message`, :obj:`~hydrogram.types.CallbackQuery`]: The Message or CallbackQuery
1081+
"""
1082+
return self._client.listen(
1083+
chat_id=self.chat.id if self.chat else None,
1084+
filters=filters,
1085+
listener_type=listener_type,
1086+
timeout=timeout,
1087+
unallowed_click_alert=unallowed_click_alert,
1088+
user_id=self.from_user.id if self.from_user else None,
1089+
message_id=message_id,
1090+
inline_message_id=inline_message_id,
1091+
)
1092+
1093+
def ask(
1094+
self,
1095+
text: str,
1096+
filters: Optional["filters.Filter"] = None,
1097+
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
1098+
timeout: Optional[int] = None,
1099+
unallowed_click_alert: bool = True,
1100+
message_id: Optional[Union[int, list[int]]] = None,
1101+
inline_message_id: Optional[Union[str, list[str]]] = None,
1102+
*args,
1103+
**kwargs,
1104+
):
1105+
"""
1106+
Bound method *ask* of :obj:`~hydrogram.types.Chat`.
1107+
1108+
Use as a shortcut for:
1109+
1110+
.. code-block:: python
1111+
1112+
await client.ask(
1113+
chat_id=chat_id,
1114+
user_id=user_id,
1115+
text=text
1116+
)
1117+
1118+
Example:
1119+
1120+
.. code-block:: python
1121+
1122+
await chat.ask("What's your name?")
1123+
1124+
Parameters:
1125+
text (``str``):
1126+
The text to send.
1127+
1128+
filters (``Optional[filters.Filter]``):
1129+
Same as :meth:`hydrogram.Client.listen`.
1130+
1131+
listener_type (``ListenerTypes``):
1132+
Same as :meth:`hydrogram.Client.listen`.
1133+
1134+
timeout (``Optional[int]``):
1135+
Same as :meth:`hydrogram.Client.listen`.
1136+
1137+
unallowed_click_alert (``bool``):
1138+
Same as :meth:`hydrogram.Client.listen`.
1139+
1140+
message_id (``Optional[Union[int, List[int]]]``):
1141+
The message ID(s) to listen for. Defaults to ``None``.
1142+
1143+
inline_message_id (``Optional[Union[str, List[str]]]``):
1144+
The inline message ID(s) to listen for. Defaults to ``None``.
1145+
1146+
args (``Any``):
1147+
Additional arguments to pass to :meth:`hydrogram.Client.send_message`.
1148+
1149+
kwargs (``Any``):
1150+
Additional keyword arguments to pass to :meth:`hydrogram.Client.send_message`.
1151+
1152+
Returns:
1153+
Union[:obj:`~hydrogram.types.Message`, :obj:`~hydrogram.types.CallbackQuery`]: The Message or CallbackQuery
1154+
"""
1155+
return self._client.ask(
1156+
chat_id=self.chat.id if self.chat else None,
1157+
text=text,
1158+
filters=filters,
1159+
listener_type=listener_type,
1160+
timeout=timeout,
1161+
unallowed_click_alert=unallowed_click_alert,
1162+
user_id=self.from_user.id if self.from_user else None,
1163+
message_id=message_id,
1164+
inline_message_id=inline_message_id,
1165+
*args,
1166+
**kwargs,
1167+
)
1168+
1169+
def stop_listening(
1170+
self,
1171+
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
1172+
message_id: Optional[Union[int, list[int]]] = None,
1173+
inline_message_id: Optional[Union[str, list[str]]] = None,
1174+
):
1175+
"""
1176+
Bound method *stop_listening* of :obj:`~hydrogram.types.Chat`.
1177+
1178+
Use as a shortcut for:
1179+
1180+
.. code-block:: python
1181+
1182+
await client.stop_listening(
1183+
chat_id=chat_id,
1184+
user_id=user_id
1185+
)
1186+
1187+
Example:
1188+
.. code-block:: python
1189+
1190+
await chat.stop_listening()
1191+
1192+
Parameters:
1193+
listener_type (``ListenerTypes``):
1194+
The type of listener to stop listening for. Defaults to :attr:`hydrogram.types.ListenerTypes.MESSAGE`.
1195+
1196+
message_id (``Optional[Union[int, List[int]]]``):
1197+
The message ID(s) to stop listening for. Defaults to ``None``.
1198+
1199+
inline_message_id (``Optional[Union[str, List[str]]]``):
1200+
The inline message ID(s) to stop listening for. Defaults to ``None``.
1201+
1202+
Returns:
1203+
``bool``: The return value of :meth:`hydrogram.Client.stop_listening`.
1204+
"""
1205+
return self._client.stop_listening(
1206+
chat_id=self.chat.id if self.chat else None,
1207+
listener_type=listener_type,
1208+
user_id=self.from_user.id if self.from_user else None,
1209+
message_id=message_id,
1210+
inline_message_id=inline_message_id,
1211+
)
1212+
10331213
@property
10341214
def link(self) -> str:
10351215
if (

0 commit comments

Comments
 (0)