Finding MIME type for images received through Telegram bot

I’m working on a Telegram bot that needs to handle photos sent by users. My bot stores the file IDs from these images so I can download them later using the bot API.

The problem is that I need to know the MIME type of each photo. The Telegram documentation mentions that you should save MIME type info when receiving files because it might not be preserved during download. But when I look at the PhotoSize object that comes with photo messages, there’s no MIME type field included.

Other file types like videos have MIME type information in their objects, but photos seem to be missing this. Is there a different way to determine what format the photo is in? I’m using a JavaScript library to interact with the bot API.

Any suggestions on how to handle this would be great.

Telegram automatically converts all photos to JPEG when they’re sent through the bot API, regardless of the original format uploaded by users. This compression is done to save bandwidth and storage space. You can reliably assume that any photo retrieved from PhotoSize objects will always be in JPEG format with the MIME type image/jpeg. My experience managing bots for the past two years confirms that all images processed through Telegram’s system result in JPEGs. In contrast, documents maintain their original MIME types since they undergo a different handling process; photos are standardized while documents retain their formats. Therefore, if users wish to send PNGs or GIFs, they must do so by sending them as documents instead of photos.

Yeah, the previous answer’s right about Telegram converting photos to JPEG, but I’d build something more robust instead of hardcoding assumptions.

I’ve been burned by API changes before when I relied on assumptions. Now I create automated workflows that detect file types after download, regardless of what the API claims.

Here’s what works: download the file, check its actual MIME type using file headers, then store that info. Works for any file type and you’re protected when APIs change.

Best part? It runs completely automatically. Bot receives photo → workflow triggers → downloads and identifies real MIME type → updates database. Zero manual work.

I built this for our company’s content system. Processes thousands of files daily and never misses the correct type, even when external APIs give garbage info.

Latenode’s perfect for this kind of automation. Create the whole workflow visually, connect it to your Telegram bot, and you’re running in minutes. No complex coding needed.

You can verify the MIME type by checking the file header bytes after downloading from Telegram’s servers. Photos do get converted to JPEG, but I’d still implement a proper check instead of assuming. Download the file using file_path from getFile, then read the first few bytes to confirm format. JPEG files start with FF D8 FF, PNG files begin with 89 50 4E 47, etc. This saved me when dealing with edge cases where the API didn’t behave predictably. I use buffer.readUInt32BE() in Node.js to check magic numbers. Takes about 20 lines of code and you’ll know exactly what you’re storing.

tbh i just set all telegram photos to image/jpeg and move on. been doing this for months without issues. telegram’s compression is pretty consistent so there’s no real reason to overthink it unless you’re dealing with some weird edge case.