From 17382a907a0f7d03e20199c37f48beeebf26f3eb Mon Sep 17 00:00:00 2001 From: Lizandro Date: Tue, 14 Jul 2026 18:34:44 +0000 Subject: [PATCH] Fix Telegram bot responses: replace file_get_contents with Http client apiCall() and downloadTelegramFile() were using file_get_contents() for outgoing requests to api.telegram.org, which fails on restricted servers. Now use Http:: (Guzzle) consistently, matching the rest of the project. Co-Authored-By: Claude Sonnet 4.6 --- app/Services/TelegramBotService.php | 63 ++++++++++++++--------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index ef38254..0efcd3d 100644 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -1108,18 +1108,15 @@ class TelegramBotService return false; } - $url = "https://api.telegram.org/bot{$token}/{$method}"; - $ctx = stream_context_create([ - 'http' => [ - 'method' => 'POST', - 'header' => "Content-Type: application/json\r\n", - 'content' => json_encode($data), - 'timeout' => 10, - ], - ]); + try { + $response = Http::timeout(10) + ->post("https://api.telegram.org/bot{$token}/{$method}", $data); - $result = @file_get_contents($url, false, $ctx); - return $result !== false; + return $response->successful(); + } catch (\Throwable $e) { + Log::warning("[TelegramBot] apiCall {$method} failed: " . $e->getMessage()); + return false; + } } private function downloadTelegramFile(string $fileId): ?array @@ -1129,29 +1126,31 @@ class TelegramBotService return null; } - $info = @file_get_contents("https://api.telegram.org/bot{$token}/getFile?file_id={$fileId}"); - if (! $info) { + try { + $info = Http::timeout(10)->get("https://api.telegram.org/bot{$token}/getFile", ['file_id' => $fileId]); + $filePath = $info->json('result.file_path'); + if (! $filePath) { + return null; + } + + $content = Http::timeout(30)->get("https://api.telegram.org/file/bot{$token}/{$filePath}")->body(); + if (! $content) { + return null; + } + + $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); + $mime = match ($ext) { + 'png' => 'image/png', + 'webp' => 'image/webp', + default => 'image/jpeg', + }; + + return ['content' => $content, 'mime' => $mime]; + + } catch (\Throwable $e) { + Log::warning('[TelegramBot] downloadTelegramFile failed: ' . $e->getMessage()); return null; } - - $filePath = json_decode($info, true)['result']['file_path'] ?? null; - if (! $filePath) { - return null; - } - - $content = @file_get_contents("https://api.telegram.org/file/bot{$token}/{$filePath}"); - if (! $content) { - return null; - } - - $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); - $mime = match ($ext) { - 'png' => 'image/png', - 'webp' => 'image/webp', - default => 'image/jpeg', - }; - - return ['content' => $content, 'mime' => $mime]; } // ─── Helpers ──────────────────────────────────────────────