-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramNotify.php
More file actions
159 lines (134 loc) · 4.59 KB
/
Copy pathTelegramNotify.php
File metadata and controls
159 lines (134 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use GuzzleHttp\Client as HttpClient;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Exception\Exception;
use PHPCensor\Common\Plugin\Plugin;
/**
* TelegramNotify Plugin.
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author LEXASOFT <lexasoft83@gmail.com>
*/
class TelegramNotify extends Plugin
{
private string $authToken = '';
private string $message;
private string $buildMsg;
private array $recipients = [];
private bool $sendLog = false;
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'telegram_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$message = $this->buildMessage();
$client = new HttpClient();
$url = '/bot' . $this->authToken . '/sendMessage';
foreach ($this->recipients as $chatId) {
$params = [
'chat_id' => $chatId,
'text' => $message,
'parse_mode' => 'Markdown',
];
$client->post(('https://api.telegram.org' . $url), [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => $params,
]);
if ($this->sendLog) {
$params = [
'chat_id' => $chatId,
'text' => $this->buildMsg,
'parse_mode' => 'Markdown',
];
$client->post(('https://api.telegram.org' . $url), [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => $params,
]);
}
}
return true;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
if (!$this->options->get('auth_token')) {
throw new Exception("Not setting telegram 'auth_token'");
}
if (!$this->options->get('recipients')) {
throw new Exception("Not setting telegram 'recipients'");
}
$this->authToken = $this->variableInterpolator->interpolate((string)$this->options->get('auth_token', $this->authToken));
$this->message = '[%ICON_BUILD%] [%PROJECT_TITLE%](%PROJECT_LINK%)' .
' - [Build #%BUILD_ID%](%BUILD_LINK%) has finished ' .
'for commit [%SHORT_COMMIT_ID% (%COMMITTER_EMAIL%)](%COMMIT_LINK%) ' .
'on branch [%BRANCH%](%BRANCH_LINK%)';
$this->message = (string)$this->options->get('message', $this->message);
$this->sendLog = (bool)$this->options->get('send_log', $this->sendLog);
$recipients = $this->options->get('recipients', []);
if ($recipients) {
if (\is_string($recipients)) {
$this->recipients = [$recipients];
} elseif (\is_array($recipients)) {
$this->recipients = $recipients;
}
}
}
/**
* Build message.
*/
private function buildMessage(): string
{
$this->buildMsg = '';
$buildIcon = $this->build->isSuccessful() ? '✅' : '❌';
$buildLog = $this->build->getLog();
$buildLog = \str_replace(['[0;32m', '[0;31m', '[0m', '/[0m'], '', (string)$buildLog);
$buildMessages = \explode('RUNNING PLUGIN: ', $buildLog);
foreach ($buildMessages as $bm) {
$pos = (int)\mb_strpos($bm, "\n");
$firstRow = \mb_substr($bm, 0, $pos);
//skip long outputs
if (\in_array($firstRow, ['slack_notify', 'php_loc', 'telegram_notify'], true)) {
continue;
}
$this->buildMsg .= '*RUNNING PLUGIN: ' . $firstRow . "*\n";
$this->buildMsg .= $firstRow === 'composer' ? '' : ('```' . \mb_substr($bm, $pos) . '```');
}
return $this->variableInterpolator->interpolate(
\str_replace(['%ICON_BUILD%'], [$buildIcon], $this->message)
);
}
}