S
S
Shakir Yakubov2020-06-12 16:42:41
PHP
Shakir Yakubov, 2020-06-12 16:42:41

Vk bot api. message.send method. Why is the bot sending many messages instead of just one?

There is a VKontakte bot that should send messages to users. The bot sends them continuously, up to 200 messages, even with the condition that after the send function, "ok" is returned to the server. I am attaching the code:

$user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$user_id}&access_token={$token}&v=5.107"));

// Извлекаем из ответа его имя
$user_name = $user_info->response[0]->first_name;

// Отправляемое сообщение
$message = 'Здравствуйте, ' . $user_name . '! Вы запросили привязку данного аккаунта к аккунту на ресурсе SUDRUS. Ваш код подтверждения ' . $code . '. Введите его на сайте, чтобы подтвердить этот метод отправки уведомлений.';

function send($id, $msg, $token, $version){
    $url = 'https://api.vk.com/method/messages.send';
    $params = array(
        'user_id' => $id,
        'message' => $msg,
        'access_token' => $token,
        'v' => $version,
        'random_id' => '0'
    );
    
    $result = file_get_contents($url, false, stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        )
    )));
}

send($user_id, $message, $token, $api_version);
         echo 'OK';

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergei Chamkin, 2020-06-12
@Shakirchinskiy

random_id should not be 0, it's just a random number that you generate yourself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question