|
8 | 8 | use App\Services\VK\VkEditService; |
9 | 9 | use App\Services\VK\VkMessageService; |
10 | 10 | use Illuminate\Http\Request; |
| 11 | +use Illuminate\Http\Response; |
| 12 | +use Illuminate\Support\Facades\Cache; |
11 | 13 |
|
12 | 14 | class VkBotController |
13 | 15 | { |
14 | | - private VkUpdateDto $dataHook; |
15 | | - |
16 | | - private ?BotUser $botUser; |
17 | | - |
18 | | - public function __construct(Request $request) |
| 16 | + /** |
| 17 | + * @return Response |
| 18 | + * |
| 19 | + * @throws \Exception |
| 20 | + */ |
| 21 | + public function bot_query(Request $request): Response |
19 | 22 | { |
20 | | - if (request()->type === 'confirmation') { |
21 | | - echo config('traffic_source.settings.vk.confirm_code'); |
22 | | - die(); |
| 23 | + if ($request->type === 'confirmation') { |
| 24 | + return response(config('traffic_source.settings.vk.confirm_code'), 200); |
23 | 25 | } |
24 | 26 |
|
25 | 27 | $dataHook = VkUpdateDto::fromRequest($request); |
26 | | - $this->dataHook = !empty($dataHook) ? $dataHook : die('ok'); |
| 28 | + if (empty($dataHook)) { |
| 29 | + return response('ok', 200); |
| 30 | + } |
27 | 31 |
|
28 | | - $this->botUser = (new BotUser())->getUserByChatId($this->dataHook->from_id, 'vk'); |
29 | | - } |
| 32 | + $cacheKey = 'vk_event_' . $dataHook->event_id; |
| 33 | + if (Cache::has($cacheKey)) { |
| 34 | + return response('ok', 200); |
| 35 | + } |
| 36 | + Cache::put($cacheKey, true, 600); |
30 | 37 |
|
31 | | - /** |
32 | | - * @return void |
33 | | - * |
34 | | - * @throws \Exception |
35 | | - */ |
36 | | - public function bot_query(): void |
37 | | - { |
38 | | - if ($this->botUser->isBanned()) { |
39 | | - (new SendBannedMessageVk())->execute($this->botUser); |
40 | | - die('ok'); |
| 38 | + $botUser = (new BotUser())->getUserByChatId($dataHook->from_id, 'vk'); |
| 39 | + |
| 40 | + if ($botUser->isBanned()) { |
| 41 | + (new SendBannedMessageVk())->execute($botUser); |
| 42 | + |
| 43 | + return response('ok', 200); |
41 | 44 | } |
42 | 45 |
|
43 | | - switch ($this->dataHook->type) { |
| 46 | + switch ($dataHook->type) { |
44 | 47 | case 'message_new': |
45 | | - (new VkMessageService($this->dataHook))->handleUpdate(); |
| 48 | + (new VkMessageService($dataHook))->handleUpdate(); |
46 | 49 | break; |
47 | 50 |
|
48 | 51 | case 'message_edit': |
49 | | - (new VkEditService($this->dataHook))->handleUpdate(); |
| 52 | + (new VkEditService($dataHook))->handleUpdate(); |
50 | 53 | break; |
51 | 54 | } |
| 55 | + |
| 56 | + return response('ok', 200); |
52 | 57 | } |
53 | 58 | } |
0 commit comments