K
K
Kirill2020-04-16 23:42:57
Python
Kirill, 2020-04-16 23:42:57

How to get text from fwd_messages in vk_api?

The person forwards a message from another conversation to the bot. You need to get the content of the message, its 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
longpoll = VkBotLongPoll(vk_session, group_id=194081345)
vk = vk_session.get_api() 

while True:
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW and event.from_user:
            fwd_messages = [{'date': 1587065635, 'from_id': 574078662, 'text': 'Мда..Вк на приложении сдохло', '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': 'Мда..Вк на приложении сдохло', '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'}
            '''

It is not possible to get the fwd_messages array so to speak. Its json path is event.object.message.*should be fwd_messages* here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Grigoriev, 2020-04-17
@kustiktm_kirill

You need to read the basics of 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': 'Мда..Вк на приложении сдохло', '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(t['object']['message']['fwd_messages'])
# Можно не ловя ошибок
print(t.get('object', {}).get('message', {}).get('fwd_messages'))


# Можно обернуть в функцию и скармливать строку
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 wrap all this in a class. but I'm too lazy to do an example here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question