@@ -15,6 +15,9 @@ class TelegramNotifier
|
|||||||
) {
|
) {
|
||||||
$this->botToken = $this->repo->getConfig('telegram_bot_token');
|
$this->botToken = $this->repo->getConfig('telegram_bot_token');
|
||||||
$this->chatId = $this->repo->getConfig('telegram_chat_id');
|
$this->chatId = $this->repo->getConfig('telegram_chat_id');
|
||||||
|
fprintf(STDERR, "Telegram: bot_token=%s, chat_id=%s\n",
|
||||||
|
$this->botToken ? substr($this->botToken, 0, 8) . '...' : 'EMPTY',
|
||||||
|
$this->chatId ?? 'EMPTY');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isConfigured(): bool
|
public function isConfigured(): bool
|
||||||
@@ -25,9 +28,12 @@ class TelegramNotifier
|
|||||||
public function send(Alert $alert): bool
|
public function send(Alert $alert): bool
|
||||||
{
|
{
|
||||||
if (!$this->isConfigured()) {
|
if (!$this->isConfigured()) {
|
||||||
|
fprintf(STDERR, "Telegram: not configured, skipping alert #%d\n", $alert->id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(STDERR, "Telegram: sending alert #%d [%s]...\n", $alert->id, $alert->severity->value);
|
||||||
|
|
||||||
$emoji = match ($alert->severity->value) {
|
$emoji = match ($alert->severity->value) {
|
||||||
'emergency', 'critical_high', 'critical' => "\xF0\x9F\x94\xA5",
|
'emergency', 'critical_high', 'critical' => "\xF0\x9F\x94\xA5",
|
||||||
'critical_low', 'error' => "\xE2\x9D\x8C",
|
'critical_low', 'error' => "\xE2\x9D\x8C",
|
||||||
@@ -48,26 +54,36 @@ class TelegramNotifier
|
|||||||
|
|
||||||
$url = 'https://api.telegram.org/bot' . $this->botToken . '/sendMessage';
|
$url = 'https://api.telegram.org/bot' . $this->botToken . '/sendMessage';
|
||||||
|
|
||||||
$ch = curl_init();
|
fprintf(STDERR, "Telegram: POST %s\n", preg_replace('/bot.*\//', 'botTOKEN/', $url));
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
$postData = http_build_query([
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
|
||||||
'chat_id' => $this->chatId,
|
'chat_id' => $this->chatId,
|
||||||
'text' => $text,
|
'text' => $text,
|
||||||
'parse_mode' => 'Markdown',
|
'parse_mode' => 'Markdown',
|
||||||
'disable_web_page_preview' => true,
|
'disable_web_page_preview' => true,
|
||||||
]));
|
]);
|
||||||
|
fprintf(STDERR, "Telegram: post data length=%d, text length=%d\n", strlen($postData), strlen($text));
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$curlError = curl_error($ch);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
|
fprintf(STDERR, "Telegram: HTTP %d, curl_error=%s, response=%s\n", $httpCode, $curlError, substr($response, 0, 500));
|
||||||
|
|
||||||
if ($httpCode !== 200) {
|
if ($httpCode !== 200) {
|
||||||
fprintf(STDERR, "Telegram send failed (HTTP %d): %s\n", $httpCode, $response);
|
fprintf(STDERR, "Telegram send failed (HTTP %d): %s\n", $httpCode, $response);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(STDERR, "Telegram: sent successfully\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user