gpt4 book ai didi

api - 如何下载发送到 Telegram 机器人的文件或照片?

转载 作者:行者123 更新时间:2023-12-01 22:20:40 35 4
gpt4 key购买 nike

我正在使用电报机器人 API,但无论如何我都看不到下载发送到我的机器人的文件。我得到了文件的哈希值,但不知道如何处理它。有什么办法吗?谢谢。

最佳答案

现在可用!

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

万岁!于 2015 年 9 月 18 日添加:

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

用法:

在消息的 JSON 中,您将像以前一样收到 file_id。带有语音文件的消息对象示例:

{
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,您现在可以获得文件所需的路径信息:

https://api.telegram.org/bot<bot_token>/getFile?file_id=the_file_id

这将返回一个带有 file_idfile_sizefile_path 的对象。然后您可以使用file_path下载文件:

https://api.telegram.org/file/bot<token>/<file_path>

请注意,此链接仅可用一个小时。一小时后,您可以请求另一个链接。这意味着,如果您想以某种方式托管该文件,并且您宁愿避免每次提供该文件时检查并重新检查新链接,您最好将该文件下载到您自己的托管服务。

通过此方法获取的文件最大大小为20MB。错误:使用大于20mb的文件时获取。(如下所示)

{"ok":false,"error_code":400,"description":"Bad Request: file is too big[size:1556925644]"}

来自电报的文档:

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.

关于api - 如何下载发送到 Telegram 机器人的文件或照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31096358/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com