· 5 years ago · Apr 16, 2020, 08:16 AM
1
2
3<?php
4
5
6
7if (!isset($_REQUEST)) {
8return;
9}
10
11
12
13
14function message($peer_id_, $message_, $token_, $idpost_, $keyboard_)
15{
16if($keyboard_ != '0' && $idpost_ =='0') {
17 $request_params = array(
18 'message' => "{$message_}",
19 'peer_id' => $peer_id_,
20 'access_token' => $token_,
21 'v' => '5.101',
22 'random_id' => '0',
23 'keyboard' => json_encode($keyboard_)
24 );
25
26}
27elseif($keyboard_ == '0' && $idpost_ !='0'){
28 $request_params = array(
29 'message' => "{$message_}",
30 'peer_id' => $peer_id_,
31 'access_token' => $token_,
32 'v' => '5.101',
33 'random_id' => '0',
34 'attachment' => "{$idpost_}"
35 );
36
37}
38elseif($keyboard_ != '0' && $idpost_ !='0'){
39 $request_params = array(
40 'message' => "{$message_}",
41 'peer_id' => $peer_id_,
42 'access_token' => $token_,
43 'v' => '5.101',
44 'random_id' => '0',
45 'keyboard' => json_encode($keyboard_),
46 'attachment' => "{$idpost_}"
47 );
48}
49else{
50 $request_params = array(
51 'message' => "{$message_}",
52 'peer_id' => $peer_id_,
53 'access_token' => $token_,
54 'v' => '5.101',
55 'random_id' => '0'
56 );
57
58}
59 $get_params = http_build_query($request_params);
60 file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
61}
62//переменные....................................................................................
63
64
65$host = 'localhost';
66$user = 'id10398630_posts';
67$password = '753698qaz';
68$db_name = 'id10398630_posts'; //имя базы данных
69 //Соединяемся с базой данных используя наши доступы:
70$link = mysqli_connect($host, $user, $password, $db_name);
71
72$confirmation_token = 'a6cfb8d7';
73//$tocken3 = '3a39b1c2638a0a6f6612a05436361a4a07b9e1134a68348ae01cec7235e601e33401b86c52846ac26dbe2';
74//$token1 = '606bb0edb011677ae8bc6dc7f4750a89778a88b3d50cda389c132e02ba5435626c3f5d650442ef8444ec7';
75//$token2 = '3ab4d82e0977910594f8897c81c84c32db3fc1a39093ac475d2fb01c6eec8c248ec0feb03d45122895bed';
76$token = '606bb0edb011677ae8bc6dc7f4750a89778a88b3d50cda389c132e02ba5435626c3f5d650442ef8444ec7';
77 $secretKey = 'ghjuky76t5rfghjuy76tr5dfgvhjuy7t6r5dfgvhj'
78$keyboard1 = [
79 'one_time' => false,
80 'buttons' => [
81 [
82 [
83 'action' =>
84 [
85 'type' => 'text',
86 'payload' => '{"button": "1"}',
87 'label' => 'Товары',
88 ],
89 'color' => 'primary',
90 ],
91 [
92 'action' =>
93 [
94 'type' => 'text',
95 'payload' => '{"button": "2"}',
96 'label' => 'Донатик',
97 ],
98 'color' => 'primary',
99 ],
100 ],
101 [
102 [
103 'action' =>
104 [
105 'type' => 'text',
106 'payload' => '{"button": "3"}',
107 'label' => 'Стримы',
108 ],
109 'color' => 'primary',
110 ],
111 [
112 'action' =>
113 [
114 'type' => 'text',
115 'payload' => '{"button": "4"}',
116 'label' => 'Подп. на рассылку',
117 ],
118 'color' => 'positive',
119 ],
120 ],
121 [
122 [
123 'action' =>
124 [
125 'type' => 'text',
126 'payload' => '{"button": "5"}',
127 'label' => 'Скрыть клавиауру',
128 ],
129 'color' => 'secondary',
130 ],
131 ],
132 ],
133];
134
135$keyboard2 = [
136 'one_time' => false,
137 'buttons' => [
138 [
139 [
140 'action' =>
141 [
142 'type' => 'text',
143 'payload' => '{"button": "1"}',
144 'label' => 'Товары',
145 ],
146 'color' => 'primary',
147 ],
148 [
149 'action' =>
150 [
151 'type' => 'text',
152 'payload' => '{"button": "2"}',
153 'label' => 'Донатик',
154 ],
155 'color' => 'primary',
156 ],
157 ],
158 [
159 [
160 'action' =>
161 [
162 'type' => 'text',
163 'payload' => '{"button": "3"}',
164 'label' => 'Стримы',
165 ],
166 'color' => 'primary',
167 ],
168 [
169 'action' =>
170 [
171 'type' => 'text',
172 'payload' => '{"button": "4"}',
173 'label' => 'Отп. от рассылки',
174 ],
175 'color' => 'negative',
176 ],
177 ],
178 [
179 [
180 'action' =>
181 [
182 'type' => 'text',
183 'payload' => '{"button": "5"}',
184 'label' => 'Скрыть клавиауру',
185 ],
186 'color' => 'secondary',
187 ],
188 ],
189 ],
190];
191$keyboardoff = [
192 'one_time' => true,
193 'buttons' => [],
194];
195//..................................................................................................
196
197
198
199
200//Получаем и декодируем уведомление
201$data = json_decode(file_get_contents('php://input'));
202
203
204if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0)
205 return;
206//Проверяем, что находится в поле "type"
207switch ($data->type) {
208//Если это уведомление для подтверждения адреса...
209case 'confirmation':
210//...отправляем строку для подтверждения
211echo $confirmation_token;
212break;
213
214
215case 'message_new':
216
217//----------------отслеживание времени--------------------------------------------------------------
218$time_start = microtime(true);
219//-----------------------------------------------------------------------------------------------------
220
221$peer_id = $data->object->peer_id;
222$text = $data->object->text;
223echo('ok');
224
225if($text == "/команды" || $text == "Начать") {
226$sql = "SELECT peer_id FROM `stream`";
227if (!$link) {
228 $err = 'Не могу соединиться с БД. Код ошибки: ' . mysqli_connect_errno() . ', ошибка: ' . mysqli_connect_error();
229 message("16212252", $err, $token, '0', '0');
230 exit;
231 }
232
233 $result = mysqli_query($link, $sql);
234 if($result)
235{
236 while ($row = mysqli_fetch_array($result))
237 {
238 $ids[] = $row[0];
239 }
240}
241if (in_array($peer_id, $ids)){
242 message($peer_id, 'Приветствую тебя, котенок. Я -- информационный бот, который может тебе помочь!
243 Вот список моих команд :
244 💸"Донатики"
245 💲"Товары"
246 📺"Стримы"
247 📝"Отп. от рассылки"
248 🔧;"/инфо"', $token, '0', $keyboard2);
249}
250else{
251 message($peer_id, 'Приветствую тебя, котенок. Я -- информационный бот, который может тебе помочь!
252 Вот список моих команд:
253 💸"Донатики"
254 💲"Товары"
255 📺"Стримы"
256 📝"Подп. на рассылку"
257 🔧;"/инфо"', $token, '0', $keyboard1);
258}
259 // message($peer_id, "{$ids[0]}/{$ids[1]}", $token, '0', '0');
260}
261elseif($text == "Скрыть клавиауру"){
262 message($peer_id, 'Убираю. теперь ты можешь написать админу, он ответит в ближайшее время. чтобы снова вызвать меню, напиши: "/команды", а чтобы узнать информацию о боте, напиши: "/инфо".', $token, '0', $keyboardoff);
263}
264elseif($text == "Товары"){
265message($peer_id, "продуктики: https://vk.com/market-185587678", $token, 'market-185587678_3289034,market-185587678_3288976', '0');
266}
267elseif($text == "Донатик"){
268 message($peer_id, "https://vk.com/sladka9_bulochka?w=app6471849_-185587678 /// https://vk.com/sladka9_bulochka?w=app5727453_-185587678", $token, '0', '0');
269}
270elseif($text == "Стримы"){
271message($peer_id, "https://www.twitch.tv/hut_of_stories", $token, '0', '0');
272}
273elseif($text == "Отп. от рассылки"){
274 $sql = "SELECT peer_id FROM `stream`";
275 if (!$link) {
276 $err = 'Не могу соединиться с БД. Код ошибки: ' . mysqli_connect_errno() . ', ошибка: ' . mysqli_connect_error();
277 message("16212252", $err, $token, '0', '0');
278 exit;
279 }
280
281 $result = mysqli_query($link, $sql);
282 if($result)
283{
284 while ($row = mysqli_fetch_array($result))
285 {
286 $ids[] = $row[0];
287 }
288}
289if (in_array($peer_id, $ids)){
290 $sql = "DELETE FROM `stream` WHERE `stream`.`peer_id` = {$peer_id}";
291 mysqli_query($link, $sql);
292 message($peer_id, "подписка успешно отключена", $token, '0', $keyboard1);
293}
294else{
295 message($peer_id, "Так вы и не подписаны", $token, '0', $keyboard1);
296}}
297elseif($text == "Подп. на рассылку"){
298 $sql = "SELECT peer_id FROM `stream`";
299 if (!$link) {
300 $err = 'Не могу соединиться с БД. Код ошибки: ' . mysqli_connect_errno() . ', ошибка: ' . mysqli_connect_error();
301 message("16212252", $err, $token, '0', '0');
302 exit;
303 }
304
305 $result = mysqli_query($link, $sql);
306 if($result)
307{
308 while ($row = mysqli_fetch_array($result))
309 {
310 $ids[] = $row[0];
311 }
312}
313if (in_array($peer_id, $ids)){
314 message($peer_id, "У вас уже оформленна подписка на новости", $token, '0', $keyboard2);
315}
316else{
317 $sql = "INSERT INTO `stream` (`id`, `peer_id`, `date`) VALUES (NULL, {$peer_id}, current_timestamp())";
318 mysqli_query($link, $sql);
319 message($peer_id, "подписка успешно оформленна", $token, '0', $keyboard2);
320}}
321elseif($text == "/инфо"){
322message($peer_id, 'Привет, я -- информационный бот для паблика [club185587678|Наша ламповая хижина рассказов]. Создал меня мой [id16212252|Отец]. Версия ядра:2.8.3', $token, 'photo-184775255_457239042', '0');
323}
324elseif($text[0] == "/" && $text[1] == "r" && $text[2] == "s"){
325if($peer_id == '16212252' || $peer_id == '388409722'){
326//----------------отслеживание времени--------------------------------------------------------------
327$time_start = microtime(true);
328//-----------------------------------------------------------------------------------------------------
329 message ($peer_id, "Отправка сообщения...", $token, "0", "0");
330 $att = $data->object->attachments;
331 for($i = 0;$i < count($att);$i++){
332 $type[$i] = $att[$i]->type;
333 $id[$i] = $att[$i]->{$type[$i]}->id;
334 if($type[$i] == "wall"){
335 $own[$i] = $att[$i]->{$type[$i]}->to_id;
336 }
337 else{
338 $own[$i] = $att[$i]->{$type[$i]}->owner_id;
339 }
340 $tkn[$i] = $att[$i]->{$type[$i]}->access_key;
341 if($tkn != ""){
342 $attn[$i] = "{$type[$i]}{$own[$i]}_{$id[$i]}_{$tkn[$i]}";
343 }
344 else{
345 $attn[$i] = "{$type[$i]}{$own[$i]}_{$id[$i]}";
346 }
347 }
348
349for ($i = 4; $i <= strlen($text)-1; $i++) {
350 $ntxt[] = $text[$i];
351}
352$sql = "SELECT peer_id FROM `stream`";
353if (!$link) {
354 $err = 'Не могу соединиться с БД. Код ошибки: ' . mysqli_connect_errno() . ', ошибка: ' . mysqli_connect_error();
355 message("16212252", $err, $token, '0', '0');
356 exit;
357 }
358
359 $result = mysqli_query($link, $sql);
360 if($result)
361{
362 while ($row = mysqli_fetch_array($result))
363 {
364 $ids[] = $row[0];
365 }
366}
367for ($i = 0; $i <= count($ids) - 1; $i++) {
368 message($ids[$i], implode("", $ntxt), $token, implode(",", $attn), '0');
369}
370message($peer_id, "Сообщение отправленно", $token, '0', '0');
371
372
373//----------------отслеживание времени--------------------------------------------------------------
374$time_end = microtime(true);
375 $time = $time_end - $time_start;
376 message($peer_id, $time, $token, '0', '0');
377//-----------------------------------------------------------------------------------------------------
378}
379
380//message($peer_id, implode(",", $attn), $token, '0', '0');
381}
382elseif($text == "/count"){
383if($peer_id == '16212252' || $peer_id == '388409722'){
384$sql = "SELECT peer_id FROM `stream`";
385if (!$link) {
386 $err = 'Не могу соединиться с БД. Код ошибки: ' . mysqli_connect_errno() . ', ошибка: ' . mysqli_connect_error();
387 message("16212252", $err, $token, '0', '0');
388 exit;
389 }
390
391 $result = mysqli_query($link, $sql);
392 if($result)
393{
394 while ($row = mysqli_fetch_array($result))
395 {
396 $ids[] = $row[0];
397 }
398}
399 message($peer_id, count($ids), $token, '0', '0');
400}
401}
402if($peer_id == '16212252' || $peer_id == '388409722'){
403//----------------отслеживание времени--------------------------------------------------------------
404$time_end = microtime(true);
405 $time = $time_end - $time_start;
406 message($peer_id, $time, $token, '0', '0');
407//-----------------------------------------------------------------------------------------------------
408}
409break;
410case 'message_reply':
411echo('ok');
412break;
413
414
415default:
416echo 'ok';
417break;
418}
419?>