2025-10-08 14:23:07 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Channels;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2025-10-17 11:30:59 +03:00
|
|
|
use App\Services\Logger;
|
2025-10-08 14:23:07 +03:00
|
|
|
|
|
|
|
|
class SlackChannel implements NotificationChannelInterface
|
|
|
|
|
{
|
|
|
|
|
private Client $client;
|
2025-10-17 11:30:59 +03:00
|
|
|
private Logger $logger;
|
2025-10-08 14:23:07 +03:00
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->client = new Client(['timeout' => 10]);
|
2025-10-17 11:30:59 +03:00
|
|
|
$this->logger = new Logger('slack_channel');
|
2025-10-08 14:23:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function send(array $config, string $message, array $data = []): bool
|
|
|
|
|
{
|
|
|
|
|
if (!isset($config['webhook_url'])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$payload = [
|
|
|
|
|
'text' => $message,
|
|
|
|
|
'blocks' => $this->createBlocks($message, $data)
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$response = $this->client->post($config['webhook_url'], [
|
|
|
|
|
'json' => $payload
|
|
|
|
|
]);
|
|
|
|
|
|
2025-10-17 11:30:59 +03:00
|
|
|
$ok = $response->getStatusCode() === 200;
|
|
|
|
|
if ($ok) {
|
|
|
|
|
$this->logger->info('Slack message sent', [
|
|
|
|
|
'status' => $response->getStatusCode()
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$this->logger->error('Slack non-200 status', [
|
|
|
|
|
'status' => $response->getStatusCode()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
return $ok;
|
2025-10-08 14:23:07 +03:00
|
|
|
} catch (\Exception $e) {
|
2025-10-17 11:30:59 +03:00
|
|
|
$this->logger->error('Slack send failed', [
|
|
|
|
|
'exception' => $e->getMessage()
|
|
|
|
|
]);
|
2025-10-08 14:23:07 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function createBlocks(string $message, array $data): array
|
|
|
|
|
{
|
|
|
|
|
$blocks = [
|
|
|
|
|
[
|
|
|
|
|
'type' => 'header',
|
|
|
|
|
'text' => [
|
|
|
|
|
'type' => 'plain_text',
|
|
|
|
|
'text' => '🔔 Domain Expiration Alert'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'type' => 'section',
|
|
|
|
|
'text' => [
|
|
|
|
|
'type' => 'mrkdwn',
|
|
|
|
|
'text' => $message
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (isset($data['domain'])) {
|
|
|
|
|
$blocks[] = [
|
|
|
|
|
'type' => 'section',
|
|
|
|
|
'fields' => [
|
|
|
|
|
[
|
|
|
|
|
'type' => 'mrkdwn',
|
|
|
|
|
'text' => "*Domain:*\n{$data['domain']}"
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'type' => 'mrkdwn',
|
|
|
|
|
'text' => "*Days Left:*\n{$data['days_left']}"
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'type' => 'mrkdwn',
|
|
|
|
|
'text' => "*Expiration:*\n{$data['expiration_date']}"
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'type' => 'mrkdwn',
|
|
|
|
|
'text' => "*Registrar:*\n{$data['registrar']}"
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $blocks;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|