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 ──────────────────────────────────────────────