The translation was generated automatically and may contain mistakes
Sending Messages
Chatbot communicates with users using community Messages . You can send text and attachments in messages: photos, audio recordings or videos, documents, audio messages, links. Messages are sent via requests to API VKontakte .
Method messages.send
A method is used to send a message messages.send .
In the response, the method will return to your server the ID of the message sent or the error. If you use the Callback API, an event will be generated if you send it successfully message_reply .
Features of use
- •
C access to the Community You can make up to 20 requests per second.
To reduce the number of API requests, group messages with the same text into a parameter
messageдля разны?1?… получателей. Для этого перечислите идентификаторы получателей (до 100) в параметреuser_ids.<!--Группируйте вызовы методов API с помощью [`execute`](method/execute) — до 25 вызовов в одном запросе. execute планируется выпиливать--> - •
- •
Use the parameter
random_id(int32) to avoid sending the message back to the same recipient. This parameter should always be unique, so use large random numbers. - •
Using the method
messages.sendThe bot can forward messages or respond to messages within the chat. To do this, pass a JSON object in the parameterforward.
Example in PHP
define('VK_API_VERSION', '5.131'); // Используемая версия API
define('VK_API_ENDPOINT', "https://api.vk.ru/method/");
// Функция для вызова произвольного метода API
function _vkApi_call($method, $params = array()) {
$params['v'] = VK_API_VERSION;
$url = VK_API_ENDPOINT.$method.'?'.http_build_query($params);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// VK_API_ACCESS_TOKEN передаём в заголовке Authorization
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.VK_API_ACCESS_TOKEN
));
$json = curl_exec($curl);
curl_close($curl);
$response = json_decode($json, true);
return $response['response'];
}
// Функция для вызова messages.send
function vkApi_messagesSend($peer_id, $message, $attachments = array()) {
return _vkApi_call('messages.send', array(
'peer_id' => $peer_id,
'message' => $message,
'attachment' => implode(',', $attachments)
));
}
vkApi_messagesSend(494075, 'Hello world!');Materials on the topic
- •
- •
- •
- •
- •