mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Merge pull request #1272 from Badiboy/master
Simplify and speedup _test_filter
This commit is contained in:
commit
06923c8274
18
README.md
18
README.md
@ -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>!
|
## <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)
|
* [Getting started.](#getting-started)
|
||||||
* [Writing your first bot](#writing-your-first-bot)
|
* [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)
|
* ✔ [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.
|
## 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?
|
### How can I distinguish a User and a GroupChat in message.chat?
|
||||||
Telegram Bot API support new type Chat for 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.
|
Get help. Discuss. Chat.
|
||||||
|
|
||||||
* Join the [pyTelegramBotAPI Telegram Chat Group](https://telegram.me/joinchat/Bn4ixj84FIZVkwhk2jag6A)
|
* 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
|
## More examples
|
||||||
|
|
||||||
|
@ -2544,11 +2544,11 @@ class TeleBot:
|
|||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback,
|
||||||
content_types=content_types,
|
content_types=content_types,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
regexp=regexp,
|
regexp=regexp,
|
||||||
func=func,
|
func=func,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.add_message_handler(handler_dict)
|
self.add_message_handler(handler_dict)
|
||||||
def edited_message_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
def edited_message_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -2595,11 +2595,11 @@ class TeleBot:
|
|||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback,
|
||||||
content_types=content_types,
|
content_types=content_types,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
regexp=regexp,
|
regexp=regexp,
|
||||||
func=func,
|
func=func,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.add_edited_message_handler(handler_dict)
|
self.add_edited_message_handler(handler_dict)
|
||||||
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -2646,11 +2646,11 @@ class TeleBot:
|
|||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback,
|
||||||
content_types=content_types,
|
content_types=content_types,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
regexp=regexp,
|
regexp=regexp,
|
||||||
func=func,
|
func=func,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.add_channel_post_handler(handler_dict)
|
self.add_channel_post_handler(handler_dict)
|
||||||
def edited_channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
def edited_channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -2697,11 +2697,11 @@ class TeleBot:
|
|||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback,
|
||||||
content_types=content_types,
|
content_types=content_types,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
regexp=regexp,
|
regexp=regexp,
|
||||||
func=func,
|
func=func,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.add_edited_channel_post_handler(handler_dict)
|
self.add_edited_channel_post_handler(handler_dict)
|
||||||
|
|
||||||
def inline_handler(self, func, **kwargs):
|
def inline_handler(self, func, **kwargs):
|
||||||
@ -2734,9 +2734,7 @@ class TeleBot:
|
|||||||
:param func:
|
:param func:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
|
||||||
func=func,
|
|
||||||
**kwargs)
|
|
||||||
self.add_inline_handler(handler_dict)
|
self.add_inline_handler(handler_dict)
|
||||||
|
|
||||||
def chosen_inline_handler(self, func, **kwargs):
|
def chosen_inline_handler(self, func, **kwargs):
|
||||||
@ -2769,9 +2767,7 @@ class TeleBot:
|
|||||||
:param func:
|
:param func:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
|
||||||
func=func,
|
|
||||||
**kwargs)
|
|
||||||
self.add_chosen_inline_handler(handler_dict)
|
self.add_chosen_inline_handler(handler_dict)
|
||||||
|
|
||||||
|
|
||||||
@ -2805,9 +2801,7 @@ class TeleBot:
|
|||||||
:param func:
|
:param func:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
|
||||||
func=func,
|
|
||||||
**kwargs)
|
|
||||||
self.add_callback_query_handler(handler_dict)
|
self.add_callback_query_handler(handler_dict)
|
||||||
|
|
||||||
def shipping_query_handler(self, func, **kwargs):
|
def shipping_query_handler(self, func, **kwargs):
|
||||||
@ -2840,9 +2834,7 @@ class TeleBot:
|
|||||||
:param func:
|
:param func:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
|
||||||
func=func,
|
|
||||||
**kwargs)
|
|
||||||
self.add_shipping_query_handler(handler_dict)
|
self.add_shipping_query_handler(handler_dict)
|
||||||
|
|
||||||
def pre_checkout_query_handler(self, func, **kwargs):
|
def pre_checkout_query_handler(self, func, **kwargs):
|
||||||
@ -2875,9 +2867,7 @@ class TeleBot:
|
|||||||
:param func:
|
:param func:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
|
||||||
func=func,
|
|
||||||
**kwargs)
|
|
||||||
self.add_pre_checkout_query_handler(handler_dict)
|
self.add_pre_checkout_query_handler(handler_dict)
|
||||||
|
|
||||||
def poll_handler(self, func, **kwargs):
|
def poll_handler(self, func, **kwargs):
|
||||||
@ -3015,9 +3005,7 @@ class TeleBot:
|
|||||||
:param func:
|
:param func:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
handler_dict = self._build_handler_dict(callback,
|
handler_dict = self._build_handler_dict(callback, func=func, **kwargs)
|
||||||
func=func,
|
|
||||||
**kwargs)
|
|
||||||
self.add_chat_member_handler(handler_dict)
|
self.add_chat_member_handler(handler_dict)
|
||||||
|
|
||||||
def _test_message_handler(self, message_handler, message):
|
def _test_message_handler(self, message_handler, message):
|
||||||
@ -3040,19 +3028,28 @@ class TeleBot:
|
|||||||
def _test_filter(message_filter, filter_value, message):
|
def _test_filter(message_filter, filter_value, message):
|
||||||
"""
|
"""
|
||||||
Test filters
|
Test filters
|
||||||
:param message_filter:
|
:param message_filter: Filter type passed in handler
|
||||||
:param filter_value:
|
:param filter_value: Filter value passed in handler
|
||||||
:param message:
|
:param message: Message to test
|
||||||
:return:
|
:return: True if filter conforms
|
||||||
"""
|
"""
|
||||||
test_cases = {
|
# test_cases = {
|
||||||
'content_types': lambda msg: msg.content_type in filter_value,
|
# '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),
|
# '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,
|
# 'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value,
|
||||||
'func': lambda msg: filter_value(msg)
|
# 'func': lambda msg: filter_value(msg)
|
||||||
}
|
# }
|
||||||
|
# return test_cases.get(message_filter, lambda msg: False)(message)
|
||||||
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):
|
def _notify_command_handlers(self, handlers, new_messages):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user