From 02b886465e3b6d7bfa05067beceaf8e38a0d4c8f Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sun, 25 Jul 2021 15:46:53 +0500 Subject: [PATCH] new filters --- telebot/__init__.py | 56 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index b13c396..b5ce7cf 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -2461,7 +2461,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, is_private=None, supergroup=None, user_id=None, chat_id=None, **kwargs): """ Message handler decorator. This decorator can be used to decorate functions that must handle certain types of messages. @@ -2487,12 +2487,32 @@ class TeleBot: 'text', 'location', 'contact', 'sticker']) def default_command(message): bot.send_message(message.chat.id, "This is the default command handler.") + # Handle all messages in private chat. + @bot.message_handler(is_private=True) + def default_command(message): + bot.send_message(message.chat.id, "You wrote message in private chat") + # Handle all messages in group/supergroup. + @bot.message_handler(supergroup=True) + def default_command(message): + bot.send_message(message.chat.id, "You wrote message in supergroup") + # Handle all messages from user 11111 + @bot.message_handler(user_id=[11111]) + def default_command(message): + bot.send_message(message.chat.id, "You wrote me message in special chat")# This doesn't work for other users than '11111' + # Handle all messages in specific group/supergroup. + @bot.message_handler(chat_id=[1111]]) + def default_command(message): + bot.send_message(message.chat.id, "You wrote message in special supergroup") :param commands: Optional list of strings (commands to handle). :param regexp: Optional regular expression. :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 is_private: True/False, only private chats. + :param supergroup: True/False, only supergroups/groups. + :param user_id: List of user ids, which can use handler. + :param chat_id: List of chat_ids where handler is executed. """ if content_types is None: @@ -2503,6 +2523,10 @@ class TeleBot: content_types=content_types, commands=commands, regexp=regexp, + is_private=is_private, + supergroup=supergroup, + user_id=user_id, + chat_id = chat_id, func=func, **kwargs) self.add_message_handler(handler_dict) @@ -2518,13 +2542,17 @@ class TeleBot: """ self.message_handlers.append(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, is_private=None, supergroup=None, user_id=None, chat_id=None, **kwargs): """ Edit message handler decorator :param commands: :param regexp: :param func: :param content_types: + :param is_private: + :param supergroup: + :param user_id: + :param chat_id: :param kwargs: :return: """ @@ -2534,10 +2562,14 @@ class TeleBot: def decorator(handler): handler_dict = self._build_handler_dict(handler, + content_types=content_types, commands=commands, regexp=regexp, + is_private=is_private, + supergroup=supergroup, + user_id=user_id, + chat_id = chat_id, func=func, - content_types=content_types, **kwargs) self.add_edited_message_handler(handler_dict) return handler @@ -2552,13 +2584,14 @@ class TeleBot: """ self.edited_message_handlers.append(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, chat_id=None, **kwargs): """ Channel post handler decorator :param commands: :param regexp: :param func: :param content_types: + :param chat_id: :param kwargs: :return: """ @@ -2568,10 +2601,11 @@ class TeleBot: def decorator(handler): handler_dict = self._build_handler_dict(handler, + content_types=content_types, commands=commands, regexp=regexp, + chat_id = chat_id, func=func, - content_types=content_types, **kwargs) self.add_channel_post_handler(handler_dict) return handler @@ -2586,7 +2620,7 @@ class TeleBot: """ self.channel_post_handlers.append(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, is_private=None, supergroup=None, user_id=None, chat_id=None, **kwargs): """ Edit channel post handler decorator :param commands: @@ -2602,10 +2636,14 @@ class TeleBot: def decorator(handler): handler_dict = self._build_handler_dict(handler, + content_types=content_types, commands=commands, regexp=regexp, + is_private=is_private, + supergroup=supergroup, + user_id=user_id, + chat_id = chat_id, func=func, - content_types=content_types, **kwargs) self.add_edited_channel_post_handler(handler_dict) return handler @@ -2857,6 +2895,10 @@ class TeleBot: '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, + 'is_private': lambda msg: msg.chat.type == 'private', + 'supergroup': lambda msg: msg.chat.type == "supergroup", + 'user_id': lambda msg: msg.from_user.id in filter_value, + 'chat_id': lambda msg: msg.chat.id in filter_value, 'func': lambda msg: filter_value(msg) }