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.
Question posted in Telegram API
A comprehensive official documentation can be found here.
A comprehensive official documentation can be found here.
6
Answers
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).
The method to work with files is not available yet.
Source: telegram on twitter
https://twitter.com/telegram/status/614468951926509568
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:
Via the API’s getFile you can now get the required path information for the file:
This will return an object with file_id, file_size and file_path. You can then use the file_path to download the file:
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)
From telegram’s docs:
Yay! It’s just added at September 18, 2015
You can use
getFile(file_id)
. This function returns a File object containingfile_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.If you are using pyTelegramBotAPI you can download your photo using this code:
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.