Files
domnitor/app/Services/Channels/WebhookChannel.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

67 lines
1.8 KiB
PHP

<?php
namespace App\Services\Channels;
use GuzzleHttp\Client;
use App\Services\Logger;
class WebhookChannel implements NotificationChannelInterface
{
private Client $httpClient;
private Logger $logger;
public function __construct()
{
$this->httpClient = new Client(['timeout' => 10]);
$this->logger = new Logger('webhook_channel');
}
public function send(array $config, string $message, array $data = []): bool
{
$url = trim($config['webhook_url'] ?? '');
if (empty($url)) {
return false;
}
// Build a sane, generic JSON payload for automation tools (n8n, Zapier, etc.)
$payload = [
'event' => 'domain_expiration_alert',
'message' => $message,
'data' => $data,
'sent_at' => date('c')
];
try {
$response = $this->httpClient->post($url, [
'headers' => [
'Content-Type' => 'application/json'
],
'json' => $payload
]);
$status = $response->getStatusCode();
$ok = $status >= 200 && $status < 300;
if ($ok) {
$this->logger->info('Webhook sent successfully', [
'url' => $url,
'status' => $status
]);
} else {
$this->logger->error('Webhook responded with non-2xx', [
'url' => $url,
'status' => $status
]);
}
return $ok;
} catch (\Exception $e) {
$this->logger->error('Webhook send failed', [
'url' => $url,
'exception' => $e->getMessage()
]);
return false;
}
}
}