This commit is contained in:
@@ -7,27 +7,45 @@ use Jakach\Logging\Storage\Repository;
|
|||||||
|
|
||||||
class TelegramNotifier
|
class TelegramNotifier
|
||||||
{
|
{
|
||||||
private ?string $botToken;
|
private Repository $repo;
|
||||||
private ?string $chatId;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private Repository $repo,
|
Repository $repo,
|
||||||
) {
|
) {
|
||||||
$this->botToken = $this->repo->getConfig('telegram_bot_token');
|
$this->repo = $repo;
|
||||||
$this->chatId = $this->repo->getConfig('telegram_chat_id');
|
$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",
|
error_log(sprintf("Telegram: bot_token=%s, chat_id=%s",
|
||||||
$this->botToken ? substr($this->botToken, 0, 8) . '...' : 'EMPTY',
|
$token ? substr($token, 0, 8) . '...' : 'EMPTY',
|
||||||
$this->chatId ?? 'EMPTY'));
|
$chatId ?? 'EMPTY'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isConfigured(): bool
|
public function isConfigured(): bool
|
||||||
{
|
{
|
||||||
return !empty($this->botToken) && !empty($this->chatId);
|
return !empty($this->getBotToken()) && !empty($this->getChatId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function send(Alert $alert): bool
|
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}");
|
error_log("Telegram: not configured, skipping alert #{$alert->id}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -52,17 +70,16 @@ class TelegramNotifier
|
|||||||
$text .= sprintf("*Source:* %s\n", $alert->sourceName ?? '—');
|
$text .= sprintf("*Source:* %s\n", $alert->sourceName ?? '—');
|
||||||
$text .= sprintf("*Message:* ```\n%s\n```", mb_substr($alert->message, 0, 1000));
|
$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));
|
error_log("Telegram: POST " . preg_replace('/bot.*\//', 'botTOKEN/', $url));
|
||||||
|
|
||||||
$postData = http_build_query([
|
$postData = http_build_query([
|
||||||
'chat_id' => $this->chatId,
|
'chat_id' => $chatId,
|
||||||
'text' => $text,
|
'text' => $text,
|
||||||
'parse_mode' => 'Markdown',
|
'parse_mode' => 'Markdown',
|
||||||
'disable_web_page_preview' => true,
|
'disable_web_page_preview' => true,
|
||||||
]);
|
]);
|
||||||
error_log("Telegram: post data length=" . strlen($postData) . ", text length=" . strlen($text));
|
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
|||||||
Reference in New Issue
Block a user