How to get the text from fwd_messages in vk_api?

The person sends a message from another dialogue bot. You need to get the contents of the message, the text.
Now there is such a code.
import vk_api

vk_session = vk_api.VkApi(token='xxx')

from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
add = VkBotLongPoll(vk_session, group_id=194081345)
vk = vk_session.get_api() 

while True:
 for event in add.listen():
 if event.type == VkBotEventType.MESSAGE_NEW and event.from_user:
 fwd_messages = [{'date': 1587065635, 'from_id': 574078662, 'text': 'Hmm..VK on the app is dead', 'attachments': [], 'conversation_message_id': 1887}], 'conversation_message_id': 328, 'peer_id': 396298943, 'id': 844}], 'important': False, 'random_id': 0, 'attachments': [], 'is_hidden': False}, 'client_info': {'button_actions': ['text', 'vkpay', 'open_app', 'location', 'open_link'], 'keyboard': True, 'inline_keyboard': True, 'lang_id': 0}}, 'group_id': 194081345, 'event_id': '919d7461a9d3217cb9f1b9eb50c7b5299571140d'}]
print(event.object.message)
 s = fwd_messages[8]

"'
{'type': 'message_new', 'object': { 'message': 
 {'date': 1587065982, 'from_id': 396298943, 'id': 845, 'out': 0, 'peer_id': 396298943, 'text': ", 'conversation_message_id': 329, 'fwd_messages': 
 [{'date': 1587065973, 'from_id': 396298943, 'text': ", 'attachments': [], 'fwd_messages':
 [{'date': 1587065635, 'from_id': 574078662, 'text': 'Hmm..VK on the app is dead', 'attachments': [], 'conversation_message_id': 1887}], 'conversation_message_id': 328, 'peer_id': 396298943, 'id': 844}], 'important': False, 'random_id': 0, 'attachments': [], 'is_hidden': False}, 'client_info': {'button_actions': ['text', 'vkpay', 'open_app', 'location', 'open_link'], 'keyboard': True, 'inline_keyboard': True, 'lang_id': 0}}, 'group_id': 194081345, 'event_id': '919d7461a9d3217cb9f1b9eb50c7b5299571140d'}
 "'

Does not work so to speak to get the array fwd_messages. His path for json - event.object.message.*there must be fwd_messages*.
April 19th 20 at 12:25
1 answer
April 19th 20 at 12:27
Solution
You need to read the basics in Python
t = {
 'type': 'message_new',
 'object': {
 'message': {
 'date': 1587065982,
 'from_id': 396298943,
 'id': 845, 'out': 0,
 'peer_id': 396298943,
 'text': ",
 'conversation_message_id': 329,
'fwd_messages':
 [{'date': 1587065973, 'from_id': 396298943, 'text': ",
 'attachments': [], 'fwd_messages':
 [{'date': 1587065635, 'from_id': 574078662,
 'text': 'Hmm..VK on the app is dead', 'attachments': [],
 'conversation_message_id': 1887}],
 'conversation_message_id': 328, 'peer_id': 396298943, 'id': 844}],
 'important': False, 'random_id': 0, 'attachments': [], 'is_hidden': False},
 'client_info': {
 'button_actions': ['text', 'vkpay', 'open_app', 'location', 'open_link'],
 'keyboard': True, 'inline_keyboard': True, 'lang_id': 0}},
 'group_id': 194081345, 'event_id': '919d7461a9d3217cb9f1b9eb50c7b5299571140d'}

# You can just
print(t['object']['message']['fwd_messages'])
# Can you not catching errors
print(t.get('object', {}).get('message', {}).get('fwd_messages'))


# You can wrap in a function and feed line
def get_key(obj: dict, path):
 for k in path.split('.'):
 obj = obj.get(k, {})

 return obj


print(get_key(t, 'object.message.fwd_messages'))

Still it is possible to class all of this was wrapped. but I'm too lazy to do an example

Find more questions by tags BotsVKontaktePython