Advertisement
Guest User

Untitled

a guest
Jul 4th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <?php
  2.  
  3. if (!isset($_REQUEST)) {
  4. return;
  5. }
  6.  
  7. //Строка для подтверждения адреса сервера из настроек Callback API
  8. $confirmation_token = '123';
  9.  
  10. //Ключ доступа сообщества
  11. $token = '123';
  12.  
  13. //Получаем и декодируем уведомление
  14. $data = json_decode(file_get_contents('php://input'));
  15. //Проверяем, что находится в поле "type"
  16. switch ($data->type) {
  17. //Если это уведомление для подтверждения адреса...
  18. case 'confirmation':
  19. //...отправляем строку для подтверждения
  20. echo $confirmation_token;
  21. break;
  22.  
  23. //Если это уведомление о новом сообщении...
  24. case 'message_new':
  25. $datas = array(
  26. 'action' => 'pay-to-group',
  27. 'amount' => '1',
  28. 'group_id' => '183981859'
  29. );
  30. $buttons = [
  31. 'one_time' => false,
  32. 'buttons' =>
  33. array (
  34. array (
  35. array (
  36. 'action' =>
  37. array (
  38. 'type' => 'vkpay',
  39. 'hash' => http_build_query($datas),
  40. ),
  41. ),
  42. ),
  43. ),
  44. ];
  45. $buttons = json_encode($buttons);
  46. //...получаем id его автора
  47. $user_id = $data->object->from_id;
  48. //затем с помощью users.get получаем данные об авторе
  49. $user_info = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$user_id}&access_token={$token}&v=5.0"));
  50.  
  51. //и извлекаем из ответа его имя
  52. $user_name = $user_info->response[0]->first_name;
  53. $random_id = mt_rand(1, 99999999);
  54. //С помощью messages.send отправляем ответное сообщение
  55. $request_params = array(
  56. 'message' => "Hello, {$user_name}!",
  57. 'user_id' => $user_id,
  58. 'access_token' => $token,
  59. 'v' => '5.95',
  60. 'group_id' => '183981859',
  61. 'random_id' => $random_id,
  62. 'keyboard' => $buttons
  63.  
  64. );
  65.  
  66. $get_params = http_build_query($request_params);
  67.  
  68. var_dump(file_get_contents('https://api.vk.com/method/messages.send?'. $get_params));
  69.  
  70. //Возвращаем "ok" серверу Callback API
  71.  
  72. echo('ok');
  73.  
  74. break;
  75.  
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement