M
M
mrisid2019-08-21 08:53:18
PHP
mrisid, 2019-08-21 08:53:18

Bot vk on php sends greetings 2 times. How to fix?

There is a code in which if a person enters hello or Hello, then the bot will answer him - Hello!
The trouble is that sometimes the bot sends 2 messages Hello!
Maybe the problem lies in the hosting? (I have it free)
Here is the code itself ->

<?php


$confirmationToken = 'Тут что должен ответить сервер VK';

$token = 'Тут ключ API';


$secretKey = 'Тут секретный ключ';

$data = json_decode(file_get_contents('php://input'));


if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0)
    return;


switch ($data->type) {
    case 'confirmation':
        echo $confirmationToken;
        break;
case 'message_new': 
    $user_id = $data->object->user_id; 
    $user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$user_id}&v=5.0")); 
    $user_name = $user_info->response[0]->first_name; 
    $message = $data->object->body; 
    $messages_array = [ 
        'Привет' => "Привет!", 
    ]; 
    foreach($messages_array as $k => $v){ 
        if(strtolower($message) == strtolower($k)){$otwet = $v;}
} 
$request_params = [ 
'message' => $otwet, 
'user_id' => $user_id, 
'access_token' => $token, 
'v' => '5.0' 
]; 
$get_params = http_build_query($request_params); 
file_get_contents('https://api.vk.com/method/messages.send?'. $get_params); 
echo('ok'); 
break; 
return false;

}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-08-21
@vshvydky

most likely, the event model sends you your own message, check the flag that you only respond to incoming messages, if I remember correctly, then after you call the api with the message send, you receive it in events and probably do not correctly process what you receive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question