【问题标题】:How telegram bot can get file_id of uploaded file?电报机器人如何获取上传文件的file_id?
【发布时间】:2016-09-08 10:33:57
【问题描述】:

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

【问题讨论】:

    标签: node.js telegram telegram-bot


    【解决方案1】:

    这取决于您的 content_types ,例如:

    视频:

    message.video.file_id
    

    音频:

    message.audio.file_id
    

    照片:

    message.photo[2].file_id
    

    更多信息请查看this链接。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        除了上述答案之外,您还可以记录来自 https://api.telegram.org/bot'.BOT_TOKEN.'/getUpdates 的机器人更新或应用程序中的更新。在那里你会找到一个Json 属性,如下所示:

        {
            "update_id" = 1111111,
            "message" =
                {
                    "message_id" = 1111111,
                    "from" =
                        {
                            "id" = 111111,
                            ...
                        }
                    "chat" =
                        {
                        "id" = 111111,
                        ...
                        }
                    "date" = 111111,
                    "photo" = 
                        {
                            {
                                "file_id" = HERE IS YOU FILE ID 1,
                                "file_size" => XXXX,
                                "width" => XX,
                                "height" => XX,
                            }
                        }
                }
        }
        

        【讨论】:

          【解决方案4】:

          假设您收到一个Message,其中包含PhotoSize 数组

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

          如您所见,有一个file_id,您可以使用它通过sendPhoto 发送照片。

          如果我们假设Update 是一个对象,其中包含一个Message 对象,而该对象又提供了一个Chat 对象,其中包含一个id 初始消息来自的聊天和一个数组PhotoSize 的(请原谅我在这里使用 PHP,但这是我的主要语言......)

          $update->message->photo 是访问数组的方式。 使用某种 For 循环来迭代项目,或者如果数组不大于 1,则只访问第一个。

          之后,您可以使用结果提取file_id,并通过sendPhotophoto 参数和chat_id 参数将其作为string 发送。

          我希望这会有所帮助!

          附: Here 是我当前的 API 实现图,希望能给你带来一些清晰的理解!

          【讨论】:

            【解决方案5】:

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

            将您的文件上传到任何聊天室并将消息转发给@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
                    }
                }
            }
            

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

                context.bot.sendDocument(chat_id=update.effective_chat.id, 
                    document = "Your_FILE_ID_HERE")
            

            【讨论】:

              【解决方案6】:

              如果你使用 PHP:

              你可以把这行写成全尺寸:

              $file_id = $updates['message']['photo'][1]['file_id'];

              还有这行拇指:

              $file_id = $updates['message']['photo'][0]['file_id'];

              【讨论】:

                猜你喜欢
                • 2020-03-10
                • 1970-01-01
                • 2017-09-24
                • 2016-03-25
                • 2017-08-18
                • 2020-12-12
                • 2018-07-18
                • 1970-01-01
                • 2019-06-02
                相关资源
                最近更新 更多