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 <noreply@anthropic.com>
This commit is contained in:
Lizandro
2026-07-14 18:34:44 +00:00
co-authored by Claude Sonnet 4.6
parent 50bbd159c6
commit 17382a907a
+31 -32
View File
@@ -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 ──────────────────────────────────────────────