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

Merge pull request #970 from meoww-bot/master

UPG: Added the field reply_markup to the Message object
This commit is contained in:
Badiboy 2020-09-07 13:09:46 +03:00 committed by GitHub
commit 5fda52cf5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 7 deletions

View File

@ -385,6 +385,8 @@ class Message(JsonDeserializable):
if 'passport_data' in obj: if 'passport_data' in obj:
opts['passport_data'] = obj['passport_data'] opts['passport_data'] = obj['passport_data']
content_type = 'passport_data' content_type = 'passport_data'
if 'reply_markup' in obj:
opts['reply_markup'] = InlineKeyboardMarkup.de_json(obj['reply_markup'])
return cls(message_id, from_user, date, chat, content_type, opts, json_string) return cls(message_id, from_user, date, chat, content_type, opts, json_string)
@classmethod @classmethod
@ -455,6 +457,7 @@ class Message(JsonDeserializable):
self.invoice = None self.invoice = None
self.successful_payment = None self.successful_payment = None
self.connected_website = None self.connected_website = None
self.reply_markup = None
for key in options: for key in options:
setattr(self, key, options[key]) setattr(self, key, options[key])
self.json = json_string self.json = json_string
@ -937,10 +940,18 @@ class KeyboardButtonPollType(Dictionaryable):
return {'type': self.type} return {'type': self.type}
class InlineKeyboardMarkup(Dictionaryable, JsonSerializable): class InlineKeyboardMarkup(Dictionaryable, JsonSerializable, JsonDeserializable):
max_row_keys = 8 max_row_keys = 8
def __init__(self, row_width=3): @classmethod
def de_json(cls, json_string):
if (json_string is None):
return None
obj = cls.check_json(json_string)
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):
""" """
This object represents an inline keyboard that appears This object represents an inline keyboard that appears
right next to the message it belongs to. right next to the message it belongs to.
@ -953,7 +964,7 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
row_width = self.max_row_keys row_width = self.max_row_keys
self.row_width = row_width self.row_width = row_width
self.keyboard = [] self.keyboard = keyboard
def add(self, *args, row_width=None): def add(self, *args, row_width=None):
""" """
@ -979,7 +990,7 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
row_width = self.max_row_keys row_width = self.max_row_keys
for row in util.chunks(args, row_width): 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) self.keyboard.append(button_array)
return self return self
@ -1009,17 +1020,29 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
return json.dumps(self.to_dict()) return json.dumps(self.to_dict())
def to_dict(self): def to_dict(self):
json_dict = {'inline_keyboard': self.keyboard} json_dict = dict()
json_dict['inline_keyboard'] = [[button.to_dict() for button in row] for row in self.keyboard]
return json_dict 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): def __init__(self, url, forward_text=None, bot_username=None, request_write_access=None):
self.url = url self.url = url
self.forward_text = forward_text self.forward_text = forward_text
self.bot_username = bot_username self.bot_username = bot_username
self.request_write_access = request_write_access 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): def to_json(self):
return json.dumps(self.to_dict()) return json.dumps(self.to_dict())
@ -1034,7 +1057,7 @@ class LoginUrl(Dictionaryable, JsonSerializable):
return json_dict 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, 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): switch_inline_query_current_chat=None, callback_game=None, pay=None, login_url=None):
self.text = text self.text = text
@ -1046,6 +1069,21 @@ class InlineKeyboardButton(Dictionaryable, JsonSerializable):
self.pay = pay self.pay = pay
self.login_url = login_url 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): def to_json(self):
return json.dumps(self.to_dict()) return json.dumps(self.to_dict())

View File

@ -17,6 +17,28 @@ def test_json_message():
assert msg.text == 'HIHI' 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(): 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"}}' 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) msg = types.Message.de_json(jsonstring)