Retrieving MIME type for images sent to Telegram bot

I’m building a Telegram bot that needs to handle image files sent by users. My bot stores the file IDs from received images and videos for later download, but I’m running into an issue with getting the MIME type for photos.

The problem is that when I look at the PhotoSize object structure, it doesn’t include MIME type information like other file types do. For example, video files have a mime_type field, but photos don’t seem to have this.

I need to store the MIME type because the Telegram documentation warns that the getFile method might not preserve the original file format when downloading. How can I determine the MIME type for photo files that users send to my bot?

Has anyone found a workaround for this limitation? I’m using a Node.js library for the bot implementation.

Same issue here. Telegram strips all the MIME data from photos, which sucks. I just check the file extension after download - most photos are .jpg anyway. If you need more accuracy, you can read the first few bytes of the file to detect the format before saving it.

I encountered this same issue while developing a Telegram bot. The absence of MIME type data in PhotoSize objects is indeed frustrating. My approach was to default to ‘image/jpeg,’ which covers about 95% of images received since Telegram usually serves JPEGs when using getFile. For the few exceptions, I would download the file and inspect the header to determine the actual format. While this method does involve some additional processing, it proved effective for ensuring correct MIME types in most situations.

Here’s what worked for me - I use a two-step approach when downloading files. Telegram’s getFile endpoint returns files with predictable extensions (.jpg for photos), so I grab the file_path from the response and extract the extension from there. Way more reliable than inspecting file headers. I just keep a simple extension-to-MIME-type mapping in my code. The trick is that even though PhotoSize doesn’t give you MIME info, the actual file path from getFile usually has the right extension. Been using this method for my bot that handles thousands of images daily - works like a charm.