Skip to content

Commit c5d5680

Browse files
committed
update_filter attribute on filters
Makes it possible to have filters work on an update instead of message, while keeping behavior for current filters
1 parent 87afd98 commit c5d5680

File tree

4 files changed

+434
-424
lines changed

4 files changed

+434
-424
lines changed

telegram/ext/commandhandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def check_update(self, update):
160160
command[1].lower() == message.bot.username.lower()):
161161
return None
162162

163-
if self.filters is None or self.filters(message):
163+
if self.filters is None or self.filters(update):
164164
return args
165165

166166
def collect_optional_args(self, dispatcher, update=None, check_result=None):
@@ -315,5 +315,5 @@ def check_update(self, update):
315315
text_list = message.text.split()
316316
if text_list[0].lower() not in self.command:
317317
return None
318-
if self.filters is None or self.filters(message):
318+
if self.filters is None or self.filters(update):
319319
return text_list[1:]

telegram/ext/filters.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ class variable.
6060
"""
6161

6262
name = None
63+
update_filter = False
6364

64-
def __call__(self, message):
65-
return self.filter(message)
65+
def __call__(self, update):
66+
if self.update_filter:
67+
return self.filter(update)
68+
else:
69+
return self.filter(update.effective_message)
6670

6771
def __and__(self, other):
6872
return MergedFilter(self, and_filter=other)
@@ -100,12 +104,13 @@ class InvertedFilter(BaseFilter):
100104
f: The filter to invert.
101105
102106
"""
107+
update_filter = True
103108

104109
def __init__(self, f):
105110
self.f = f
106111

107-
def filter(self, message):
108-
return not self.f(message)
112+
def filter(self, update):
113+
return not self.f(update)
109114

110115
def __repr__(self):
111116
return "<inverted {}>".format(self.f)
@@ -120,17 +125,18 @@ class MergedFilter(BaseFilter):
120125
or_filter: Optional filter to "or" with base_filter. Mutually exclusive with and_filter.
121126
122127
"""
128+
update_filter = True
123129

124130
def __init__(self, base_filter, and_filter=None, or_filter=None):
125131
self.base_filter = base_filter
126132
self.and_filter = and_filter
127133
self.or_filter = or_filter
128134

129-
def filter(self, message):
135+
def filter(self, update):
130136
if self.and_filter:
131-
return self.base_filter(message) and self.and_filter(message)
137+
return self.base_filter(update) and self.and_filter(update)
132138
elif self.or_filter:
133-
return self.base_filter(message) or self.or_filter(message)
139+
return self.base_filter(update) or self.or_filter(update)
134140

135141
def __repr__(self):
136142
return "<{} {} {}>".format(self.base_filter, "and" if self.and_filter else "or",
@@ -159,6 +165,7 @@ class _Text(BaseFilter):
159165
name = 'Filters.text'
160166

161167
def filter(self, message):
168+
print('text_filter_filter {}'.format(repr(self)))
162169
return bool(message.text and not message.text.startswith('/'))
163170

164171
text = _Text()
@@ -380,6 +387,7 @@ class _StatusUpdate(BaseFilter):
380387
``Filters.status_update`` for all status update messages.
381388
382389
"""
390+
update_filter = True
383391

384392
class _NewChatMembers(BaseFilter):
385393
name = 'Filters.status_update.new_chat_members'

telegram/ext/messagehandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,4 @@ def check_update(self, update):
148148
if not self.filters:
149149
return True
150150
else:
151-
return self.filters(update.effective_message)
151+
return self.filters(update)

0 commit comments

Comments
 (0)