Files
domnitor/app/Services/Channels/TelegramChannel.php
Hosteroid c28ec4921e Add structured logging to notification channels
Replaced error_log calls with structured logging using the Logger class in DiscordChannel, SlackChannel, TelegramChannel, and WebhookChannel. Log success and error cases with relevant context for improved observability and debugging.
2025-10-17 11:30:59 +03:00

61 lines
1.7 KiB
PHP

<?php
namespace App\Services\Channels;
use GuzzleHttp\Client;
use App\Services\Logger;
class TelegramChannel implements NotificationChannelInterface
{
private Client $client;
private Logger $logger;
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://api.telegram.org',
'timeout' => 10,
]);
$this->logger = new Logger('telegram_channel');
}
public function send(array $config, string $message, array $data = []): bool
{
if (!isset($config['bot_token']) || !isset($config['chat_id'])) {
return false;
}
try {
$response = $this->client->post("/bot{$config['bot_token']}/sendMessage", [
'json' => [
'chat_id' => $config['chat_id'],
'text' => $message,
'parse_mode' => 'HTML',
'disable_web_page_preview' => true,
]
]);
$ok = $response->getStatusCode() === 200;
if ($ok) {
$this->logger->info('Telegram message sent', [
'chat_id' => $config['chat_id'],
'status' => $response->getStatusCode()
]);
} else {
$this->logger->error('Telegram non-200 status', [
'chat_id' => $config['chat_id'],
'status' => $response->getStatusCode()
]);
}
return $ok;
} catch (\Exception $e) {
$this->logger->error('Telegram send failed', [
'chat_id' => $config['chat_id'] ?? null,
'exception' => $e->getMessage()
]);
return false;
}
}
}