Inconsistent API responses for file uploads in Telegram bot

I’m experiencing inconsistent behavior when uploading files through the Telegram bot API. For instance, when I upload a text file, such as ‘example.txt’, the filename appears in the event. Here’s the JSON response I receive upon uploading a text file:

{'update_id': 154781156, 'message': {'message_id': 40, 'from': {'id': 6718326872, 'is_bot': False, 'first_name': 'Shantanu', 'last_name': 'Oak', 'username': 'soak', 'language_code': 'en'}, 'chat': {'id': 6718326872, 'first_name': 'Shantanu', 'last_name': 'Oak', 'username': 'soak', 'type': 'private'}, 'date': 1727147855, 'document': {'file_name': 'example.txt', 'mime_type': 'text/plain', 'file_id': 'FILE_ID_1', 'file_unique_id': 'UNIQUE_ID_1', 'file_size': 325405}}}

However, when I upload an image file, such as a jpg, the filename is missing from the response:

{'update_id': 154781157, 'message': {'message_id': 41, 'from': {'id': 6718326872, 'is_bot': False, 'first_name': 'Shantanu', 'last_name': 'Oak', 'username': 'soak', 'language_code': 'en'}, 'chat': {'id': 6718326872, 'first_name': 'Shantanu', 'last_name': 'Oak', 'username': 'soak', 'type': 'private'}, 'date': 1727150140, 'photo': [{'file_id': 'FILE_ID_2', 'file_unique_id': 'UNIQUE_ID_2', 'file_size': 829, 'width': 90, 'height': 50}, ...]}}

I need to retrieve the name of the image file in order to handle it properly, similar to how I receive the text file’s name.

hey alice, ive faced the same issue b4 when working with telegram bot API. For image files, the filename often isn’t included in the response. Telegram doesn’t always keep image file names. One workaround is to save the original name from ur side when uploading or rely on file IDs instead. Hope this helps!

I’ve also encountered this odd behavior when using the Telegram API. One potential solution is to incorporate a logging feature while coding, where you log the filenames before uploading them. This way, you can reference the file IDs back to your logged filenames. Another approach is to use captions with your image uploads. You can send the filename as a caption, which will then appear in the bot response. Though it’s not perfect, it might help maintain some reference to your original filenames.