Answer the question
In order to leave comments, you need to log in
The VK community bot does not work - there is no response from the server. What to do?
I set up the bot according to the official instructions of VK. The server has successfully confirmed that incoming messages are being logged but cannot get the username and send the message. Set up logging of incoming and outgoing requests with saving to the database. According to them, you can track that there is no answer from the VK server. Tell me what's the problem?
Bot cat:
<?php
if (!isset($_REQUEST)) { return; }
$test = true;
//Подключение к БД
$hostname = "localhost";
$username = "пользователь";
$password = "пароль";
$database = "бд";
$table_log_query = "таблица";
//== Подключенние к БД ==
$dbh = new PDO("mysql:host=$hostname; dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
function log_query($dir, $data) {
global $table_log_query, $dbh;
$stmt = $dbh->prepare("INSERT INTO $table_log_query (dir, time, json) VALUES (:pdir, :ptime, :pjson)");
$stmt ->bindparam(":pdir", $dir);
$stmt ->bindparam(":ptime", time());
$stmt ->bindparam(":pjson", serialize($data));
$stmt->execute();
}
//Строка для подтверждения адреса сервера из настроек Callback API
$api_v = '5.92';
$confirmation_token = 'токенизнастроек';
$token = 'токенсообщества';
$secretKey = 'секретный ключ';
//Получаем и сохраняем уведомление в БД
$data = json_decode(file_get_contents('php://input'));
if ($test) log_query("in",$data);
switch ($data->type) {
case 'confirmation':
echo $confirmation_token;
break;
case 'message_new':
$peer_id = $data->object->peer_id;
//Получаем текст сообщения
$message_origin = $data->object->text;
$message = mb_strtolower($message_origin);
//Имя автора
$user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$peer_id}&access_token={$token}&v={$api_v}"));
$user_name = $user_info->response[0]->first_name;
if ($test) log_query("usr",$user_info);
$message = $message . ' ' . $user_name;
//Ответы
$request_params = [
'user_id' => $peer_id,
'random_id' => round(time() / 43),
'message' => $message,
'access_token' => $token,
'v' => $api_v
];
//Получаем и сохраняем уведомление в БД
$get_params = http_build_query($request_params);
$error = json_decode(file_get_contents('https://api.vk.com/method/messages.send?'. $get_params));
if ($test) {
log_query("out",$get_params);
log_query("err",$error);
}
echo('ok');
break;
return false;
}
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question