forked from pytdbot/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.py
More file actions
61 lines (39 loc) · 1.49 KB
/
filters.py
File metadata and controls
61 lines (39 loc) · 1.49 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
from typing import Callable
class Filter:
r"""Filter class
A filter is a function that takes a request and returns a boolean. If the returned value is ``True`` then the handler will be called.
See :func:`~pytdbot.filters.create` for more information
"""
def __init__(self, func: Callable):
self.func = func
if not callable(func):
raise TypeError("func must be callable")
def __str__(self) -> str:
return f"Filter(func={self.func})"
def __repr__(self) -> str:
return str(self)
def create(func: Callable) -> Filter:
r"""A factory to create a filter
Example:
.. code-block:: python
from pytdbot import filters, Client
client = Client(...)
# Create a filter by a decorator
@filters.create
async def filter_photo(_, event) -> bool:
if event.content_type == "messagePhoto":
return True
return False
# Or by a function
filter_photo = filters.create(filter_photo)
# Or by lambda
filter_photo = filters.create(lambda _, event: event.content_type == "messagePhoto")
@client.on_updateNewMessage(filters=filter_photo)
async def photo_handler(c,update):
await update.reply_text('I got a photo!')
client.run()
Parameters:
func (``Callable``):
The filter function
"""
return Filter(func)