1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1298 from coder2020official/master

Custom filters
This commit is contained in:
Badiboy 2021-09-11 21:03:28 +03:00 committed by GitHub
commit df7808264f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 2 deletions

View File

@ -185,6 +185,7 @@ class TeleBot:
self.poll_answer_handlers = []
self.my_chat_member_handlers = []
self.chat_member_handlers = []
self.custom_filters = {}
if apihelper.ENABLE_MIDDLEWARE:
self.typed_middleware_handlers = {
@ -3097,8 +3098,16 @@ class TeleBot:
return True
@staticmethod
def _test_filter(message_filter, filter_value, message):
def add_custom_filter(self, custom_filter):
"""
Create custom filter.
:params:
custom_filter: Class with check(message) method."""
self.custom_filters[custom_filter.key] = custom_filter
def _test_filter(self, message_filter, filter_value, message):
"""
Test filters
:param message_filter: Filter type passed in handler
@ -3113,6 +3122,7 @@ class TeleBot:
# 'func': lambda msg: filter_value(msg)
# }
# return test_cases.get(message_filter, lambda msg: False)(message)
if message_filter == 'content_types':
return message.content_type in filter_value
elif message_filter == 'regexp':
@ -3123,9 +3133,24 @@ class TeleBot:
return message.chat.type in filter_value
elif message_filter == 'func':
return filter_value(message)
else:
return self._check_filter(message_filter,filter_value,message)
def _check_filter(self, message_filter, filter_value, message):
if message_filter in self.custom_filters:
filter_check = self.custom_filters.get(message_filter)
if isinstance(filter_value, util.SimpleCustomFilter):
if filter_value == filter_check.check(message): return True
else: return False
else:
if filter_check.check(message,filter_value) is True: return True
else: return False
else:
return False
def _notify_command_handlers(self, handlers, new_messages):
"""
Notifies command handlers

View File

@ -455,3 +455,32 @@ def webhook_google_functions(bot, request):
return 'Bot FAIL', 400
else:
return 'Bot ON'
class SimpleCustomFilter:
"""
Simple Custom Filter base class.
Create child class with check() method.
Accepts only bool.
"""
def check(message):
"""
Perform a check.
"""
pass
class AdvancedCustomFilter:
"""
Simple Custom Filter base class.
Create child class with check() method.
Can accept to parameters.
message: Message class
text: Filter value given in handler
"""
def check(message, text):
"""
Perform a check.
"""
pass