Callback API is a tool for tracking user activity in your VKontakte community. With it, you can implement new useful features, for example:
- A bot to send instant replies to incoming messages.
- Automatic content moderation system.
- Service for collecting and processing indicators of audience involvement.
To start using the Callback API , connect your server in the community settings and select the types of events you want to receive information about (for example, new comments and new photos). When the event of the selected type occurs in the community, VK will send a request in JSON format to your server with basic information about the object that triggered the event (for example, an added comment). You no longer need to make regular requests to the API to track updates - now you will receive them instantly.
The secret key you specified will be transmitted with each notification from the server in a separate secret field. This will allow you to reliably determine that the notification came precisely from our server.
$ confirmationToken $ token $ secretKey
004eec27
<?php if (!isset($_REQUEST)) { return; } // Callback API $confirmationToken = '004eec27'; // $token = ' '; // Secret key $secretKey = 'testSecureKey'; // $data = json_decode(file_get_contents('php://input')); // secretKey if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; //, "type" switch ($data->type) { // ... case 'confirmation': //... echo $confirmationToken; break; // ... case 'message_new': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, !<br>". " .", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; // case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; } ?>
// case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break;
<?php if (!isset($_REQUEST)) { return; } // Callback API $confirmationToken = '004eec27'; // $token = ' '; // Secret key $secretKey = 'testSecureKey'; // $data = json_decode(file_get_contents('php://input')); // secretKey if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; //, "type" switch ($data->type) { // ... case 'confirmation': //... echo $confirmationToken; break; // ... case 'message_new': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, !<br>". " .", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; // case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; } ?>
// case 'group_leave': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, 😔<br>" . " .<br>" . " - <br>" . " - https://vk.com/kulakovkostya", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break;
<?php if (!isset($_REQUEST)) { return; } // Callback API $confirmationToken = '004eec27'; // $token = ' '; // Secret key $secretKey = 'testSecureKey'; // $data = json_decode(file_get_contents('php://input')); // secretKey if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; //, "type" switch ($data->type) { // ... case 'confirmation': //... echo $confirmationToken; break; // ... case 'message_new': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, !<br>". " .", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; // case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; // case 'group_leave': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, 😔<br>" . " .<br>" . " - <br>" . " - https://vk.com/kulakovkostya", 'user_id' => $userId, '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); // "ok" Callback API echo('ok'); break; } ?>
Source: https://habr.com/ru/post/329150/
All Articles