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:
co-authored by
Claude Sonnet 4.6
parent
50bbd159c6
commit
17382a907a
@@ -1108,18 +1108,15 @@ class TelegramBotService
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = "https://api.telegram.org/bot{$token}/{$method}";
|
try {
|
||||||
$ctx = stream_context_create([
|
$response = Http::timeout(10)
|
||||||
'http' => [
|
->post("https://api.telegram.org/bot{$token}/{$method}", $data);
|
||||||
'method' => 'POST',
|
|
||||||
'header' => "Content-Type: application/json\r\n",
|
|
||||||
'content' => json_encode($data),
|
|
||||||
'timeout' => 10,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$result = @file_get_contents($url, false, $ctx);
|
return $response->successful();
|
||||||
return $result !== false;
|
} catch (\Throwable $e) {
|
||||||
|
Log::warning("[TelegramBot] apiCall {$method} failed: " . $e->getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function downloadTelegramFile(string $fileId): ?array
|
private function downloadTelegramFile(string $fileId): ?array
|
||||||
@@ -1129,17 +1126,14 @@ class TelegramBotService
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$info = @file_get_contents("https://api.telegram.org/bot{$token}/getFile?file_id={$fileId}");
|
try {
|
||||||
if (! $info) {
|
$info = Http::timeout(10)->get("https://api.telegram.org/bot{$token}/getFile", ['file_id' => $fileId]);
|
||||||
return null;
|
$filePath = $info->json('result.file_path');
|
||||||
}
|
|
||||||
|
|
||||||
$filePath = json_decode($info, true)['result']['file_path'] ?? null;
|
|
||||||
if (! $filePath) {
|
if (! $filePath) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$content = @file_get_contents("https://api.telegram.org/file/bot{$token}/{$filePath}");
|
$content = Http::timeout(30)->get("https://api.telegram.org/file/bot{$token}/{$filePath}")->body();
|
||||||
if (! $content) {
|
if (! $content) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -1152,6 +1146,11 @@ class TelegramBotService
|
|||||||
};
|
};
|
||||||
|
|
||||||
return ['content' => $content, 'mime' => $mime];
|
return ['content' => $content, 'mime' => $mime];
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Log::warning('[TelegramBot] downloadTelegramFile failed: ' . $e->getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Helpers ──────────────────────────────────────────────
|
// ─── Helpers ──────────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user