📜 ⬆️ ⬇️

Sending voice messages VKontakte using VK API

Everyone who worked with the VK API , has long known that access to any work with audio records on VKontakte was closed on December 16, 2016, and information about voice messages is generally not in the documentation.

image Using my empty sandbox community as an example

So how is this done?

Use hidden options to load the document.


First, you need to pay attention to one very important point: in order to send a voice message on behalf of a group or a public page, you will have to download it with a user token anyway, but you can attach this document later when sending to communities, with their token.
')
As for a regular document, we get the address of the server to load:

https://api.vk.com/method/docs.getUploadServer?access_token=ACCESS_TOKEN&type=audio_message&v=5.63 

The main point here is the parameter type = audio_message .

In response, we should get the following JSON :

 { "response": { "upload_url":"https://..." } } 

How to upload a file to VK server correctly


If you send a file that is not in multipart / form-data format, it will not work.
Downloading audio in mp3 format also fails, it is best to use ogg , although you can experiment.

You can use the code from here to download the file in the desired format (an example is specified in Java , analogs for yourself, I think, can be found on the Internet):

We use an instance of the class MultipartUtility , nothing needs to be changed in it:

 StringBuilder response_sb = new StringBuilder(); try { MultipartUtility multipart = new MultipartUtility("___", "UTF-8"); multipart.addFilePart("_____"); List<String> response = multipart.finish(); for (String line : response) { response_sb.append(line); } } catch (IOException e) { e.printStackTrace(); } 

Everything, audio message is loaded. The response from the server in case of luck will be similar to this:

 { "file":"62802565|0|0|805131||ogg|9943|file.ogg||||||=" } 

We save the document on the server


It is also important to note here: if you save the document not from the user, then when sending it it will look like a document, and not as a voice message. Or you can send just a blank message.

We make the following request:

 https://api.vk.com/method/docs.save?file=__file&access_token=ACCESS_TOKEN&v=5.63 

This was the last stage. We get the answer:

 { "response": [ { "id": 000000000, "owner_id": 000000000 ...    ,      } ] } 

That's all. You can send messages in the usual way, in attachments by specifying the doc (owner_id) _ (id) link, using owner_id and id , obtained above.

PS Normal user can not send a message containing something other than voice recording. And through the API this is done very easily. Previously, it worked in the comments / discussions and so on, but now, apparently, the shop was closed, as was the loading ( but not sending! ) Of voice messages by communities.



I am not the author of the "investigation" to find a way to send audio messages through the API, I just collected all the information in a bunch and tried to make out beautifully. I noticed on the Internet a lot of questions about this.

This article was written for those who worked with the VKontakte API, and I did not describe uninteresting things, trying to write only on business. If I make up my mind, I will write a couple more articles about how to write a bot in Java using the VK LongPoll server (for personal pages) and using the Callback API and web servlets (for communities).

For the provided materials and assistance thanks to Stanislav Kudelko.

Source: https://habr.com/ru/post/327280/


All Articles