I am using the telegram bot API but I cant see anyway to download a filé that was sent to my bot. I get a hash of the file but dont know what to do with it. Is there any way? Thanks.
6 Answers
This is now available!
https://core.telegram.org/bots/api#getfile
Hooray! It was added on Sep 18th (2015):
https://core.telegram.org/bots/api
Usage:
In the JSON of the message you will receive a file_id as before. An example of a message object with a voice file:
{
message_id: 2675,
from: {
id: 10000001,
first_name: 'john',
username: 'john'
},
chat: {
id: 10000001,
first_name: 'john',
username: 'john'
},
date: 1442848171,
voice: {
duration: 2,
mime_type: 'audio/ogg',
file_id: 'AwADBAADYwADO1wlBuF1ogMa7HnMAg', // <------- file_id
file_size: 17746
}
}
Via the API's getFile you can now get the required path information for the file:
https://api.telegram.org/bot<bot_token>/getFile?file_id=the_file_id
This will return an object with file_id, file_size and file_path. You can then use the file_path to download the file:
https://api.telegram.org/file/bot<token>/<file_path>
Note that this link will only be available for an hour. After an hour you can request another link. This means that if you want to host the file somehow and you rather avoid checking and re-checking for fresh links every time you serve it you might be better off downloading the file to your own hosting service.
The maximum size of a file obtained through this method is 20MB. Error: Obtained when a file large than 20mb is used.(Shown below)
{"ok":false,"error_code":400,"description":"Bad Request: file is too big[size:1556925644]"}
From telegram's docs:
On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.For the moment, bots can download files of up to 20MB in size.

- 7,509
- 3
- 44
- 55

- 12,488
- 16
- 79
- 119
-
4Just to clarify, the getFile API call must include a `?file_id=the_file_id` in the GET query string. – jotadepicas Apr 02 '16 at 22:53
-
When i use /getFile i can't get file_path some time for example when file is ogg file_path is define and file downloaded properly but when file is mp3 filr_path not defined, and just return file_id and file_size! – S. Ali Mihandoost May 01 '17 at 11:31
-
1for what it's worth, the telegram docs on this are not very helpful. The helpful bits here are "https://api.telegram.org/bot
/getFile?file_id=the_file_id" and "https://api.telegram.org/file/bot – Jared Menard Sep 08 '20 at 18:47/ " -
Thank You, your answer helped me a lot. – Arsham Arya May 09 '21 at 15:09
Yay! It's just added at September 18, 2015
You can use getFile(file_id)
. This function returns a File object containing file_path
. You can download the file through this address:
https://api.telegram.org/file/bot<token>/<file_path>
As mentioned in Telegram Bot API Documentation, the File object will be valid for about one hour. You should call getFile
again to get a new File object if the old one expires.

- 1,435
- 14
- 24
-
1What is the format of the getFile endpoint? I've tried /getFile, /getFile?file_id=xxx, getFile(xxx) and they haven't worked. – Zach Smith Aug 26 '16 at 06:17
If you are using pyTelegramBotAPI you can download your photo using this code:
raw = message.photo[2].file_id
path = raw+".jpg"
file_info = bot.get_file(raw)
downloaded_file = bot.download_file(file_info.file_path)
with open(path,'wb') as new_file:
new_file.write(downloaded_file)

- 728
- 6
- 18
-
photo[2] there are 4 version of photo exist there. if i want to download smaller size [0] zero index is mine? what about php sample code? – saber tabatabaee yazdi Nov 04 '17 at 21:36
The method to work with files is not available yet. Source: telegram on twitter

- 61
- 3
If you have the file_id then you need to use the sendDocument or sendPhoto methods, if you want to send to yourself, you need to tell your bot your user id or your chat id (the same in one-to-one chat).

- 187
- 2
- 5
-
1OP is asking about receiving, not sending. Forward or re-sending the file to yourself just leaves you with the same problem. – Another Code Jun 29 '15 at 22:58
-
If the bot send the file to me then I can download from the conversation with the bot to the my phone or my computer (from telegram itself). According to your comment Arthur wants to receive the message in the bot and then the bot download the image or file to the server (where de bot is running)? – delaf Jun 30 '15 at 16:07
-
I see now what you meant. But yeah, normally you'd want to have the bot download the file automatically and process it in some way. That's the point of having a bot after all. – Another Code Jun 30 '15 at 18:30
Also, pay attention, that Telegram api (by webhook) provides thumbs prop, for images and gifs it will provide thumbnail of file. To get source file, you need to check root object file_id.

- 10,289
- 4
- 52
- 53