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