Skip to content

Commit f2c6855

Browse files
authored
Merge pull request #79 from prog-time/issues-77
Issues 77
2 parents 142860c + 1547aed commit f2c6855

File tree

10 files changed

+2552
-729
lines changed

10 files changed

+2552
-729
lines changed

app/Http/Controllers/SimplePage.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ class SimplePage
99
/**
1010
* @return View
1111
*/
12-
public function index()
12+
public function index(): View
1313
{
1414
if (config('app.url') === 'https://tg-support-bot.ru') {
1515
return view('site.home');
1616
} else {
1717
return view('site.home_client_version');
1818
}
1919
}
20+
21+
public function liveChatPromo(): View
22+
{
23+
return view('site.live_chat_promo');
24+
}
2025
}

app/Http/Controllers/VkBotController.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,51 @@
88
use App\Services\VK\VkEditService;
99
use App\Services\VK\VkMessageService;
1010
use Illuminate\Http\Request;
11+
use Illuminate\Http\Response;
12+
use Illuminate\Support\Facades\Cache;
1113

1214
class VkBotController
1315
{
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
1922
{
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);
2325
}
2426

2527
$dataHook = VkUpdateDto::fromRequest($request);
26-
$this->dataHook = !empty($dataHook) ? $dataHook : die('ok');
28+
if (empty($dataHook)) {
29+
return response('ok', 200);
30+
}
2731

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);
3037

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);
4144
}
4245

43-
switch ($this->dataHook->type) {
46+
switch ($dataHook->type) {
4447
case 'message_new':
45-
(new VkMessageService($this->dataHook))->handleUpdate();
48+
(new VkMessageService($dataHook))->handleUpdate();
4649
break;
4750

4851
case 'message_edit':
49-
(new VkEditService($this->dataHook))->handleUpdate();
52+
(new VkEditService($dataHook))->handleUpdate();
5053
break;
5154
}
55+
56+
return response('ok', 200);
5257
}
5358
}

app/Jobs/SendMessage/AbstractSendMessageJob.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ protected function telegramResponseHandler(TelegramAnswerDto $response): void
7575
if ($response->response_code === 429) {
7676
$retryAfter = $response->parameters->retry_after ?? 3;
7777
(new LokiLogger())->log('warning', "429 Too Many Requests. Replay {$retryAfter}");
78-
sleep(10);
7978
$this->release($retryAfter);
8079
return;
8180
}

app/Services/VK/VkMessageService.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function handleUpdate(): void
4646
$this->sendLocation();
4747
}
4848

49-
echo 'ok';
5049
} catch (\Throwable $e) {
5150
(new LokiLogger())->logException($e);
5251
}

docker/nginx/default.conf.template

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ server {
4444

4545
# Socket.IO
4646
location /socket.io/ {
47+
# CORS headers
48+
add_header 'Access-Control-Allow-Origin' $http_origin always;
49+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
50+
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always;
51+
add_header 'Access-Control-Allow-Credentials' 'true' always;
52+
53+
if ($request_method = OPTIONS) {
54+
add_header 'Access-Control-Allow-Origin' $http_origin;
55+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
56+
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
57+
add_header 'Access-Control-Allow-Credentials' 'true';
58+
add_header 'Access-Control-Max-Age' 86400;
59+
return 204;
60+
}
61+
4762
proxy_pass http://node_server:3000;
4863
proxy_http_version 1.1;
4964
proxy_set_header Upgrade $http_upgrade;

0 commit comments

Comments
 (0)