· 5 years ago · Jun 14, 2020, 01:12 PM
1<?php
2
3const TOKEN = "token";
4const API = 5.107;
5const KEY = "key";
6
7$data = json_decode(file_get_contents('php://input'), true);
8
9if($data['type'] == "confirmation") echo KEY; else {
10 switch ($data['type']){
11 case 'message_new':
12 $data = $data['object']['message'];
13 $peer_id = $data['peer_id'];
14 $user_id = $data['from_id'];
15 $msg = $data['text'];
16 if($msg == "!бот") msgSend("Привет!", $peer_id);
17 break;
18 }
19}
20
21function call($method, $params = [])
22{
23 $curl = curl_init('https://api.vk.com/method/' . $method . '?access_token=' . TOKEN . '&v=' . API);
24 curl_setopt_array($curl, [
25 CURLOPT_POST => true,
26 CURLOPT_POSTFIELDS => $params,
27 CURLOPT_RETURNTRANSFER => true
28 ]);
29 $response = json_decode(curl_exec($curl), true);
30 curl_close($curl);
31 return $response['response'];
32}
33
34function msgSend($text, $peer_id){
35 return call('messages.send',
36 [
37 'random_id' => 0,
38 'peer_id' => $peer_id,
39 'message' => mb_substr($text, 0, 4096),
40 'disable_mentions' => 1,
41 ]);
42}