The translation was generated automatically and may contain mistakes
Message attachments
Parameter attachment
Method messages.send takes a parameter attachment . To send an image, audio, or any other attachment, specify its type and ID in the format: идентификатор_владельца + _ + идентификатор_объекта . For example: photo-123456_654231 .
You can use public objects that have already been uploaded VKontakte for example, send a photo from your community wall or a video from a search or download a new attachment.
Uploading a photo
There are several ways to upload images.
To send the same image to many interlocutors at once, for example, for mailing, use one of the following methods:
- •Pre-upload the image to a public album of the same community and use it in
attachmentID of this photo. - •Upload the image with
photos.getMessagesUploadServerWithout Instructionpeer_idand then follow instructions . The image will be uploaded to the band's hidden album.
To upload and send an image to a specific user, use a method with a recipient ID, and then follow instructions .
Please note:
- •After loading
owner_idThe image will be equal to the transmittedpeer_id. This is the right behavior. There is no limit for such uploads: you can upload photos in this way for an unlimited number of users. - •Each time the order in
attachmentphotos are not from the community, the image is re-uploaded on behalf of the community. For such downloads, there is a limit - you should count on no more than 7 thousand downloads per day.
Example in PHP
function vkApi_photosGetMessagesUploadServer($peer_id) {
return _vkApi_call('photos.getMessagesUploadServer', array(
'peer_id' => $peer_id,
));
}
function vkApi_photosSaveMessagesPhoto($photo, $server, $hash) {
return _vkApi_call('photos.saveMessagesPhoto', array(
'photo' => $photo,
'server' => $server,
'hash' => $hash,
));
}
function vkApi_upload($url, $file_name) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => new CURLfile($file_name)));
$json = curl_exec($curl);
curl_close($curl);
return json_decode($json, true);
}
// Загрузка фотографии в сообщение от бота
function uploadPhoto($user_id, $file_name) {
$upload_server_response = vkApi_photosGetMessagesUploadServer($user_id);
$upload_response = vkApi_upload($upload_server_response['upload_url'], $file_name);
$save_response = vkApi_photosSaveMessagesPhoto($upload_response['photo'], $upload_response['server'], $upload_response['hash']);
return array_pop($save_response);
}
$photo = uploadPhoto(494075, 'cats.jpg');Example of Function _vkApi_call described in section Sending Messages .
Loading the document
To download a new document to submit, use the method docs.getMessagesUploadServer with the receiver ID in the parameter peer_id and then follow instructions .
Please note that after loading owner_id The document shall be equal to the peer_id . This is necessary so that your bot can download new documents for an unlimited number of users.
Example in PHP
function vkApi_docsGetMessagesUploadServer($peer_id, $type) {
return _vkApi_call('docs.getMessagesUploadServer', array(
'peer_id' => $peer_id,
'type' => $type,
));
}
function vkApi_docsSave($file, $title) {
return _vkApi_call('docs.save', array(
'file' => $file,
'title' => $title,
));
}
// Загрузка голосового сообщения от бота
function uploadVoiceMessage($user_id, $file_name) {
$upload_server_response = vkApi_docsGetMessagesUploadServer($user_id, 'audio_message');
$upload_response = vkApi_upload($upload_server_response['upload_url'], $file_name);
$save_response = vkApi_docsSave($upload_response['file'], 'Voice message');
return array_pop($save_response);
}
$voice_message = uploadVoiceMessage(494075, 'voice_msg.ogg');Example of Function _vkApi_call described in section Sending Messages .
Materials on the topic
- •
- •
- •
- •
- •