1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\TelegramGitNotifier\Structures; |
4
|
|
|
|
5
|
|
|
use CSlant\TelegramGitNotifier\Constants\EventConstant; |
6
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; |
7
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
trait Notification |
12
|
|
|
{ |
13
|
|
|
public object $payload; |
14
|
|
|
|
15
|
|
|
public string $message = ''; |
16
|
|
|
|
17
|
|
|
public function accessDenied( |
18
|
|
|
string $chatId = null, |
19
|
|
|
string $viewTemplate = null, |
20
|
|
|
): void { |
21
|
|
|
$this->telegram->sendMessage([ |
22
|
|
|
'chat_id' => $this->chatBotId, |
23
|
|
|
'text' => tgn_view( |
24
|
|
|
$viewTemplate ?? config('telegram-git-notifier.view.globals.access_denied'), |
25
|
|
|
['chatId' => $chatId] |
26
|
|
|
), |
27
|
|
|
'disable_web_page_preview' => true, |
28
|
|
|
'parse_mode' => 'HTML', |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function setPayload(Request $request, string $event) |
33
|
|
|
{ |
34
|
|
|
$content = null; |
35
|
|
|
|
36
|
|
|
if ($this->event->platform === 'gitlab') { |
37
|
|
|
$content = $request->getContent(); |
38
|
|
|
} elseif ($this->event->platform === EventConstant::DEFAULT_PLATFORM) { |
39
|
|
|
$content = $request->request->get('payload'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (is_string($content)) { |
43
|
|
|
$this->payload = json_decode($content); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->setMessage($event); |
47
|
|
|
|
48
|
|
|
return $this->payload; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set message from payload |
53
|
|
|
* |
54
|
|
|
* @param string $typeEvent |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
* @throws MessageIsEmptyException |
58
|
|
|
*/ |
59
|
|
|
private function setMessage(string $typeEvent): void |
60
|
|
|
{ |
61
|
|
|
$event = tgn_event_name($typeEvent); |
62
|
|
|
$action = $this->getActionOfEvent($this->payload); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$viewTemplate = empty($action) |
65
|
|
|
? "events.{$this->event->platform}.{$event}.default" |
66
|
|
|
: "events.{$this->event->platform}.{$event}.{$action}"; |
67
|
|
|
|
68
|
|
|
$viewResult = tgn_view($viewTemplate, [ |
69
|
|
|
'payload' => $this->payload, |
70
|
|
|
'event' => tgn_convert_event_name($typeEvent), |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
if ($viewResult === null) { |
74
|
|
|
throw MessageIsEmptyException::create(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->message = $viewResult; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function sendNotify(string $message = null, array $options = []): bool |
81
|
|
|
{ |
82
|
|
|
$this->message = !empty($message) ? $message : $this->message; |
83
|
|
|
|
84
|
|
|
if (trim($this->message) === config('telegram-git-notifier.view.ignore-message')) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$queryParams = array_merge($this->createTelegramBaseContent(), ['text' => $this->message], $options); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$url = 'https://api.telegram.org/bot' . config('telegram-git-notifier.bot.token') . '/sendMessage'; |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
$options = [ |
94
|
|
|
'form_params' => $queryParams, |
95
|
|
|
'verify' => config('telegram-git-notifier.app.request_verify'), |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$response = $this->client->request('POST', $url, $options); |
99
|
|
|
|
100
|
|
|
if ($response->getStatusCode() === 200) { |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$body = (string) $response->getBody(); |
105
|
|
|
|
106
|
|
|
throw SendNotificationException::create($body); |
107
|
|
|
} catch (GuzzleException $e) { |
108
|
|
|
throw SendNotificationException::create($e->getMessage()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|