first(); if ($record) { $setting = json_decode($record->value); $this->token = $setting->bot_token ?? null; $this->group = $setting->bot_id ?? null; } else { $this->token = null; $this->group = null; } } public function SendMessage($message = '') { if (!$this->token || !$this->group) { Log::channel('Custom')->warning('Telegram notification not sent: Missing bot token or group ID'); return null; } try { $url = "https://api.telegram.org/bot$this->token/sendMessage?parse_mode=html&chat_id=$this->group&text=" . urlencode($message); $response = Http::get($url); // Log response để debug Log::channel('Custom')->info('Telegram sendMessage URL: ' . $url); Log::channel('Custom')->info('Telegram sendMessage response: ' . $response->body()); Log::channel('Custom')->info('Telegram sendMessage status: ' . $response->status()); return $response; } catch (\Exception $exception) { Log::channel('Custom')->error('Failed to send Telegram notification: ' . $exception->getMessage()); return null; } } public function SendErrorMessage($data) { $msg = "Admin: Nhận được thông báo lỗi khi khởi tạo dịch vụ \n"; $msg .= "--------------------------------\n"; $msg .= "| " . (is_string($data) ? $data : json_encode($data)) . "\n"; $msg .= "--------------------------------"; return $this->SendMessage($msg); } public function SendTrialRegistration($client, array $trialConfig = []) { $msg = "🧪 Khách hàng vừa đăng ký dùng thử Cloud VPS!\n"; $msg .= "--------------------------------\n"; $msg .= "👤 Tên: {$client->fullname}\n"; $msg .= "📧 Email: {$client->email}\n"; $msg .= "📱 SĐT: {$client->phone}\n"; $msg .= "--------------------------------\n"; $msg .= "🖥️ CPU: " . ($trialConfig['cpu'] ?? '-') . "\n"; $msg .= "💾 RAM: " . ($trialConfig['ram'] ?? '-') . "\n"; $msg .= "💽 SSD: " . ($trialConfig['ssd'] ?? '-') . "\n"; $msg .= "🧠 Hệ điều hành: " . ($trialConfig['os'] ?? '-') . "\n"; if (!empty($trialConfig['other'])) { $msg .= "📝 Yêu cầu khác: {$trialConfig['other']}\n"; } $msg .= "🕒 Thời gian: " . Carbon::now()->format('d/m/Y H:i:s'); return $this->SendMessage($msg); } public function SendManualDepositPendingApproval($customer, $transaction) { $msg = "🔔 Yêu cầu nạp tiền chờ duyệt!\n"; $msg .= "--------------------------------\n"; $msg .= "👤 Khách: {$customer->fullname}\n"; $msg .= "📧 Email: {$customer->email}\n"; $msg .= "💵 Số tiền: " . number_format($transaction->amount, 0, ',', '.') . " VNĐ\n"; $msg .= "🧾 Mã GD: {$transaction->public_code}\n"; $msg .= "🕒 Thời gian: " . Carbon::now()->format('d/m/Y H:i:s') . "\n"; $msg .= "--------------------------------\n"; $msg .= "⚠️ Vui lòng vào admin để xác nhận giao dịch."; return $this->SendMessage($msg); } public function SendJobRunInfo($jobName, $status, $additionalInfo = null) { $msg = "⚙️ Thông báo chạy job: {$jobName}\n"; $msg .= "⏱️ Thời gian: " . Carbon::now()->format('d/m/Y H:i:s') . "\n"; $msg .= "📊 Trạng thái: {$status}"; if ($additionalInfo) { $msg .= "\n📋 Thông tin thêm: " . (is_string($additionalInfo) ? $additionalInfo : json_encode($additionalInfo)); } return $this->SendMessage($msg); } /** * Notify one step of the VM rebuild pipeline (success or failure). */ public function SendRebuildStepNotification(string $vmName, string $ip, string $step, bool $success, ?string $note = null) { $msg = ($success ? '🔄' : '❌') . " Rebuild VPS — {$step}\n"; $msg .= "--------------------------------\n"; $msg .= "🖥️ VM: {$vmName}\n"; $msg .= "🌐 IP: {$ip}\n"; $msg .= "📊 Trạng thái: " . ($success ? 'Hoàn tất ✅' : 'Thất bại ❌') . "\n"; if ($note) { $msg .= "📝 " . ($success ? 'Ghi chú' : 'Lỗi') . ": {$note}\n"; } $msg .= "🕒 Thời gian: " . Carbon::now()->format('d/m/Y H:i:s') . "\n"; $msg .= "--------------------------------"; return $this->SendMessage($msg); } /** * Send a photo to Telegram group * @param string $filePath Local storage path (e.g. Upload/2024/06/01/image.png) * @param string|null $caption Optional caption for the photo * @return mixed|null */ public function SendPhoto($filePath, $caption = null) { if (!$this->token || !$this->group) { Log::channel('Custom')->warning('Telegram photo not sent: Missing bot token or group ID'); return null; } try { $fullPath = public_path('template/' . $filePath); if (!file_exists($fullPath)) { Log::channel('Custom')->error('Telegram photo not sent: File does not exist - ' . $fullPath); return null; } $multipart = [ [ 'name' => 'chat_id', 'contents' => $this->group, ], [ 'name' => 'photo', 'contents' => fopen($fullPath, 'r'), ], ]; if ($caption) { $multipart[] = [ 'name' => 'caption', 'contents' => $caption, ]; } $response = Http::attach('photo', fopen($fullPath, 'r'), basename($fullPath)) ->post("https://api.telegram.org/bot{$this->token}/sendPhoto", [ 'chat_id' => $this->group, 'caption' => $caption, ]); return $response->json(); } catch (\Exception $exception) { Log::channel('Custom')->error('Failed to send Telegram photo: ' . $exception->getMessage()); return null; } } }