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

Merge pull request #1295 from coder2020official/master

is_private filter
This commit is contained in:
Badiboy 2021-09-10 22:17:05 +03:00 committed by GitHub
commit 78fb69ded1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -167,6 +167,7 @@ TeleBot supports the following filters:
|content_types|list of strings (default `['text']`)|`True` if message.content_type is in the list of strings.|
|regexp|a regular expression as a string|`True` if `re.search(regexp_arg)` returns `True` and `message.content_type == 'text'` (See [Python Regular Expressions](https://docs.python.org/2/library/re.html))|
|commands|list of strings|`True` if `message.content_type == 'text'` and `message.text` starts with a command that is in the list of strings.|
|chat_types|list of chat types|`True` if `message.chat.type` in your filter
|func|a function (lambda or function reference)|`True` if the lambda or function reference returns `True`
Here are some examples of using the filters and message handlers:

View File

@ -2476,7 +2476,7 @@ class TeleBot:
else:
self.default_middleware_handlers.append(handler)
def message_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
def message_handler(self, commands=None, regexp=None, func=None, content_types=None, chat_types=None, **kwargs):
"""
Message handler decorator.
This decorator can be used to decorate functions that must handle certain types of messages.
@ -2491,6 +2491,11 @@ class TeleBot:
def command_help(message):
bot.send_message(message.chat.id, 'Did someone call for help?')
# Handles messages in private chat
@bot.message_handler(chat_types=['private']) # You can add more chat types
def command_help(message):
bot.send_message(message.chat.id, 'Private chat detected, sir!')
# Handle all sent documents of type 'text/plain'.
@bot.message_handler(func=lambda message: message.document.mime_type == 'text/plain',
content_types=['document'])
@ -2508,6 +2513,7 @@ class TeleBot:
:param func: Optional lambda function. The lambda receives the message to test as the first parameter.
It must return True if the command should handle the message.
:param content_types: Supported message content types. Must be a list. Defaults to ['text'].
:param chat_types: list of chat types
"""
if content_types is None:
@ -2518,6 +2524,7 @@ class TeleBot:
content_types=content_types,
commands=commands,
regexp=regexp,
chat_types=chat_types,
func=func,
**kwargs)
self.add_message_handler(handler_dict)
@ -2533,7 +2540,7 @@ class TeleBot:
"""
self.message_handlers.append(handler_dict)
def register_message_handler(self, callback, content_types=None, commands=None, regexp=None, func=None, **kwargs):
def register_message_handler(self, callback, content_types=None, commands=None, regexp=None, func=None, chat_types=None, **kwargs):
"""
Registers message handler.
:param callback: function to be called
@ -2541,6 +2548,7 @@ class TeleBot:
:param commands: list of commands
:param regexp:
:param func:
:param chat_types: True for private chat
:return: decorated function
"""
handler_dict = self._build_handler_dict(callback,
@ -2548,15 +2556,17 @@ class TeleBot:
commands=commands,
regexp=regexp,
func=func,
chat_types=chat_types,
**kwargs)
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, chat_types=None, **kwargs):
"""
Edit message handler decorator
:param commands:
:param regexp:
:param func:
:param content_types:
:param chat_types: list of chat types
:param kwargs:
:return:
"""
@ -2570,6 +2580,7 @@ class TeleBot:
regexp=regexp,
func=func,
content_types=content_types,
chat_types=chat_types,
**kwargs)
self.add_edited_message_handler(handler_dict)
return handler
@ -2584,7 +2595,7 @@ class TeleBot:
"""
self.edited_message_handlers.append(handler_dict)
def register_edited_message_handler(self, callback, content_types=None, commands=None, regexp=None, func=None, **kwargs):
def register_edited_message_handler(self, callback, content_types=None, commands=None, regexp=None, func=None, chat_types=None, **kwargs):
"""
Registers edited message handler.
:param callback: function to be called
@ -2592,6 +2603,7 @@ class TeleBot:
:param commands: list of commands
:param regexp:
:param func:
:param chat_types: True for private chat
:return: decorated function
"""
handler_dict = self._build_handler_dict(callback,
@ -2599,6 +2611,7 @@ class TeleBot:
commands=commands,
regexp=regexp,
func=func,
chat_types=chat_types,
**kwargs)
self.add_edited_message_handler(handler_dict)
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
@ -3046,6 +3059,8 @@ class TeleBot:
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 == 'chat_types':
return message.chat.type in filter_value
elif message_filter == 'func':
return filter_value(message)
else: