@@ -63,7 +63,9 @@ if __name__ == "__main__":
63
63
asyncio.run(main())
64
64
```
65
65
66
- Process updates in parallel:
66
+ ### Process updates in parallel
67
+
68
+ Dispatcher processes updates sequentially by default. but you can change the behavior.
67
69
68
70
``` py
69
71
# ---- sniff ----
@@ -81,7 +83,9 @@ dp = Dispatcher(
81
83
)
82
84
```
83
85
84
- Manage propagation of handlers:
86
+ ### Manage propagation of handlers
87
+
88
+ Stop processing this handler or all of pending handlers.
85
89
86
90
``` py
87
91
@dp.register_message_handler (mf.regex(" ^/start" ) & mf.private)
@@ -97,3 +101,59 @@ async def handle_message(context: MessageContext):
97
101
context.continue_propagation() # -> continue propagating this update to other handlers.
98
102
99
103
```
104
+
105
+ ### Custom filters
106
+
107
+ You can create custom filters for any type of update.
108
+
109
+ #### Abstractly ( Slow, Featured )
110
+
111
+ Create a class that inherit from ` Filter ` , then setup your filter.
112
+
113
+ ``` py
114
+ from typing import Optional
115
+
116
+ from src.telegrambots.custom.filters import Filter
117
+ from telegrambots.wrapper.types.objects import Message
118
+
119
+
120
+ class AdvancedMessageFilter (Filter[Message]):
121
+ def __init__ (self ) -> None :
122
+ super ().__init__ ()
123
+ # ---- do your initialization here ----
124
+
125
+ def __check__ (self , update : Optional[Message]) -> bool :
126
+ # ---- check if update is a valid for your case ----
127
+ return True
128
+
129
+ # ---- or anything you like ----
130
+
131
+ # @dp.register_message_handler(AdvancedMessageFilter())
132
+ # ...
133
+ ```
134
+
135
+ #### Using factories ( Fast, Low options )
136
+
137
+ Or you can use filter factories ( available for each type of update ) to quickly create filter.
138
+
139
+ ``` py
140
+ import re
141
+
142
+ from src.telegrambots.custom.filters.messages import message_filter_factory
143
+
144
+
145
+ def regex (pattern : str | re.Pattern[str ]):
146
+ if isinstance (pattern, str ):
147
+ ap = re.compile(pattern)
148
+ else :
149
+ ap = pattern
150
+
151
+ return message_filter_factory(
152
+ lambda message : message.text is not None and ap.match(message.text) is not None
153
+ )
154
+
155
+ # @dp.register_message_handler(regex('start'))
156
+ # ...
157
+ ```
158
+
159
+ ...
0 commit comments