From 6832c337333536f39a500c7bccc1b354633189c8 Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Mon, 31 Aug 2020 12:00:56 +0000 Subject: [PATCH 1/8] feat: Added the field reply_markup to the Message Added the field `reply_markup` to the Message object --- telebot/types.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/telebot/types.py b/telebot/types.py index 8597101..e44d6b5 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -385,6 +385,9 @@ class Message(JsonDeserializable): if 'passport_data' in obj: opts['passport_data'] = obj['passport_data'] content_type = 'passport_data' + if 'reply_markup' in obj: + opts['reply_markup'] = InlineKeyboardMarkup.de_json(obj['reply_markup']) + content_type = 'reply_markup' return cls(message_id, from_user, date, chat, content_type, opts, json_string) @classmethod @@ -455,6 +458,7 @@ class Message(JsonDeserializable): self.invoice = None self.successful_payment = None self.connected_website = None + self.reply_markup = None for key in options: setattr(self, key, options[key]) self.json = json_string From cdae65116b92e1d5dbace592588e9a6b436b66e4 Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Tue, 1 Sep 2020 18:03:21 +0800 Subject: [PATCH 2/8] feat: make LoginUrl JsonDeserializable feat: make LoginUrl JsonDeserializable, add de_json func --- telebot/types.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/telebot/types.py b/telebot/types.py index e44d6b5..7d88a06 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1017,12 +1017,23 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): return json_dict -class LoginUrl(Dictionaryable, JsonSerializable): +class LoginUrl(Dictionaryable, JsonSerializable, JsonDeserializable): def __init__(self, url, forward_text=None, bot_username=None, request_write_access=None): self.url = url self.forward_text = forward_text self.bot_username = bot_username self.request_write_access = request_write_access + + @classmethod + def de_json(cls, json_string): + if (json_string is None): + return None + obj = cls.check_json(json_string) + url = obj['url'] + forward_text = obj.get('forward_text') + bot_username = obj.get('bot_username') + request_write_access = obj.get('request_write_access') + return cls(url, forward_text, bot_username, request_write_access) def to_json(self): return json.dumps(self.to_dict()) From 630a9a5b2ca653193de5b3f31672d45995a53fa4 Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Tue, 1 Sep 2020 18:07:45 +0800 Subject: [PATCH 3/8] feat: make InlineKeyboardButton JsonDeserializable feat: make InlineKeyboardButton JsonDeserializable, add de_json func to InlineKeyboardButton Object --- telebot/types.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/telebot/types.py b/telebot/types.py index 7d88a06..adf7e18 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1049,7 +1049,7 @@ class LoginUrl(Dictionaryable, JsonSerializable, JsonDeserializable): return json_dict -class InlineKeyboardButton(Dictionaryable, JsonSerializable): +class InlineKeyboardButton(Dictionaryable, JsonSerializable, JsonDeserializable): def __init__(self, text, url=None, callback_data=None, switch_inline_query=None, switch_inline_query_current_chat=None, callback_game=None, pay=None, login_url=None): self.text = text @@ -1060,6 +1060,21 @@ class InlineKeyboardButton(Dictionaryable, JsonSerializable): self.callback_game = callback_game self.pay = pay self.login_url = login_url + + @classmethod + def de_json(cls, json_string): + if (json_string is None): + return None + obj = cls.check_json(json_string) + text = obj['text'] + url = obj.get('url') + callback_data = obj.get('callback_data') + switch_inline_query = obj.get('switch_inline_query') + switch_inline_query_current_chat = obj.get('switch_inline_query_current_chat') + callback_game = obj.get('callback_game') + pay = obj.get('pay') + login_url = LoginUrl.de_json(obj.get('login_url')) + return cls(text, url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay, login_url) def to_json(self): return json.dumps(self.to_dict()) From decad450d0e1fa4433f36cfbd465c7d26aee551d Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Tue, 1 Sep 2020 18:13:22 +0800 Subject: [PATCH 4/8] feat: make InlineKeyboardMarkup JsonDeserializable feat: make InlineKeyboardMarkup JsonDeserializable, add de_json func to InlineKeyboardMarkup object --- telebot/types.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index adf7e18..9ae4ccc 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -941,10 +941,18 @@ class KeyboardButtonPollType(Dictionaryable): return {'type': self.type} -class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): +class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable)): max_row_keys = 8 + + @classmethod + def de_json(cls, json_string): + if (json_string is None): + return None + obj = cls.check_json(json_string) + keyboard = [[button for button in row] for row in obj['inline_keyboard']] + return cls(keyboard) - def __init__(self, row_width=3): + def __init__(self, keyboard=[] ,row_width=3): """ This object represents an inline keyboard that appears right next to the message it belongs to. @@ -957,7 +965,7 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): row_width = self.max_row_keys self.row_width = row_width - self.keyboard = [] + self.keyboard = keyboard def add(self, *args, row_width=None): """ From 32a9e65ecca3781cf067c81251bd9c13a763b5d1 Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Wed, 2 Sep 2020 09:12:49 +0800 Subject: [PATCH 5/8] fix: reply_markup does not change content_type --- telebot/types.py | 1 - 1 file changed, 1 deletion(-) diff --git a/telebot/types.py b/telebot/types.py index 9ae4ccc..45181f3 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -387,7 +387,6 @@ class Message(JsonDeserializable): content_type = 'passport_data' if 'reply_markup' in obj: opts['reply_markup'] = InlineKeyboardMarkup.de_json(obj['reply_markup']) - content_type = 'reply_markup' return cls(message_id, from_user, date, chat, content_type, opts, json_string) @classmethod From a803edd09b70e3ff3636e19f2dbd98c6769c6e47 Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Wed, 2 Sep 2020 09:25:23 +0800 Subject: [PATCH 6/8] fix: button in markup should be obj, not json text --- telebot/types.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index 45181f3..0c9b99e 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -940,7 +940,7 @@ class KeyboardButtonPollType(Dictionaryable): return {'type': self.type} -class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable)): +class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable): max_row_keys = 8 @classmethod @@ -948,7 +948,7 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable) if (json_string is None): return None obj = cls.check_json(json_string) - keyboard = [[button for button in row] for row in obj['inline_keyboard']] + keyboard = [[InlineKeyboardButton.de_json(button) for button in row] for row in obj['inline_keyboard']] return cls(keyboard) def __init__(self, keyboard=[] ,row_width=3): @@ -990,7 +990,7 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable) row_width = self.max_row_keys for row in util.chunks(args, row_width): - button_array = [button.to_dict() for button in row] + button_array = [button for button in row] self.keyboard.append(button_array) return self @@ -1020,7 +1020,8 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable) return json.dumps(self.to_dict()) def to_dict(self): - json_dict = {'inline_keyboard': self.keyboard} + json_dict = dict() + json_dict['inline_keyboard'] = [[json.loads(button.to_json()) for button in row] for row in self.keyboard] return json_dict From 698b4371e6d5aca396fe011de3d9b17ceffbe801 Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Wed, 2 Sep 2020 10:33:32 +0800 Subject: [PATCH 7/8] test: Add tests for InlineKeyboardMarkup and ... Add tests for InlineKeyboardMarkup and InlineKeyboardButton --- tests/test_types.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_types.py b/tests/test_types.py index 2f1e698..173cda9 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -17,6 +17,28 @@ def test_json_message(): assert msg.text == 'HIHI' +def test_json_message_with_reply_markup(): + jsonstring = r'{"message_id":48,"from":{"id":153587469,"is_bot":false,"first_name":"Neko","username":"Neko"},"chat":{"id":153587469,"first_name":"Neko","username":"Neko","type":"private"},"date":1598879570,"text":"test","reply_markup":{"inline_keyboard":[[{"text":"Google","url":"http://www.google.com"},{"text":"Yahoo","url":"http://www.yahoo.com"}]]}}' + msg = types.Message.de_json(jsonstring) + assert msg.content_type == 'text' + assert msg.reply_markup.keyboard[0][0].text == 'Google' + + +def test_json_InlineKeyboardMarkup(): + jsonstring = r'{"inline_keyboard":[[{"text":"Google","url":"http://www.google.com"},{"text":"Yahoo","url":"http://www.yahoo.com"}]]}' + markup = types.InlineKeyboardMarkup.de_json(jsonstring) + assert markup.keyboard[0][0].text == 'Google' + assert markup.keyboard[0][1].url == 'http://www.yahoo.com' + + +def test_json_InlineKeyboardButton(): + jsonstring = r'{"text":"Google","url":"http://www.google.com"}' + button = types.InlineKeyboardButton.de_json(jsonstring) + assert button.text == 'Google' + assert button.url == 'http://www.google.com' + + + def test_json_message_with_dice(): jsonstring = r'{"message_id":5560,"from":{"id":879343317,"is_bot":false,"first_name":"George","last_name":"Forse","username":"dr_fxrse","language_code":"ru"},"chat":{"id":879343317,"first_name":"George","last_name":"Forse","username":"dr_fxrse","type":"private"},"date":1586926330,"dice":{"value": 4, "emoji": "\ud83c\udfaf"}}' msg = types.Message.de_json(jsonstring) From 9ab906e60ccbcba7a445d9dac5d4be215cecdd3b Mon Sep 17 00:00:00 2001 From: meoww-bot <14239840+meoww-bot@users.noreply.github.com> Date: Wed, 2 Sep 2020 18:09:14 +0800 Subject: [PATCH 8/8] fix: simplify code json.loads(button.to_json()) equals to button.to_dict() --- telebot/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telebot/types.py b/telebot/types.py index 0c9b99e..f1b9af0 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1021,7 +1021,7 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable) def to_dict(self): json_dict = dict() - json_dict['inline_keyboard'] = [[json.loads(button.to_json()) for button in row] for row in self.keyboard] + json_dict['inline_keyboard'] = [[button.to_dict() for button in row] for row in self.keyboard] return json_dict