2222from .handler import Handler
2323from telegram import Update
2424
25- from .filters import * # flake8: noqa
25+
26+ class Filters (object ):
27+ """
28+ Convenient namespace (class) & methods for the filter funcs of the
29+ MessageHandler class.
30+ """
31+
32+ @staticmethod
33+ def text (update ):
34+ return update .message .text and not update .message .text .startswith ('/' )
35+
36+ @staticmethod
37+ def command (update ):
38+ return update .message .text and update .message .text .startswith ('/' )
39+
40+ @staticmethod
41+ def audio (update ):
42+ return bool (update .message .audio )
43+
44+ @staticmethod
45+ def document (update ):
46+ return bool (update .message .document )
47+
48+ @staticmethod
49+ def photo (update ):
50+ return bool (update .message .photo )
51+
52+ @staticmethod
53+ def sticker (update ):
54+ return bool (update .message .sticker )
55+
56+ @staticmethod
57+ def video (update ):
58+ return bool (update .message .video )
59+
60+ @staticmethod
61+ def voice (update ):
62+ return bool (update .message .voice )
63+
64+ @staticmethod
65+ def contact (update ):
66+ return bool (update .message .contact )
67+
68+ @staticmethod
69+ def location (update ):
70+ return bool (update .message .location )
71+
72+ @staticmethod
73+ def venue (update ):
74+ return bool (update .message .venue )
75+
76+ @staticmethod
77+ def status_update (update ):
78+ return bool (
79+ update .message .new_chat_member or
80+ update .message .left_chat_member or
81+ update .message .new_chat_title or
82+ update .message .new_chat_photo or
83+ update .message .delete_chat_photo or
84+ update .message .group_chat_created or
85+ update .message .supergroup_chat_created or
86+ update .message .channel_chat_created or
87+ update .message .migrate_to_chat_id or
88+ update .message .migrate_from_chat_id or
89+ update .message .pinned_message )
2690
2791
2892class MessageHandler (Handler ):
@@ -32,8 +96,10 @@ class MessageHandler(Handler):
3296 updates.
3397
3498 Args:
35- filters (list): A list of filters defined in ``telegram.ext.filters``.
36- All messages that match at least one of those filters will be
99+ filters (list[function]): A list of filter functions. Standard filters
100+ can be found in the Filters class above.
101+ | Each `function` takes ``Update`` as arg and returns ``bool``.
102+ | All messages that match at least one of those filters will be
37103 accepted. If ``bool(filters)`` evaluates to ``False``, messages are
38104 not filtered.
39105 callback (function): A function that takes ``bot, update`` as
@@ -49,36 +115,14 @@ def __init__(self, filters, callback, pass_update_queue=False):
49115 self .filters = filters
50116
51117 def checkUpdate (self , update ):
52- filters = self .filters
53118 if isinstance (update , Update ) and update .message :
54- message = update .message
55- return (not filters or # If filters is empty, accept all messages
56- TEXT in filters and message .text and
57- not message .text .startswith ('/' ) or
58- AUDIO in filters and message .audio or
59- DOCUMENT in filters and message .document or
60- PHOTO in filters and message .photo or
61- STICKER in filters and message .sticker or
62- VIDEO in filters and message .video or
63- VOICE in filters and message .voice or
64- CONTACT in filters and message .contact or
65- LOCATION in filters and message .location or
66- VENUE in filters and message .venue or
67- STATUS_UPDATE in filters and (
68- message .new_chat_member or
69- message .left_chat_member or
70- message .new_chat_title or
71- message .new_chat_photo or
72- message .delete_chat_photo or
73- message .group_chat_created or
74- message .supergroup_chat_created or
75- message .channel_chat_created or
76- message .migrate_to_chat_id or
77- message .migrate_from_chat_id or
78- message .pinned_message )
79- )
119+ if not self .filters :
120+ res = True
121+ else :
122+ res = any (func (update ) for func in self .filters )
80123 else :
81- return False
124+ res = False
125+ return res
82126
83127 def handleUpdate (self , update , dispatcher ):
84128 optional_args = self .collectOptionalArgs (dispatcher )
0 commit comments