· 5 years ago · Jun 15, 2020, 01:08 PM
1<?php
2
3const TOKEN = "0ba3fb3dd0738527bbded8a15dff1db607e610b4d1ac36b278e90e7ffb8a698c83b5a723828dcd5562729";
4const API = 5.107;
5const KEY = "4d781a00";
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 switch(mb_strtolower($msg)){ //зависимость от регистра кмд убрана
17 case "start":
18 msgSend("Олла, друк! \n меня ещё пишут", $peer_id);
19 break;
20 case "города":
21 msgSend("Список городов: \n -Denver \n - Futurama", $peer_id);
22 break;
23 case "фото":
24 msgSend("Photo", $peer_id, upload("path/to/photo.png", $peer_id));
25 break;
26 }
27 break;
28 }
29}
30
31function call($method, $params = [])
32{
33 $curl = curl_init('https://api.vk.com/method/' . $method . '?access_token=' . TOKEN . '&v=' . API);
34 curl_setopt_array($curl, [
35 CURLOPT_POST => true,
36 CURLOPT_POSTFIELDS => $params,
37 CURLOPT_RETURNTRANSFER => true
38 ]);
39 $response = json_decode(curl_exec($curl), true);
40 curl_close($curl);
41 return $response['response'];
42}
43
44function msgSend($text, $peer_id, $attachments = []){
45 if(!is_array($attachments)) $attachments = [$attachments];
46 return call('messages.send',
47 [
48 'random_id' => 0,
49 'peer_id' => $peer_id,
50 'message' => mb_substr($text, 0, 4096),
51 'attachment' => implode(',', $attachments),
52 'disable_mentions' => 1,
53 ]);
54}
55
56function upload($file, $peer_id)
57 {
58 $server = json_decode(file_get_contents('https://api.vk.com/method/photos.getMessagesUploadServer?v=' . API . '&access_token=' . TOKEN . '&peer_id=' . $peer_id), true)['response']['upload_url'];
59 $ch = curl_init($server);
60 curl_setopt_array($ch, [
61 CURLOPT_RETURNTRANSFER => true,
62 CURLOPT_POST => true,
63 CURLOPT_POSTFIELDS => [
64 'photo' => new CURLfile(realpath($file))
65 ]
66 ]);
67 $data = json_decode(curl_exec($ch), true);
68 curl_close($ch);
69 $d1 = json_decode(file_get_contents('https://api.vk.com/method/photos.saveMessagesPhoto?v=' . API . '&access_token=' . TOKEN . '&server=' . $data['server'] . '&hash=' . $data['hash'] . '&photo=' . $data['photo']), true);
70 return "photo" . $d1['response'][0]['owner_id'] . "_" . $d1['response'][0]['id'];
71 }