Simplify and speedup _test_filter

This commit is contained in:
Badiboy 2021-08-19 22:46:12 +03:00
parent 6e871b8eb1
commit f5de0eeacf
2 changed files with 22 additions and 29 deletions

View File

@ -10,7 +10,7 @@
## <p align="center">Supported Bot API version: <a href="https://core.telegram.org/bots/api#june-25-2021">5.3</a>!
##Contents
## Contents
* [Getting started.](#getting-started)
* [Writing your first bot](#writing-your-first-bot)
@ -636,23 +636,8 @@ apihelper.proxy = {'https':'socks5://userproxy:password@proxy_address:port'}
* ✔ [Bot API 2.0](https://core.telegram.org/bots/api-changelog#april-9-2016)
## Change log
27.04.2020 - Poll and Dice are up to date.
Python2 conformance is not checked any more due to EOL.
11.04.2020 - Refactoring. new_chat_member is out of support. Bugfix in html_text. Started Bot API conformance checking.
06.06.2019 - Added polls support (Poll). Added functions send_poll, stop_poll
## F.A.Q.
### Bot 2.0
April 9,2016 Telegram release new bot 2.0 API, which has a drastic revision especially for the change of method's interface.If you want to update to the latest version, please make sure you've switched bot's code to bot 2.0 method interface.
[More information about pyTelegramBotAPI support bot2.0](https://github.com/eternnoir/pyTelegramBotAPI/issues/130)
### How can I distinguish a User and a GroupChat in message.chat?
Telegram Bot API support new type Chat for message.chat.
@ -682,7 +667,6 @@ Bot instances that were idle for a long time might be rejected by the server whe
Get help. Discuss. Chat.
* Join the [pyTelegramBotAPI Telegram Chat Group](https://telegram.me/joinchat/Bn4ixj84FIZVkwhk2jag6A)
* We now have a Telegram Channel as well! Keep yourself up to date with API changes, and [join it](https://telegram.me/pytelegrambotapi).
## More examples

View File

@ -3040,19 +3040,28 @@ class TeleBot:
def _test_filter(message_filter, filter_value, message):
"""
Test filters
:param message_filter:
:param filter_value:
:param message:
:return:
:param message_filter: Filter type passed in handler
:param filter_value: Filter value passed in handler
:param message: Message to test
:return: True if filter conforms
"""
test_cases = {
'content_types': lambda msg: msg.content_type in filter_value,
'regexp': lambda msg: msg.content_type == 'text' and re.search(filter_value, msg.text, re.IGNORECASE),
'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value,
'func': lambda msg: filter_value(msg)
}
return test_cases.get(message_filter, lambda msg: False)(message)
# test_cases = {
# 'content_types': lambda msg: msg.content_type in filter_value,
# 'regexp': lambda msg: msg.content_type == 'text' and re.search(filter_value, msg.text, re.IGNORECASE),
# 'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value,
# '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':
return message.content_type == 'text' and re.search(filter_value, message.text, re.IGNORECASE)
elif message_filter == 'commands':
return message.content_type == 'text' and util.extract_command(message.text) in filter_value
elif message_filter == 'func':
return filter_value(message)
else:
return False
def _notify_command_handlers(self, handlers, new_messages):
"""