电报机器人如何获取上载文件的file_id?

Ars*_*ler 6 node.js telegram telegram-bot

在电报API文档中,我看到:“您可以将file_id作为String 传递,以重新发送已经在电报服务器上的照片”,但是我找不到获取上载文件的file_id的方法。我怎么才能得到它?

Yip*_*Hon 6

这是我发现的最简单的方法。

将您的文件上传到任何聊天并将消息转发到@RawDataBot。它会返回类似这样的内容:

{
    "update_id": 754677603,
    "message": {
        "message_id": 403656,
        "from": {
            "id": xxx,
            "is_bot": false,
            "first_name": "xxx",
            "username": "xxx",
            "language_code": "en"
        },
        "chat": {
            "id": xxx,
            "first_name": "xxx",
            "username": "xxx",
            "type": "private"
        },
        "date": 1589342513,
        "forward_from": {
            "id": xxx,
            "is_bot": false,
            "first_name": "xxx",
            "username": "xxx",
            "language_code": "en"
        },
        "forward_date": 1589342184,
        "document": {
            "file_name": "filename.pdf",
            "mime_type": "application/pdf",
            "file_id": "This_Is_The_Thing_You_Need",
            "file_unique_id": "notthis",
            "file_size": 123605
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您需要的是 下的字符串file_id。复制后,您只需使用以下代码即可发送消息。

    context.bot.sendDocument(chat_id=update.effective_chat.id, 
        document = "Your_FILE_ID_HERE")
Run Code Online (Sandbox Code Playgroud)


Far*_*ian 5

它取决于您的content_types,例如:

视频:

message.video.file_id
Run Code Online (Sandbox Code Playgroud)

音讯:

message.audio.file_id
Run Code Online (Sandbox Code Playgroud)

照片:

message.photo[2].file_id
Run Code Online (Sandbox Code Playgroud)

欲了解更多看到这个链接。


ari*_*aby 4

根据您选择发送文件的方法(文件类型),将文件发送到 Telegram 后会返回响应。例如,如果您使用sendAudio方法将 MP3 文件发送到 Telegram,Telegram 将返回一个Audio包含文件 ID 的对象。来源: https: //core.telegram.org/bots/api#audio