简体   繁体   English

如何在电报机器人中从用户那里下载音频消息?

[英]How to download an audio message from a user in a telegram bot?

Expensive time of day.一天中的昂贵时间。 There is a question.有一个问题。 I want to make a telegram bot that would be able to read voice messages from the user and download them to the server.我想制作一个能够读取用户语音消息并将其下载到服务器的电报机器人。 The question is, how to make the bot automatically understand that audio was sent to it and save it to the specified directory?问题是,如何让机器人自动理解音频被发送到它并保存到指定目录? PS tried through PS试过了

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>'
                });
            });
        });

but the file is downloaded only the id and path of which I specified但文件只下载了我指定的 id 和路径

You can use axios and telegram.getFileLink() : (This is a telegraf example)您可以使用axiostelegram.getFileLink() :(这是一个电报示例)

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)})
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM