如何在电报Bot中从用户下载音频消息?

问题描述

一天中的昂贵时间。有一个问题。我想制作一个电报机器人,它能够读取用户的语音消息并将其下载到服务器。问题是,如何使漫游器自动理解音频已发送给它并保存到指定目录? 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和路径的文件

解决方法

您可以使用 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)})
  })
})

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...