S
S
Shakir Yakubov2020-06-13 22:19:55
PHP
Shakir Yakubov, 2020-06-13 22:19:55

Why random_id in vk api refuses to work normally?

Greetings friends, initially the problem was that the bot sends one message several times, the reason for this was the random_id parameter, which should be unique each time, and I had 'random_id' => 0. Now that the problem is found, I'm trying to generate random_id, but for some reason the bot still sends multiple messages. There is no problem with the uniqueness of $random_id, since I output it through var_damp and see that each time a new number. The strange thing is that if I manually set the number to some value, for example, 1, 2, 3, etc. then everything works, and it sends 1 time, only after that it needs to be changed every time, but again, if you do it manually, then everything works, so why doesn’t it work with normal generation. I am attaching the code below:

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

function send($id, $msg, $token, $version, $random_id){
    $url = 'https://api.vk.com/method/messages.send';
    $params = array(
        'user_id' => $id,
        'message' => $msg,
        'access_token' => $token,
        'v' => $version,
        'random_id' => $random_id
    );
    
    $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, $random_id);
var_dump($random_id); 
         echo 'OK';

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Nedzvetsky, 2020-06-16
@Nezd

the bot sends one message several times, the reason for this was the random_id parameter, which must be unique each time, and I had 'random_id' => 0.

No, the bot sent the message multiple times because the `messages.send` method was called multiple times
I'm trying to generate a random_id, but for some reason the bot still sends a few messages.

Because the problem is not solved several times in a row, `messages.send` is called
. If your bot works through the Callback API, then you need to check that you always answer OK in response, now in yours when there is `var_dump($random_id);` which also breaks this and Callback API will send requests again.
You can see if there are retries in the Callback API at Community Management > Working with API

A
Anton Morozov, 2020-06-13
@fanofmarta

Try reducing the number of nines to something like: I've come across this, I can't professionally describe why, but it works. In any case, try to remove the nine, until you succeed. The problem seems to be that the rand function does not support numeric types with large values.
$random_id = rand(1,99999999);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question