From cc36207992a6003c673a27e18ab7b48ffb5c87cf Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sun, 2 Aug 2020 18:58:22 +0300 Subject: [PATCH 1/2] Minor keyboard update followup --- telebot/__init__.py | 2 +- telebot/types.py | 38 ++++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index ce41c6d..44119ba 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -18,7 +18,7 @@ console_output_handler = logging.StreamHandler(sys.stderr) console_output_handler.setFormatter(formatter) logger.addHandler(console_output_handler) -logger.setLevel(logging.WARNING) +logger.setLevel(logging.ERROR) from telebot import apihelper, types, util from telebot.handler_backends import MemoryHandlerBackend, FileHandlerBackend diff --git a/telebot/types.py b/telebot/types.py index 4e33277..3ea5e6e 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -808,10 +808,13 @@ class ReplyKeyboardRemove(JsonSerializable): class ReplyKeyboardMarkup(JsonSerializable): + max_row_keys = 12 + def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3): - if row_width>12: - logger.warning('Telegram does not support reply keyboard row width over 12') - row_width=12 + if row_width > self.max_row_keys: + # Todo: Will be replaced with Exception in future releases + logger.error('Telegram does not support reply keyboard row width over %d.' % self.max_row_keys) + row_width = self.max_row_keys self.resize_keyboard = resize_keyboard self.one_time_keyboard = one_time_keyboard @@ -830,12 +833,14 @@ class ReplyKeyboardMarkup(JsonSerializable): :param row_width: width of row :return: self, to allow function chaining. """ - row_width = row_width or self.row_width + if row_width is None: + row_width = self.row_width - if row_width>12: - logger.warning('Telegram does not support reply keyboard row width over 12') - row_width=12 + if row_width > self.max_row_keys: + # Todo: Will be replaced with Exception in future releases + logger.error('Telegram does not support reply keyboard row width over %d.' % self.max_row_keys) + row_width = self.max_row_keys for row in util.chunks(args, row_width): button_array = [] @@ -907,6 +912,8 @@ class KeyboardButtonPollType(Dictionaryable): class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): + max_row_keys = 12 + def __init__(self, row_width=3): """ This object represents an inline keyboard that appears @@ -914,9 +921,10 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): :return: """ - if row_width>8: - logger.warning('Telegram does not support inline keyboard row width over 8') - row_width=8 + if row_width > self.max_row_keys: + # Todo: Will be replaced with Exception in future releases + logger.error('Telegram does not support inline keyboard row width over %d.' % self.max_row_keys) + row_width = self.max_row_keys self.row_width = row_width self.keyboard = [] @@ -936,11 +944,13 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): :param row_width: width of row :return: self, to allow function chaining. """ - row_width = row_width or self.row_width + if row_width is None: + row_width = self.row_width - if row_width>8: - logger.warning('Telegram does not support inline keyboard row width over 8') - row_width=8 + if row_width > self.max_row_keys: + # Todo: Will be replaced with Exception in future releases + logger.error('Telegram does not support inline keyboard row width over %d.' % self.max_row_keys) + row_width = self.max_row_keys for row in util.chunks(args, row_width): button_array = [button.to_dict() for button in row] From a5fd407eb6a46608db0286ab7650665f8953087c Mon Sep 17 00:00:00 2001 From: Badiboy Date: Tue, 4 Aug 2020 12:29:56 +0300 Subject: [PATCH 2/2] Bugfix and DISABLE_KEYLEN_ERROR Bugfix and DISABLE_KEYLEN_ERROR to supress keyboard length errors. --- telebot/apihelper.py | 3 +-- telebot/types.py | 9 +++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 7603d17..fa61fe6 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -11,7 +11,6 @@ from requests.exceptions import HTTPError, ConnectionError, Timeout try: from requests.packages.urllib3 import fields - format_header_param = fields.format_header_param except ImportError: format_header_param = None @@ -130,7 +129,7 @@ def _check_result(method_name, result): raise ApiInvalidJSONException(method_name, result) if not result_json['ok']: - raise ApiTelegramException(msg, method_name, result, result_json) + raise ApiTelegramException(method_name, result, result_json) return result_json diff --git a/telebot/types.py b/telebot/types.py index 3ea5e6e..4d35176 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -11,8 +11,11 @@ import six from telebot import util +DISABLE_KEYLEN_ERROR = False + logger = logging.getLogger('TeleBot') + class JsonSerializable(object): """ Subclasses of this class are guaranteed to be able to be converted to JSON format. @@ -813,7 +816,8 @@ class ReplyKeyboardMarkup(JsonSerializable): def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3): if row_width > self.max_row_keys: # Todo: Will be replaced with Exception in future releases - logger.error('Telegram does not support reply keyboard row width over %d.' % self.max_row_keys) + if not DISABLE_KEYLEN_ERROR: + logger.error('Telegram does not support reply keyboard row width over %d.' % self.max_row_keys) row_width = self.max_row_keys self.resize_keyboard = resize_keyboard @@ -839,7 +843,8 @@ class ReplyKeyboardMarkup(JsonSerializable): if row_width > self.max_row_keys: # Todo: Will be replaced with Exception in future releases - logger.error('Telegram does not support reply keyboard row width over %d.' % self.max_row_keys) + if not DISABLE_KEYLEN_ERROR: + logger.error('Telegram does not support reply keyboard row width over %d.' % self.max_row_keys) row_width = self.max_row_keys for row in util.chunks(args, row_width):