How to fix InputFile error when uploading images via URL in Telegram bot SDK

I’m experiencing issues with the telegram-bot-sdk package while trying to upload images from external URLs. Each time I attempt to send a photo using a web link, I receive an error message indicating that I need to utilize the InputFile::create() method. The message highlights that ‘A path to local file, a URL, or a file resource should be uploaded using ‘Telegram\Bot\FileUpload\InputFile::create($pathOrUrlOrResource, $filename)’ for the ‘photo’ property.’ I’m using version 3 of the package. Here’s my code example:

$botApi = new Api($token);
$response = $botApi->sendPhoto([
    'chat_id' => '123456789',
    'photo' => 'https://example.com/sample-image.jpg',
    'caption' => 'Test image upload'
]);

What’s the appropriate way to manage image uploads from URLs with this version of the SDK?

This happens because telegram-bot-sdk v3 got stricter with type checking for file uploads. You’ve got to use the InputFile class now for any file operations, even URL uploads. Here’s the fix:

use Telegram\Bot\FileUpload\InputFile;

$botApi = new Api($token);
$response = $botApi->sendPhoto([
    'chat_id' => '123456789',
    'photo' => InputFile::create('https://example.com/sample-image.jpg'),
    'caption' => 'Test image upload'
]);

Don’t forget the use statement at the top. They made this change to handle errors better and keep file uploads consistent across the newer version.

Had the same issue migrating from v2 to v3 a few months ago. That breaking change totally caught me off guard too. Besides wrapping your URL with InputFile::create(), make sure you handle exceptions properly - external URLs fail all the time due to network timeouts, bad URLs, or servers being down. Learned this the hard way when my bot started crashing in production. Some image URLs need proper user agents or headers to work, so if you’re still getting errors after using InputFile::create(), try downloading the image locally first then upload it as a local file instead.

just wrap ur url with InputFile::create() like this: ‘photo’ => InputFile::create(‘https://example.com/sample-image.jpg’). had the exact same issue last week - this fixed it. the SDK changed how it handles external links in v3.