Skip to content

Commit 2c15b4d

Browse files
committed
Updated readme
1 parent 97fb628 commit 2c15b4d

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.mypy_cache/
44
__pycache__/
55
_out/
6-
dummy_test.py
6+
dummy_test*
77
.pytest_cache/
88
.env
99
telegrambots.egg-info/

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ if __name__ == "__main__":
6363
asyncio.run(main())
6464
```
6565

66-
Process updates in parallel:
66+
### Process updates in parallel
67+
68+
Dispatcher processes updates sequentially by default. but you can change the behavior.
6769

6870
```py
6971
# ---- sniff ----
@@ -81,7 +83,9 @@ dp = Dispatcher(
8183
)
8284
```
8385

84-
Manage propagation of handlers:
86+
### Manage propagation of handlers
87+
88+
Stop processing this handler or all of pending handlers.
8589

8690
```py
8791
@dp.register_message_handler(mf.regex("^/start") & mf.private)
@@ -97,3 +101,59 @@ async def handle_message(context: MessageContext):
97101
context.continue_propagation() # -> continue propagating this update to other handlers.
98102

99103
```
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

Comments
 (0)