A
A
abbaturSF2019-03-09 15:05:18
Python
abbaturSF, 2019-03-09 15:05:18

How to get information about VK media attachment?

I don't know how to get media attachment information from this code. If in the case of text, it’s enough for me to turn to the event.text variable, then what should I do if, for example, I sent a photo or audio to the bot. How can I get information about this message?

import requests
import vk_api
import requests
vk_session = vk_api.VkApi(token='мой токен')
from vk_api.longpoll import VkLongPoll, VkEventType
longpoll = VkLongPoll(vk_session, mode=234)
vk = vk_session.get_api()
for event in longpoll.listen():
  if event.type == VkEventType.MESSAGE_NEW and event.to_me:
    if event.text == '50':
      info = vk.photos.getMessagesUploadServer()
      proba = requests.post(info['upload_url'], files={'photo': open('C:\Project\VkBot1\photos.jpg', 'rb')}).json()
      c = vk.photos.saveMessagesPhoto(photo=proba['photo'], server=proba['server'], hash=proba['hash'])[0]
      d = "photo{}_{}".format(c['owner_id'], c['id'])
      vk.messages.send(
        user_id=event.user_id,
        random_id=event.random_id,
        message='Рандомное сообщение',
        attachment=d)
      print(event)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stockholm Syndrome, 2019-03-09
@StockholmSyndrome

the documentation has a detailed description of the message object:
https://vk.com/dev/objects/message
you need the attachments field:
https://vk.com/dev/objects/attachments_m

V
Vitaly Ogai, 2019-03-10
@ogai22

It is enough not to use the vk_api module. Good replacement: vk.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question