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

Merge pull request #1454 from Troshchk/message_handler_checking

Additional check of the regexp and commands types
This commit is contained in:
Badiboy 2022-02-19 21:33:30 +03:00 committed by GitHub
commit fc374ec57a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2741,6 +2741,14 @@ class TeleBot:
""" """
self.add_middleware_handler(callback, update_types) self.add_middleware_handler(callback, update_types)
def __check_commands_input(self, commands, method_name):
if not isinstance(commands, list) or not all(isinstance(item, str) for item in commands):
logger.error(f"{method_name}: Commands filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use the supplied type.")
def __check_regexp_input(self, regexp, method_name):
if not isinstance(regexp, str):
logger.error(f"{method_name}: Regexp filter should be string. Not able to use the supplied type.")
def message_handler(self, commands=None, regexp=None, func=None, content_types=None, chat_types=None, **kwargs): def message_handler(self, commands=None, regexp=None, func=None, content_types=None, chat_types=None, **kwargs):
""" """
Message handler decorator. Message handler decorator.
@ -2783,9 +2791,13 @@ class TeleBot:
if content_types is None: if content_types is None:
content_types = ["text"] content_types = ["text"]
if isinstance(commands, str): method_name = "message_handler"
logger.warning("message_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("message_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("message_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -2824,9 +2836,13 @@ class TeleBot:
:param pass_bot: Pass TeleBot to handler. :param pass_bot: Pass TeleBot to handler.
:return: decorated function :return: decorated function
""" """
if isinstance(commands, str): method_name = "register_message_handler"
logger.warning("register_message_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("register_message_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("register_message_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -2856,9 +2872,13 @@ class TeleBot:
if content_types is None: if content_types is None:
content_types = ["text"] content_types = ["text"]
if isinstance(commands, str): method_name = "edited_message_handler"
logger.warning("edited_message_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("edited_message_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("edited_message_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -2897,9 +2917,13 @@ class TeleBot:
:param pass_bot: Pass TeleBot to handler. :param pass_bot: Pass TeleBot to handler.
:return: decorated function :return: decorated function
""" """
if isinstance(commands, str): method_name = "register_edited_message_handler"
logger.warning("register_edited_message_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("register_edited_message_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("register_edited_message_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -2929,9 +2953,13 @@ class TeleBot:
if content_types is None: if content_types is None:
content_types = ["text"] content_types = ["text"]
if isinstance(commands, str): method_name = "channel_post_handler"
logger.warning("channel_post_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("channel_post_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -2968,9 +2996,13 @@ class TeleBot:
:param pass_bot: Pass TeleBot to handler. :param pass_bot: Pass TeleBot to handler.
:return: decorated function :return: decorated function
""" """
if isinstance(commands, str): method_name = "register_channel_post_handler"
logger.warning("register_channel_post_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("register_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("register_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -2998,9 +3030,13 @@ class TeleBot:
if content_types is None: if content_types is None:
content_types = ["text"] content_types = ["text"]
if isinstance(commands, str): method_name = "edited_channel_post_handler"
logger.warning("edited_channel_post_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.")
@ -3037,9 +3073,13 @@ class TeleBot:
:param pass_bot: Pass TeleBot to handler. :param pass_bot: Pass TeleBot to handler.
:return: decorated function :return: decorated function
""" """
if isinstance(commands, str): method_name = "register_edited_channel_post_handler"
logger.warning("register_edited_channel_post_handler: 'commands' filter should be List of strings (commands), not string.")
commands = [commands] if commands is not None:
self.__check_commands_input(commands, method_name)
if regexp is not None:
self.__check_regexp_input(regexp, method_name)
if isinstance(content_types, str): if isinstance(content_types, str):
logger.warning("register_edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") logger.warning("register_edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.")