怎样下载已发送给我的电报BOT文件或照片? [英] How do I download a file or photo that was sent to my Telegram bot?

查看:946
本文介绍了怎样下载已发送给我的电报BOT文件或照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用电报机器人API,但我不能看到反正下载已发送到我的机器人的文件。我得到的文件的哈希,但不知道怎么用它做。有什么办法?谢谢。

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.

推荐答案

这是现已上市!

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

万岁!它是在9月18日说:

Hooray! It was added on Sep 18th:

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

用法:

在消息的JSON,您将收到的 FILE_ID 如前。与语音文件的消息对象的例子:

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
  }
}

通过API的的GetFile您现在可以得到的文件所需的路径信息:

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 FILE_SIZE FILE_PATH 。然后,您可以使用 FILE_PATH 以下载文件:

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.

通过此方法获得的文件的最大尺寸为 20MB。
错误:当使用超过20MB的大文件获得(如下图所示)

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]"}

从电报的文档:

在成功时,返回的File对象。该文件然后可以经由链接 https://api.telegram.org/file/bot /,其中从所述响应取。它是保证该链接将是有效的,至少1小时。当链接到期时,一个新的可调用的GetFile again.For时刻请求,机器人可以在尺寸下载的文件备份到20MB。

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.

这篇关于怎样下载已发送给我的电报BOT文件或照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆