From 68095ad69a193533de82cb824e4398dd4c29a9ed Mon Sep 17 00:00:00 2001 From: Troshchk Date: Tue, 15 Feb 2022 15:24:55 +0100 Subject: [PATCH 1/3] 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.") From 907543021071376c9b41ecca335090df39135051 Mon Sep 17 00:00:00 2001 From: Troshchk Date: Tue, 15 Feb 2022 15:46:02 +0100 Subject: [PATCH 2/3] Making first condition shorter, no change in functionality --- telebot/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index f370205..d3b4e42 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -2783,7 +2783,7 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -2840,7 +2840,7 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -2888,7 +2888,7 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -2945,7 +2945,7 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -2993,7 +2993,7 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -3048,7 +3048,7 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -3094,7 +3094,7 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): @@ -3149,7 +3149,7 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, list) or isinstance(commands, set) or isinstance(commands, tuple): + if isinstance(commands, (list, set, 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): From ec766a3e43a39a2a24c069ac15fda02d4d277fd2 Mon Sep 17 00:00:00 2001 From: Troshchk Date: Wed, 16 Feb 2022 14:05:54 +0100 Subject: [PATCH 3/3] Wrapping checking in private methods; warnings changed to errors --- telebot/__init__.py | 200 +++++++++++++------------------------------- 1 file changed, 56 insertions(+), 144 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index d3b4e42..c18f7dc 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -2741,6 +2741,14 @@ class TeleBot: """ 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): """ Message handler decorator. @@ -2783,25 +2791,13 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, (list, set, 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.") + method_name = "message_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2840,25 +2836,13 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, (list, set, 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.") + method_name = "register_message_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("register_message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2888,25 +2872,13 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, (list, set, 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.") + method_name = "edited_message_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("edited_message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2945,25 +2917,13 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, (list, set, 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.") + method_name = "register_edited_message_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("register_edited_message_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -2993,25 +2953,13 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, (list, set, 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.") + method_name = "channel_post_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -3048,25 +2996,13 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, (list, set, 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.") + method_name = "register_channel_post_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("register_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -3094,25 +3030,13 @@ class TeleBot: if content_types is None: content_types = ["text"] - if isinstance(commands, (list, set, 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.") + method_name = "edited_channel_post_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.") @@ -3149,25 +3073,13 @@ class TeleBot: :param pass_bot: Pass TeleBot to handler. :return: decorated function """ - if isinstance(commands, (list, set, 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.") + method_name = "register_edited_channel_post_handler" + + if commands is not None: + self.__check_commands_input(commands, method_name) - if not isinstance(regexp, (str, type(None))): - logger.warning("message_handler: regex filter should be string. Not able to use the supplied type.") + if regexp is not None: + self.__check_regexp_input(regexp, method_name) if isinstance(content_types, str): logger.warning("register_edited_channel_post_handler: 'content_types' filter should be List of strings (content types), not string.")