-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathTelegramNotify.php
More file actions
172 lines (144 loc) · 4.93 KB
/
Copy pathTelegramNotify.php
File metadata and controls
172 lines (144 loc) · 4.93 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
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace PHPCensor\Plugin;
use Exception;
use GuzzleHttp\Client;
use PHPCensor\Builder;
use PHPCensor\Common\Exception\InvalidArgumentException;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Telegram Plugin
*
* @package PHP Censor
* @subpackage Application
*
* @author LEXASOFT <lexasoft83@gmail.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class TelegramNotify extends Plugin
{
protected $authToken;
protected $message;
protected $buildMsg;
protected $recipients;
protected $sendLog;
/**
* @return string
*/
public static function pluginName()
{
return 'telegram_notify';
}
/**
* Standard Constructor
*
* @throws Exception
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (empty($options['auth_token'])) {
throw new InvalidArgumentException("Not setting telegram 'auth_token'");
}
if (empty($options['recipients'])) {
throw new InvalidArgumentException("Not setting recipients");
}
if (\array_key_exists('auth_token', $options)) {
$this->authToken = $this->builder->interpolate($options['auth_token'], true);
}
$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%)';
if (isset($options['message'])) {
$this->message = $options['message'];
}
$this->recipients = [];
if (\is_string($options['recipients'])) {
$this->recipients = [$options['recipients']];
} elseif (\is_array($options['recipients'])) {
$this->recipients = $options['recipients'];
}
$this->sendLog = isset($options['send_log']) && ((bool)$options['send_log'] !== false);
}
/**
* Run Telegram plugin.
* @return bool
*/
public function execute()
{
$message = $this->buildMessage();
$client = new Client();
$url = '/bot' . $this->authToken . '/sendMessage';
foreach ($this->recipients as $chatId) {
$chatId = $this->builder->interpolate($chatId, true);
[$chatId, $topicId] = $this->splitChatIdAndTopicId($chatId);
$params = [
'chat_id' => $chatId,
'text' => $message,
'parse_mode' => 'Markdown',
];
if ($topicId !== null) {
$params['message_thread_id'] = $topicId;
}
$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',
];
if ($topicId !== null) {
$params['message_thread_id'] = $topicId;
}
$client->post(('https://api.telegram.org' . $url), [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => $params,
]);
}
}
return true;
}
/**
* Build message.
* @return string
*/
private function buildMessage()
{
$this->buildMsg = '';
$buildIcon = $this->build->isSuccessful() ? '✅' : '❌';
$buildLog = $this->build->getLog();
$buildLog = \str_replace(['[0;32m', '[0;31m', '[0m', '/[0m'], '', $buildLog);
$buildMessages = \explode('RUNNING PLUGIN: ', $buildLog);
foreach ($buildMessages as $bm) {
$pos = \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->builder->interpolate(\str_replace(['%ICON_BUILD%'], [$buildIcon], $this->message));
}
/**
* Split chat group id to chat id and topic id
*
* @param int|string $chatId
* @return array{string, string|null}
*/
protected function splitChatIdAndTopicId($chatId)
{
$parts = \explode('/', \trim((string) $chatId) . '/');
$topicId = $parts[1] !== '' ? $parts[1] : null;
return [$parts[0], $topicId];
}
}