forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegram.php
More file actions
140 lines (120 loc) · 3.94 KB
/
Copy pathTelegram.php
File metadata and controls
140 lines (120 loc) · 3.94 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
<?php
namespace PHPCensor\Plugin;
use Exception;
use GuzzleHttp\Client;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Telegram Plugin
*
* @author LEXASOFT <lexasoft83@gmail.com>
*/
class Telegram extends Plugin
{
protected $apiKey;
protected $message;
protected $buildMsg;
protected $recipients;
protected $sendLog;
/**
* @return string
*/
public static function pluginName()
{
return 'telegram';
}
/**
* Standard Constructor
*
* @param Builder $builder
* @param Build $build
* @param array $options
* @throws Exception
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (empty($options['api_key'])) {
throw new Exception("Not setting telegram api_key");
}
if (empty($options['recipients'])) {
throw new Exception("Not setting recipients");
}
$this->apiKey = $options['api_key'];
$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->apiKey . '/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;
}
/**
* 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'])) {
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));
}
}