Answer the question
In order to leave comments, you need to log in
How to organize the download of an audio file as a stream from telegram in express.js using axios?
I have a handler (middleware) that receives the ID of an audio file from telegram as input.
router.get('/api/track/:id', async(req, res) => {
// Создаем бота использую плагин Telegraf с прокси агентом
const telegram = new Telegram(token, { agent });
const fileId = req.params.id;
let url; // Сюда запишем ссылку
try {
url = await telegram.getFileLink(fileId); // Получаем ссылку на файл
} catch (error) {
console.log(error);
}
// ...
}
res.set('content-type', 'audio/mp3');
res.set('accept-ranges', 'bytes');
try {
let audioFile = await axios({
url, // Ссылка на файл в телеграме
method: 'GET',
responseType: 'stream'
})
audioFile.data.pipe(res)
audioFile.on('error', err => {
res.statusCode = 500
res.end("Server Error")
console.error(err)
});
audioFile
.on('open', (chunk) => {
console.log("open")
res.write(chunk)
})
.on('close', () => {
console.log("close")
res.end()
})
res.on('close', () => {
audioFile.destroy();
})
} catch (error) {
console.log(error)
}
Answer the question
In order to leave comments, you need to log in
I found the problem, it turned out that I completely forgot about the fact that you also need to attach a proxy to the axios, since it downloads the file from the telegram ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question