Advertisement
Guest User

vk_chat

a guest
Sep 14th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. //vk_chat.php
  2.  
  3. else if ($matches[1] === 'key') {
  4.                 $colors = ['positive','negative','primary','secondary'];
  5.                 $buttons = [];
  6.                 foreach (SERVERS as $key => $server){
  7.                     $buttons[] = $vk->button('!' . $key, $colors[array_rand($colors)]);
  8.                 }
  9.                 $buttons = array_chunk($buttons, 4, true);//делим на таблицу. если убрать эту строчку, то кнопки будут идти друг за другом (вертикально)
  10.                 $keyboard = [
  11.                     'one_time' => true,
  12.                     'buttons' => $buttons,
  13.                 ];
  14.                 $vk->put('keyboard', $keyboard);
  15.                 $vk->keyboard($peerid, 'Тестируем клавиатуру', $keyboard);
  16.             }
  17.  
  18.  
  19.  
  20.  
  21. // vk_class.php
  22.  
  23. //Посылает клавиатуру
  24.     public function keyboard($peerid, $str, $keyboard){
  25.         $params = [
  26.             'random_id' => rand(0, 32767),
  27.             'message' => $str,
  28.             'keyboard' => json_encode($keyboard, JSON_UNESCAPED_UNICODE),
  29.             'access_token' => VK_TOKEN,
  30.             'v' => VK_VERSION,
  31.         ];
  32.        
  33.         if ($this->is_personal($peerid)) $params['user_id'] = $peerid;
  34.         else $params['chat_id'] = $peerid -= 2000000000;
  35.  
  36.         $get_params = http_build_query($params);
  37.         $this->put('xd', 'https://api.vk.com/method/messages.send?' . $get_params);
  38.         file_get_contents('https://api.vk.com/method/messages.send?' . $get_params);
  39.     }
  40.    
  41.     //Создаёт кнопку. Цвета: primary (синий), secondary (белый), negative (красный), positive (зелёный)
  42.     //Подробнее тут https://vk.com/dev/bots_docs_3?f=4.3.+%D0%9E%D1%82%D0%BF%D1%80%D0%B0%D0%B2%D0%BA%D0%B0+%D0%BA%D0%BB%D0%B0%D0%B2%D0%B8%D0%B0%D1%82%D1%83%D1%80%D1%8B
  43.     public function button($text, $color){
  44.         return ['action' => ['type' => 'text', 'payload' => '{"button": "1"}', 'label' => $text], 'color' => $color];
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement