Answer the question
In order to leave comments, you need to log in
API problem?
Krch, I wrote a bot in VK that responds to a message, but the problem is that it can respond to one message several times, how can I fix it? Code below
if (!isset($_REQUEST))
{
return;
}
$data = json_decode(file_get_contents('php://input'));
switch ($data->type)
{
case 'confirmation':
echo $cal_string;
header("HTTP/1.1 200 OK");
break;
case'message_new':
$text = $data->object->text;
$user_id = $data->object->from_id;
$user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$user_id}&access_token={$key}&v=5.101"));
$user_name = $user_info->response[0]->first_name;
$params = array(
'message' => "Привет, $user_name!",
'access_token' => $key,
'user_id' => $user_id,
'v' => '5.101',
'random_id' => rand()
);
$get_params = http_build_query($params);
file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
echo 'ok';
header("HTTP/1.1 200");
break;
Answer the question
In order to leave comments, you need to log in
Extra:
if (!isset($_REQUEST))
{
return;
}
<?php
$data = json_decode(file_get_contents('php://input'), true);
switch ($data['type'])
{
case 'confirmation':
{
echo $cal_string;
break;
}
case'message_new':
{
echo 'ok';
fastcgi_finish_request();
$text = $data['object']['text'];
$user_id = $data['object']['from_id'];
$user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$user_id}&access_token={$key}&v=5.101"));
$user_name = $user_info['response'][0]['first_name'];
$params = [
'message' => urlencode("Привет, $user_name!"),
'access_token' => $key,
'user_id' => $user_id,
'v' => '5.101',
];
$get_params = http_build_query($params);
file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
break;
}
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question