【问题标题】:How to download an audio message from a user in a telegram bot?如何在电报机器人中从用户那里下载音频消息?
【发布时间】:2020-12-05 17:59:30
【问题描述】:

一天中的昂贵时间。有一个问题。我想制作一个能够读取用户语音消息并将其下载到服务器的电报机器人。问题是,如何让机器人自动理解音频被发送到它并保存到指定目录? P.S 试过了

bot.on('voice', (msg)=>{
        bot.getFile(msg.voice.file_id, () => {
            bot.getFile(voiceId).then((resp) => {
                {
                    file_id = 'file_id',
                        file_size = 6666,
                    file_path = 'file_path'
                }
                bot.getFileLink(voiceId).then((resp) => {
                    'https://api.telegram.org/file/bot<BOT_TOKEN>/<file_path>'
                });
            });
        });

但是文件只下载了我指定的id和路径

【问题讨论】:

    标签: javascript node.js audio bots telegram


    【解决方案1】:

    您可以使用axiostelegram.getFileLink():(这是telegraf 示例)

    bot.on("voice",ctx => {
      ctx.telegram.getFileLink(ctx.message.vioce.file_id).then((url) => {
          axios.get(url, { responseType: "arraybuffer" }).then((voice) => {
          fs.writefile(`./lib/voices/${ctx.from.id}/${ctx.message.vioce.file_id}.ogg`,voice)})
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 2021-03-09
      • 1970-01-01
      • 2020-08-25
      • 2019-09-16
      • 2021-10-12
      • 2022-11-08
      • 2023-02-02
      • 2018-01-12
      相关资源
      最近更新 更多