-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathlisten.py
More file actions
112 lines (92 loc) · 4.29 KB
/
listen.py
File metadata and controls
112 lines (92 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Hydrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2020-present Cezar H. <https://github.com/usernein>
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
#
# This file is part of Hydrogram.
#
# Hydrogram 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.
#
# Hydrogram 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 Hydrogram. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import asyncio
import inspect
from typing import TYPE_CHECKING
from hydrogram.errors import (
ListenerTimeout,
)
from hydrogram.types import Identifier, Listener, ListenerTypes
from hydrogram.utils import PyromodConfig
if TYPE_CHECKING:
import hydrogram
from hydrogram.filters import Filter
class Listen:
async def listen(
self: hydrogram.Client,
filters: Filter | None = None,
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
timeout: int | None = None,
unallowed_click_alert: bool = True,
chat_id: int | str | list[int | str] | None = None,
user_id: int | str | list[int | str] | None = None,
message_id: int | list[int] | None = None,
inline_message_id: str | list[str] | None = None,
) -> hydrogram.types.Message | hydrogram.types.CallbackQuery:
"""
Creates a listener and waits for it to be fulfilled.
Parameters:
filters (``Optional[Filter]``):
A filter to check if the listener should be fulfilled.
listener_type (``ListenerTypes``):
The type of listener to create. Defaults to :attr:`hydrogram.types.ListenerTypes.MESSAGE`.
timeout (``Optional[int]``):
The maximum amount of time to wait for the listener to be fulfilled. Defaults to ``None``.
unallowed_click_alert (``bool``):
Whether to alert the user if they click on a button that is not intended for them. Defaults to ``True``.
chat_id (``Optional[Union[int, str], List[Union[int, str]]]``):
The chat ID(s) to listen for. Defaults to ``None``.
user_id (``Optional[Union[int, str], List[Union[int, str]]]``):
The user ID(s) to listen for. Defaults to ``None``.
message_id (``Optional[Union[int, List[int]]]``):
The message ID(s) to listen for. Defaults to ``None``.
inline_message_id (``Optional[Union[str, List[str]]]``):
The inline message ID(s) to listen for. Defaults to ``None``.
Returns:
``Union[Message, CallbackQuery]``: The Message or CallbackQuery that fulfilled the listener.
"""
pattern = Identifier(
from_user_id=user_id,
chat_id=chat_id,
message_id=message_id,
inline_message_id=inline_message_id,
)
future = self.loop.create_future()
listener = Listener(
future=future,
filters=filters,
unallowed_click_alert=unallowed_click_alert,
identifier=pattern,
listener_type=listener_type,
)
future.add_done_callback(lambda _future: self.remove_listener(listener))
self.listeners[listener_type].append(listener)
try:
return await asyncio.wait_for(future, timeout)
except asyncio.exceptions.TimeoutError:
if callable(PyromodConfig.timeout_handler):
if inspect.iscoroutinefunction(PyromodConfig.timeout_handler.__call__):
await PyromodConfig.timeout_handler(pattern, listener, timeout)
else:
await self.loop.run_in_executor(
None, PyromodConfig.timeout_handler, pattern, listener, timeout
)
elif PyromodConfig.throw_exceptions:
raise ListenerTimeout(timeout)