简体   繁体   中英

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

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

You can use axios and telegram.getFileLink() : (This is a telegraf example)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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