I’m working with a Telegram bot and running into issues when trying to send images. Every time I attempt to upload a picture, I get this error message saying the photo has an unsupported extension and I should use jpg, jpeg, gif, png, tif or bmp formats.
The weird thing is that my image file is definitely a jpg file, so I don’t understand why it’s complaining about the extension. The bot connection works fine since I can call other methods without problems.
Here’s the code I’m using:
using (var fileStream = File.Open("photo.jpg", FileMode.Open))
{
var response = await telegramBot.SendPhotoAsync(chatId, new InputOnlineFile(fileStream, "image.jpg"));
}
This happens when your file stream isn’t at the beginning. Add fileStream.Seek(0, SeekOrigin.Begin); right after opening the stream and before the API call. I’ve hit this exact error when the stream was already partially read or positioned elsewhere in the file. Also check you’re not passing an empty stream - throw in a quick fileStream.Length > 0 check before sending. Telegram’s API freaks out when it can’t read the file signature from the stream start.
Had the same issue a few months ago - turned out to be a corrupted file. The .jpg extension was there, but the file header was trashed. Open it in an image editor first to make sure it’s actually a valid JPG. Files get corrupted during transfer all the time. Also check the file size since Telegram caps photo uploads, and make sure you’re disposing the file stream properly when done. I just recreated the image file from scratch and that fixed it.
Check your mime type when creating the InputOnlineFile. Telegram can be picky about this even with the right extension. Try setting it explicitly to “image/jpeg” in the constructor, or just use InputOnlineFile.FromStream instead - fixed the same issue for me.