How I can save a file from a stream or from an url in nodejs? I have a telegram bot that will get files from users. I'm able to get the file url or the stream but I'm not sure how to save it in a temporary directory before process.
Here is the link to the library I'm using
How I can proceed?Here is the code
#!/usr/bin/env node
const process = require('process');
const fs = require('fs');
const path = require('path');
const TelegramBot = require('node-telegram-bot-api');
const token = process.env.TELEGRAM_BOT_TOKEN || '5xxxxxxxxxxxxx';
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});
bot.onText(/\/echo(.+)/, async (msg, match) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
//const chatId = msg.chat.id;
});
bot.on('message', async (msg) => {
...
});
bot.on('document', async (data) => {
console.log(data);
// here I'm getting the file url
const fileURL = await bot.getFileLink(data.document.file_id);
// the library have a method that give the ability to get the file stream. How I save it to a file?
const stream = await bot.getFileStream(data.document.file_id);
console.log(fileURL, stream);
});
//