Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download a file or photo that was sent to my Telegram bot?

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.

like image 821
Arthur Felipe Avatar asked Jun 28 '15 05:06

Arthur Felipe


People also ask

How can I get image from telegram?

Tap the Telegram icon on your Home screen or Apps menu to open Telegram. Tap the chat that contains the image. This displays all messages in the chat. Tap the image you want to save.

What is file to bot in telegram?

File to Bot allows the users to save files on the cloud with unlimited storage. You can send files in the chat and they will get stored category-wise. Uploaded files could be checked and downloaded easily through it. There is no limitation on file size and all file types are supported by the Telegram bot.

Can we download files from telegram Web?

Find the video file in the chat conversation, and right-click to see your options. It will open a drop-down menu. Click Save File As on the menu. This option will allow you to download the video file, and save it on your computer.


2 Answers

This is now available!

https://core.telegram.org/bots/api#getfile

Hooray! It was added on Sep 18th:

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/, where 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.

like image 65
Guy Avatar answered Sep 21 '22 03:09

Guy


It's just added at September 18, 2015!

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.

like image 36
zxcmehran Avatar answered Sep 21 '22 03:09

zxcmehran