N
N
Ninzalo2022-02-22 19:02:20
Python
Ninzalo, 2022-02-22 19:02:20

How to send a document in a VK message via vk_api?

Hello,
I have a function that forwards messages (something like message mirroring) - a message comes to the bot > the bot sends this message back.

If the message contains attachments, it processes them and forwards the message along with all attachments. But for some reason, if the attachment = doc, then the attachments are not forwarded (photo/audio/video attachments are perfectly processed)
Quote from the vk_api page

Media attachments to a private message, separated by commas. Each attachment is in the format: {type}{owner_id}_{media_id}

attachments = []
if event.object.message['attachments'] != []:
    attachments = event.object.message['attachments'] 
sender(id=target['user_id'], text=text, attachments=attachments)


def sender(id, text=None, keyboard=None, attachments=None):
    post = {
        'user_id': id,
        'random_id': 0
    }
    if text is not None:
        post['message'] = text
    if keyboard is not None:
        post["keyboard"] = keyboard.get_keyboard()
    if attachments is not None and attachments != []:
        post['attachment'] = ''
        for attachment in attachments:
            type_of_attachment = attachment['type']
            attachment_owner_id = attachment[f'{type_of_attachment}']['owner_id']
            attachment_id = attachment[f'{type_of_attachment}']['id']
            try:
                attachment_access_key = attachment[f'{type_of_attachment}']['access_key']
                post['attachment'] += (f'{type_of_attachment}{attachment_owner_id}_{attachment_id}_{attachment_access_key},')
            except:
                post['attachment'] += (f'{type_of_attachment}{attachment_owner_id}_{attachment_id},')
    vk_session.method('messages.send', post)


Is it possible to just send the document, and not save this document + upload it to the VK server on your own behalf + send it?

UPD:
The only option so far is to send a link to the document instead of the document, but I don't really like how it is displayed in the message
Code example:
attachments = []
if event.object.message['attachments'] != []:
    attachments = event.object.message['attachments']
sender(id=target['user_id'], text=text, attachments=attachments)


def sender(id, text=None, keyboard=None, attachments=None):
    post = {
        'user_id': id,
        'random_id': 0
    }
    if text is not None:
        post['message'] = text
    if keyboard is not None:
        post["keyboard"] = keyboard.get_keyboard()
   if attachments is not None and attachments != []:
        post['attachment'] = ''
        for attachment in attachments:
            type_of_attachment = attachment['type']
            if type_of_attachment == 'doc':
                post['attachment'] += f'{attachment[f"{type_of_attachment}"]["url"]}'
            else:
                attachment_owner_id = attachment[f'{type_of_attachment}']['owner_id']
                attachment_id = attachment[f'{type_of_attachment}']['id']
                try:
                    attachment_access_key = attachment[f'{type_of_attachment}']['access_key']
                    post['attachment'] += f'{type_of_attachment}{attachment_owner_id}_{attachment_id}_{attachment_access_key},'
                except:
                    post['attachment'] += f'{type_of_attachment}{attachment_owner_id}_{attachment_id},'
        print(post['attachment'])
    vk_session.method('messages.send', post)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question