Skip to content

Commit 7f587b7

Browse files
committed
feat: send notification
1 parent ae982f5 commit 7f587b7

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

src/Http/Actions/IndexAction.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
4+
5+
use CSlant\TelegramGitNotifier\Bot;
6+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
7+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
8+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
9+
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
10+
use CSlant\TelegramGitNotifier\Notifier;
11+
use CSlant\LaravelTelegramGitNotifier\Services\NotificationService;
12+
use GuzzleHttp\Client;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Telegram;
15+
16+
class IndexAction
17+
{
18+
protected Client $client;
19+
20+
protected Bot $bot;
21+
22+
protected Notifier $notifier;
23+
24+
protected Request $request;
25+
26+
/**
27+
* @throws ConfigFileException
28+
*/
29+
public function __construct()
30+
{
31+
$this->client = new Client();
32+
33+
$telegram = new Telegram(config('telegram-git-notifier.bot.token'));
34+
$this->bot = new Bot($telegram);
35+
$this->notifier = new Notifier();
36+
}
37+
38+
/**
39+
* Handle telegram git notifier app
40+
*
41+
* @return void
42+
* @throws InvalidViewTemplateException
43+
* @throws MessageIsEmptyException
44+
* @throws SendNotificationException
45+
*/
46+
public function __invoke(): void
47+
{
48+
$sendNotification = new NotificationService($this->notifier,
49+
$this->bot->setting);
50+
$sendNotification->handle();
51+
}
52+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
6+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
7+
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
8+
use CSlant\TelegramGitNotifier\Models\Setting;
9+
use CSlant\TelegramGitNotifier\Notifier;
10+
use CSlant\TelegramGitNotifier\Objects\Validator;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
class NotificationService
14+
{
15+
protected Request $request;
16+
17+
protected array $chatIds = [];
18+
19+
protected Notifier $notifier;
20+
21+
protected Setting $setting;
22+
23+
public function __construct(
24+
Notifier $notifier,
25+
Setting $setting,
26+
) {
27+
$this->request = Request::createFromGlobals();
28+
$this->notifier = $notifier;
29+
$this->chatIds = $this->notifier->parseNotifyChatIds();
30+
31+
$this->setting = $setting;
32+
}
33+
34+
/**
35+
* Handle to send notification from webhook event to telegram
36+
*
37+
* @return void
38+
* @throws InvalidViewTemplateException
39+
* @throws SendNotificationException
40+
* @throws MessageIsEmptyException
41+
*/
42+
public function handle(): void
43+
{
44+
$eventName = $this->notifier->handleEventFromRequest($this->request);
45+
if (!empty($eventName)) {
46+
$this->sendNotification($eventName);
47+
}
48+
}
49+
50+
/**
51+
* @param string $event
52+
*
53+
* @return void
54+
* @throws InvalidViewTemplateException
55+
* @throws SendNotificationException
56+
* @throws MessageIsEmptyException
57+
*/
58+
private function sendNotification(string $event): void
59+
{
60+
if (!$this->validateAccessEvent($event)) {
61+
return;
62+
}
63+
64+
foreach ($this->chatIds as $chatId => $thread) {
65+
if (empty($chatId)) {
66+
continue;
67+
}
68+
69+
if (empty($thread)) {
70+
$this->notifier->sendNotify(null, ['chat_id' => $chatId]);
71+
72+
continue;
73+
}
74+
75+
foreach ($thread as $threadId) {
76+
$this->notifier->sendNotify(null, [
77+
'chat_id' => $chatId, 'message_thread_id' => $threadId,
78+
]);
79+
}
80+
}
81+
}
82+
83+
/**
84+
* Validate access event
85+
*
86+
* @param string $event
87+
*
88+
* @return bool
89+
* @throws InvalidViewTemplateException|MessageIsEmptyException
90+
*/
91+
private function validateAccessEvent(string $event): bool
92+
{
93+
$payload = $this->notifier->setPayload($this->request, $event);
94+
$validator = new Validator($this->setting, $this->notifier->event);
95+
96+
if (empty($payload)
97+
|| !$validator->isAccessEvent(
98+
$this->notifier->event->platform,
99+
$event,
100+
$payload
101+
)
102+
) {
103+
return false;
104+
}
105+
106+
return true;
107+
}
108+
}

0 commit comments

Comments
 (0)