1 | <?php |
||
18 | class Target extends \yii\log\Target |
||
19 | { |
||
20 | /** |
||
21 | * @var Client|array|string Yii HTTP client configuration. |
||
22 | * This can be a component ID, a configuration array or a Client instance. |
||
23 | */ |
||
24 | public $httpClient = [ |
||
25 | 'class' => 'yii\httpclient\Client', |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var string bot token. |
||
30 | * @see https://core.telegram.org/bots/api#authorizing-your-bot |
||
31 | */ |
||
32 | public $token; |
||
33 | |||
34 | /** |
||
35 | * @var int|string unique identifier for the target chat or username of the target channel |
||
36 | * (in the format `{@}channelusername`). |
||
37 | */ |
||
38 | public $chatId; |
||
39 | |||
40 | /** |
||
41 | * @var string log message template. |
||
42 | */ |
||
43 | public $template = '{levelAndRequest} |
||
44 | |||
45 | {text} |
||
46 | |||
47 | {category} |
||
48 | {user} |
||
49 | {stackTrace}'; |
||
50 | |||
51 | /** |
||
52 | * @var array log message template substitutions. |
||
53 | * [[defaultSubstitutions()]] will be used by default. |
||
54 | */ |
||
55 | public $substitutions; |
||
56 | |||
57 | /** |
||
58 | * @var bool|bool[] whether to send the message silently (`bool` or an array of `bool` per a logger level). |
||
59 | * iOS users will not receive a notification, Android users will receive a notification with no sound. |
||
60 | */ |
||
61 | public $enableNotification = true; |
||
62 | |||
63 | /** |
||
64 | * @var string[] level emoji per a logger level. |
||
65 | * @since 2.0 |
||
66 | */ |
||
67 | public $levelEmojis = [ |
||
68 | Logger::LEVEL_ERROR => '☠️', |
||
69 | Logger::LEVEL_WARNING => '⚠️', |
||
70 | Logger::LEVEL_INFO => 'ℹ️', |
||
71 | Logger::LEVEL_TRACE => '📝', |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * @inheritDoc |
||
76 | */ |
||
77 | 5 | public function __construct($config = []) |
|
82 | |||
83 | /** |
||
84 | * @inheritDoc |
||
85 | */ |
||
86 | 5 | public function init() |
|
94 | |||
95 | /** |
||
96 | * @inheritDoc |
||
97 | * @throws \yii\base\InvalidValueException |
||
98 | */ |
||
99 | 2 | public function export() |
|
100 | { |
||
101 | 2 | foreach (array_map([$this, 'formatMessageRequest'], $this->messages) as $request) { |
|
102 | $response = $this->httpClient |
||
103 | ->post('https://api.telegram.org/bot' . $this->token . '/sendMessage', $request) |
||
104 | ->setFormat(Client::FORMAT_JSON) |
||
105 | ->send(); |
||
106 | if (!$response->getIsOk()) { |
||
107 | if (isset($response->getData()['description'])) { |
||
108 | $description = $response->getData()['description']; |
||
109 | } else { |
||
110 | $description = $response->getContent(); |
||
111 | } |
||
112 | throw new InvalidValueException( |
||
113 | 'Unable to send logs to Telegram: ' . $description, (int) $response->getStatusCode() |
||
114 | ); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns a `sendMessage` request for Telegram. |
||
121 | * @param array $message raw message. |
||
122 | * @return array request as an array. |
||
123 | */ |
||
124 | 5 | protected function formatMessageRequest($message) |
|
149 | |||
150 | /** |
||
151 | * Returns an array with the default substitutions. |
||
152 | * @return array default substitutions. |
||
153 | */ |
||
154 | 2 | protected function defaultSubstitutions() |
|
155 | { |
||
156 | return [ |
||
157 | 'levelAndRequest' => [ |
||
158 | 2 | 'title' => null, |
|
159 | 2 | 'short' => false, |
|
160 | 2 | 'wrapAsCode' => false, |
|
161 | 'value' => function (Message $message) { |
||
162 | 2 | if (isset($this->levelEmojis[$message->message[1]])) { |
|
163 | 1 | $value = $this->levelEmojis[$message->message[1]] . ' '; |
|
164 | 1 | } else { |
|
165 | 1 | $value = '*' . ucfirst($message->getLevel()) . '* @ '; |
|
166 | } |
||
167 | 2 | if ($message->getIsConsoleRequest()) { |
|
168 | 1 | $value .= '`' . $message->getCommandLine() . '`'; |
|
169 | 1 | } else { |
|
170 | 1 | $value .= '[' . $message->getUrl() . '](' . $message->getUrl() . ')'; |
|
171 | } |
||
172 | 1 | return $value; |
|
173 | 2 | }, |
|
174 | 2 | ], |
|
175 | 'category' => [ |
||
176 | 2 | 'emojiTitle' => '📖', |
|
177 | 2 | 'short' => true, |
|
178 | 2 | 'wrapAsCode' => false, |
|
179 | 'value' => function (Message $message) { |
||
180 | 1 | return '`' . $message->getCategory() . '`'; |
|
181 | 2 | }, |
|
182 | 2 | ], |
|
183 | 'user' => [ |
||
184 | 2 | 'emojiTitle' => '🙂', |
|
185 | 2 | 'short' => true, |
|
186 | 2 | 'wrapAsCode' => false, |
|
187 | 'value' => function (Message $message) { |
||
188 | 1 | $value = []; |
|
189 | 1 | $ip = $message->getUserIp(); |
|
190 | 1 | if ((string) $ip !== '') { |
|
191 | $value[] = $ip; |
||
192 | } |
||
193 | 1 | $id = $message->getUserId(); |
|
194 | 1 | if ((string) $id !== '') { |
|
195 | $value[] = "ID: `{$id}`"; |
||
196 | } |
||
197 | 1 | return implode(str_repeat(' ', 4), $value); |
|
198 | 2 | }, |
|
199 | 2 | ], |
|
200 | 'stackTrace' => [ |
||
201 | 2 | 'title' => 'Stack Trace', |
|
202 | 2 | 'short' => false, |
|
203 | 2 | 'wrapAsCode' => true, |
|
204 | 'value' => function (Message $message) { |
||
205 | 1 | return $message->getStackTrace(); |
|
206 | 2 | }, |
|
207 | 2 | ], |
|
208 | 'text' => [ |
||
209 | 2 | 'title' => null, |
|
210 | 2 | 'short' => false, |
|
211 | 2 | 'wrapAsCode' => true, |
|
212 | 2 | 'value' => function (Message $message) { |
|
213 | 1 | return $message->getText(); |
|
214 | 2 | }, |
|
215 | 2 | ], |
|
216 | 2 | ]; |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns a substituted value. |
||
221 | * @param string $name |
||
222 | * @param Message $message |
||
223 | * @return string |
||
224 | */ |
||
225 | 5 | private function substitute($name, Message $message) |
|
248 | } |
||
249 |