G
G
Gawain2020-06-11 10:29:41
Python
Gawain, 2020-06-11 10:29:41

How to send a voice message with a VK bot?

This is how I try to send a voice message on behalf of the bot

id = str(messages["items"][0]["last_message"]["from_id"])
upload = vk.method("docs.getMessagesUploadServer", {"type": "audio_message", "peer_id": id})
jsn = requests.post(upload["upload_url"], files={'file': open(path + f'_{i}.ogg', 'rb')}).json()
save = vk.method("docs.save", {"file": jsn["file"]})
send = 'doc{}_{}'.format(save["audio_message"]["owner_id"], save["audio_message"]["id"])
vk.method('messages.send',
                          {"peer_id": int(id),
                           "attachment": send, "random_id": random.randint(0, 2147483647)})

At the stage of calling the docs.save method, I get a dictionary describing the audio message, and the "duration" parameter in it is equal to zero. And as a result, the bot also sends a voice message with a length of 0 seconds. Tell me, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Tanashchuk, 2020-06-12
@Xaeruz

Try using the following:

audio = open('lol.mp3','rb')
a = session_api.docs.getMessagesUploadServer(type='audio_message')
b = requests.post(a['upload_url'], files={'file':audio}).json()
c = session_api.docs.save(file=b['file'])
doc = 'doc%s_%s_%s'%(c['audio_message']['owner_id'],c['audio_message']['id'],c['audio_message']['access_key'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question