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 TelegramChannel 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([
|
|
|
|
|
'base_uri' => 'https://api.telegram.org',
|
|
|
|
|
'timeout' => 10,
|
|
|
|
|
]);
|
2025-10-17 11:30:59 +03:00
|
|
|
$this->logger = new Logger('telegram_channel');
|
2025-10-08 14:23:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
2025-10-17 11:30:59 +03:00
|
|
|
$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;
|
2025-10-08 14:23:07 +03:00
|
|
|
} catch (\Exception $e) {
|
2025-10-17 11:30:59 +03:00
|
|
|
$this->logger->error('Telegram send failed', [
|
|
|
|
|
'chat_id' => $config['chat_id'] ?? null,
|
|
|
|
|
'exception' => $e->getMessage()
|
|
|
|
|
]);
|
2025-10-08 14:23:07 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|