diff --git a/src/Notifier/TelegramNotifier.php b/src/Notifier/TelegramNotifier.php index 34ce982..421a351 100644 --- a/src/Notifier/TelegramNotifier.php +++ b/src/Notifier/TelegramNotifier.php @@ -7,27 +7,45 @@ use Jakach\Logging\Storage\Repository; class TelegramNotifier { - private ?string $botToken; - private ?string $chatId; + private Repository $repo; public function __construct( - private Repository $repo, + Repository $repo, ) { - $this->botToken = $this->repo->getConfig('telegram_bot_token'); - $this->chatId = $this->repo->getConfig('telegram_chat_id'); + $this->repo = $repo; + $this->logConfig(); + } + + private function getBotToken(): ?string + { + return $this->repo->getConfig('telegram_bot_token'); + } + + private function getChatId(): ?string + { + return $this->repo->getConfig('telegram_chat_id'); + } + + private function logConfig(): void + { + $token = $this->getBotToken(); + $chatId = $this->getChatId(); error_log(sprintf("Telegram: bot_token=%s, chat_id=%s", - $this->botToken ? substr($this->botToken, 0, 8) . '...' : 'EMPTY', - $this->chatId ?? 'EMPTY')); + $token ? substr($token, 0, 8) . '...' : 'EMPTY', + $chatId ?? 'EMPTY')); } public function isConfigured(): bool { - return !empty($this->botToken) && !empty($this->chatId); + return !empty($this->getBotToken()) && !empty($this->getChatId()); } public function send(Alert $alert): bool { - if (!$this->isConfigured()) { + $botToken = $this->getBotToken(); + $chatId = $this->getChatId(); + + if (empty($botToken) || empty($chatId)) { error_log("Telegram: not configured, skipping alert #{$alert->id}"); return false; } @@ -52,17 +70,16 @@ class TelegramNotifier $text .= sprintf("*Source:* %s\n", $alert->sourceName ?? '—'); $text .= sprintf("*Message:* ```\n%s\n```", mb_substr($alert->message, 0, 1000)); - $url = 'https://api.telegram.org/bot' . $this->botToken . '/sendMessage'; + $url = 'https://api.telegram.org/bot' . $botToken . '/sendMessage'; error_log("Telegram: POST " . preg_replace('/bot.*\//', 'botTOKEN/', $url)); $postData = http_build_query([ - 'chat_id' => $this->chatId, + 'chat_id' => $chatId, 'text' => $text, 'parse_mode' => 'Markdown', 'disable_web_page_preview' => true, ]); - error_log("Telegram: post data length=" . strlen($postData) . ", text length=" . strlen($text)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);