-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
113 lines (83 loc) · 2.53 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
require_once './vendor/autoload.php';
const VK_TOKEN = 'ae51ff9f59f0c40f029c67c3588466bcec1f7ff0f333404769952a31290c3cf097f5b7c0cfd90b0da4562';
use VK\Client\Enums\VKLanguage;
use VK\Client\VKApiClient;
function myLog($str) {
file_put_contents("php://stdout", "$str\n");
}
const COLOR_NEGATIVE = 'negative';
const COLOR_POSITIVE = 'positive';
const COLOR_DEFAULT = 'default';
const COLOR_PRIMARY = 'primary';
const CMD_ID = 'ID';
const CMD_NEXT = 'NEXT';
const CMD_TYPING = 'TYPING';
function getBtn($label, $color, $payload = '') {
return [
'action' => [
'type' => 'text',
"payload" => json_encode($payload, JSON_UNESCAPED_UNICODE),
'label' => $label
],
'color' => $color
];
}
$json = file_get_contents('php://input');
//myLog($json);
$data = json_decode($json, true);
$type = $data['type'] ?? '';
$vk = new VKApiClient('5.78', VKLanguage::RUSSIAN);
if ($type === 'message_new') {
$message = $data['object'] ?? [];
$userId = $message['user_id'] ?? $message['peer_id'];
$body = $message['body'] ?? '';
$payload = $message['payload'] ?? '';
if ($payload) {
$payload = json_decode($payload, true);
}
myLog("MSG: ".$body." PAYLOAD:".$payload);
$kbd = [
'one_time' => false,
'buttons' => [
[getBtn("Покажи мой ID", COLOR_DEFAULT, CMD_ID)],
[getBtn("Далее", COLOR_PRIMARY, CMD_NEXT)],
]
];
$msg = "Привет я бот!";
if ($payload === CMD_ID) {
$msg = "Ваш id ".$userId;
}
if ($payload === CMD_NEXT) {
$kbd = [
'one_time' => false,
'buttons' => [
[getBtn("Пошли тайпинг", COLOR_POSITIVE, CMD_TYPING)],
[getBtn("Назад", COLOR_NEGATIVE)],
]
];
}
if ($payload === CMD_TYPING) {
try {
$res = $vk->messages()->setActivity(VK_TOKEN, [
'peer_id' => $userId,
'type' => 'typing'
]);
$msg = null;
} catch (\Exception $e) {
myLog( $e->getCode().' '.$e->getMessage() );
}
}
try {
if ($msg !== null) {
$response = $vk->messages()->send(VK_TOKEN, [
'peer_id' => $userId,
'message' => $msg,
'keyboard' => json_encode($kbd, JSON_UNESCAPED_UNICODE)
]);
}
} catch (\Exception $e) {
myLog( $e->getCode().' '.$e->getMessage() );
}
}
echo "OK";