From 68095ad69a193533de82cb824e4398dd4c29a9ed Mon Sep 17 00:00:00 2001 From: Troshchk Date: Tue, 15 Feb 2022 15:24:55 +0100 Subject: [PATCH] Adding checks for the commands and regexp input types --- telebot/__init__.py | 160 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 144 insertions(+), 16 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 1eb496f..f370205 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -2783,9 +2783,25 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, str): - logger.warning("message_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2824,9 +2840,25 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, str): - logger.warning("register_message_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("register_message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2856,9 +2888,25 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, str): - logger.warning("edited_message_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("edited_message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2897,9 +2945,25 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, str): - logger.warning("register_edited_message_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("register_edited_message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2929,9 +2993,25 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, str): - logger.warning("channel_post_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2968,9 +3048,25 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, str): - logger.warning("register_channel_post_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("register_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2998,9 +3094,25 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, str): - logger.warning("edited_channel_post_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -3037,9 +3149,25 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, str): - logger.warning("register_edited_channel_post_handler: 'commands' filter should be List of strings (commands), not string.") + if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if any(not isinstance(item, (str, int, float)) for item in commands): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter list. Not able to use.") + elif all(isinstance(item, str) for item in commands): + commands = list(commands) + else: + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not list of different types. Converting non-strings to strings.") + commands = [str(item) if not isinstance(item,str) else item for item in commands] + elif isinstance(commands, str): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not string. Converting to list of string.") commands = [commands] + elif isinstance(commands, (int, float)): + logger.warning("message_handler: 'commands' filter should be list of strings (commands), not numeric. Converting to the list of string.") + commands = [str(commands)] + elif not isinstance(commands, type(None)): + logger.warning("message_handler: commands' filter should be list of strings (commands), unknown type supplied to the 'commands' filter. Not able to use.") + + if not isinstance(regexp, (str, type(None))): + logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") if isinstance(content_types, str): logger.warning("register_edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.")