Deploy on 2026-06-05 16:08:53
This commit is contained in:
@@ -2,21 +2,49 @@
|
||||
|
||||
namespace App\Services\Notification;
|
||||
|
||||
use App\Services\Database\Connection;
|
||||
use PDO;
|
||||
use Throwable;
|
||||
|
||||
class TelegramNotifier
|
||||
{
|
||||
private ?string $botToken;
|
||||
private ?string $chatId;
|
||||
private PDO $pdo;
|
||||
private ?string $botToken = null;
|
||||
private ?string $chatId = null;
|
||||
private ?string $lastError = null;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->botToken = $_ENV['TELEGRAM_BOT_TOKEN'] ?? null;
|
||||
$this->chatId = $_ENV['TELEGRAM_CHAT_ID'] ?? null;
|
||||
$this->pdo = $connection->getPdo();
|
||||
$this->loadSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure from database settings.
|
||||
* Load settings from database settings table.
|
||||
*/
|
||||
public function loadSettings(): void
|
||||
{
|
||||
$this->botToken = $this->getSetting('telegram_bot_token');
|
||||
$this->chatId = $this->getSetting('telegram_chat_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to retrieve setting by key.
|
||||
*/
|
||||
private function getSetting(string $key): ?string
|
||||
{
|
||||
try {
|
||||
$stmt = $this->pdo->prepare("SELECT `value` FROM settings WHERE `key` = ?");
|
||||
$stmt->execute([$key]);
|
||||
$val = $stmt->fetchColumn();
|
||||
return $val !== false ? $val : null;
|
||||
} catch (Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure manually (for testing unsaved parameters).
|
||||
*/
|
||||
public function configure(string $botToken, string $chatId): void
|
||||
{
|
||||
@@ -24,12 +52,23 @@ class TelegramNotifier
|
||||
$this->chatId = $chatId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the last HTTP or API error message.
|
||||
*/
|
||||
public function getLastError(): ?string
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a notification to Telegram.
|
||||
*/
|
||||
public function send(string $message, string $type = 'info'): bool
|
||||
{
|
||||
$this->lastError = null;
|
||||
|
||||
if (!$this->botToken || !$this->chatId) {
|
||||
$this->lastError = "Telegram token or chat ID is missing.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -54,19 +93,62 @@ class TelegramNotifier
|
||||
'disable_web_page_preview' => true,
|
||||
]);
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n",
|
||||
'content' => $payload,
|
||||
'timeout' => 10,
|
||||
],
|
||||
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false],
|
||||
]);
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
return $response !== false;
|
||||
$response = curl_exec($ch);
|
||||
if ($response === false) {
|
||||
$this->lastError = "Connection error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
$resDecoded = json_decode($response, true);
|
||||
$this->lastError = $resDecoded['description'] ?? "API error HTTP {$httpCode}";
|
||||
error_log("Telegram API returned code {$httpCode}: {$response}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n",
|
||||
'content' => $payload,
|
||||
'timeout' => 10,
|
||||
],
|
||||
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false],
|
||||
]);
|
||||
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
if ($response === false) {
|
||||
$error = error_get_last();
|
||||
$this->lastError = $error['message'] ?? "Connection timed out.";
|
||||
return false;
|
||||
}
|
||||
|
||||
$resDecoded = json_decode($response, true);
|
||||
if (($resDecoded['ok'] ?? false) === false) {
|
||||
$this->lastError = $resDecoded['description'] ?? "API error";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->lastError = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user