File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed
Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change 1717
1818if TYPE_CHECKING :
1919 from .client import WhatsApp
20- from .types .base_update import BaseUpdate
20+ from .types .base_update import BaseUpdate , BaseUserUpdate
2121 from .filters import Filter
2222
2323
@@ -85,7 +85,9 @@ class ListenerCanceled(Exception):
8585 update: The update that caused the listener to be canceled
8686 """
8787
88- def __init__ (self , update : BaseUpdate | None = None ):
88+ update : BaseUpdate | BaseUserUpdate | None
89+
90+ def __init__ (self , update : BaseUpdate | BaseUserUpdate | None = None ):
8991 self .update = update
9092
9193 def __str__ (self ):
@@ -126,6 +128,8 @@ def __str__(self):
126128
127129
128130class Listener :
131+ _listener_canceled = ListenerCanceled
132+
129133 def __init__ (
130134 self ,
131135 filters : Filter ,
@@ -146,7 +150,7 @@ def set_exception(self, exception: Exception) -> None:
146150 self .event .set ()
147151
148152 def cancel (self , update : BaseUpdate | None = None ) -> None :
149- self .set_exception (ListenerCanceled (update ))
153+ self .set_exception (self . _listener_canceled (update ))
150154
151155 def stop (self , reason : str | None = None ) -> None :
152156 self .set_exception (ListenerStopped (reason ))
Original file line number Diff line number Diff line change 55from pywa .listeners import * # noqa MUST BE IMPORTED FIRST
66from pywa .listeners import (
77 Listener as _Listener ,
8+ ListenerCanceled as _ListenerCanceled ,
89 BaseListenerIdentifier ,
910) # noqa MUST BE IMPORTED FIRST
1011
2223 ChatOpened ,
2324 FlowCompletion ,
2425)
26+ from .types .base_update import BaseUserUpdateAsync
2527
2628if TYPE_CHECKING :
2729 from .client import WhatsApp
2830
2931
32+ class ListenerCanceled (_ListenerCanceled ):
33+ """
34+ The listener was canceled by a filter
35+
36+ Example:
37+
38+ .. code-block:: python
39+
40+ try:
41+ wa.listen(
42+ to=UserUpdateListenerIdentifier(
43+ sender="123456",
44+ recipient="654321"
45+ ),
46+ filters=filters.message & filters.text,
47+ cancelers=filters.callback_button & filters.matches("cancel")
48+ )
49+ except ListenerCanceled as e:
50+ assert e.update.data == "cancel" # the update that caused the listener to be canceled
51+ wa.send_message("123456", "You cancelled the listener by clicking the `cancel` button")
52+
53+ Attributes:
54+ update: The update that caused the listener to be canceled
55+ """
56+
57+ update : BaseUpdate | BaseUserUpdateAsync | None
58+
59+ def __init__ (self , update : BaseUpdate | BaseUserUpdateAsync | None = None ):
60+ super ().__init__ (update )
61+
62+
3063class Listener (_Listener ):
3164 _listener_canceled = ListenerCanceled
3265
You can’t perform that action at this time.
0 commit comments