S
S
slavamironoff2019-07-16 11:55:50
Python
slavamironoff, 2019-07-16 11:55:50

How to get a link to download attachments?

Hello world ladies and gentlemen.
I have a task to write a bot for photo processing.
There was a problem with which I have been fighting for about a week, how to get src from attachments
The story is this: The
user sends a picture to the bot in the LAN (bot group), he recognizes the attachment, if it is a picture, he must download it, process it and return the processed version to the user.
Here is the code:

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import json

TOKEN = 'тут токен'

token = vk_api.VkApi(token=TOKEN, scope=['message', 'groups', 'wall'])
longpoll = VkLongPoll(token)
vk_sess = token.get_api()

''' Авторизация по токену, назначение прав доступа к сообщениям, группе, стене. Запуск longpoll '''

for event in longpoll.listen():
    ''' Прослушка longpoll '''
    if event: # Если есть сообщение
        if event.type == VkEventType.MESSAGE_NEW and event.to_me:
            ''' Если есть новое сообщение, адресованное в ЛС группы '''
            if event.from_user: # Если пишет пользователь
                vk_sess.messages.send( 
                    user_id=event.user_id,
                    message="Обработка сообщения..."
                )
                if event.attachments:
                    print("Получено медиа вложение\n Тип => " + event.attachments['attach1_type'])
                    if event.attachments['attach1_type'] == 'photo':
                        vk_sess.messages.send( 
                            user_id=event.user_id,
                            message="Фотография обрабатывается"
                        )
                        vk_sess.messages.send( 
                            user_id=event.user_id,
                            message="Секундочку...."
                        )
                        vk_sess.messages.send( 
                            user_id=event.user_id,
                            message="Фотография обработана.",
                            attachments=event.attachments
                        )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Taus, 2019-07-16
@slavamironoff

Because the documentation contradicts itself in point 3.1 and point 6 . Assuming item 6 is correct, how do you use it in

print("Получено медиа вложение\n Тип => " + event.attachments['attach1_type'])

then the object event.attachmentshas an attach1 field that can be used in the photos.getById method :
photo_id = event.attachments['attach1']
photo_info = vk_sess.photos.get_by_id(photos=photo_id)
photo_urls = {size['type']: size['url'] for size in photo_info['sizes']}

In photo_urls links according to photo_sizes .
The above method does not allow getting a photo_id with a private access_key, without which it will not be possible to call photos.getById. So:
Then you get information on event.message_id using the message.getById method, which should contain the attachments field. Description of the field structure https://vk.com/dev/objects/attachments_m and for photo https://vk.com/dev/objects/photo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question