mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 19:36:59 +00:00
Commenting auto download message media files script
This commit is contained in:
60
app.js
60
app.js
@@ -70,42 +70,44 @@ client.on('message', msg => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE!
|
||||||
|
// UNCOMMENT THE SCRIPT BELOW IF YOU WANT TO SAVE THE MESSAGE MEDIA FILES
|
||||||
// Downloading media
|
// Downloading media
|
||||||
if (msg.hasMedia) {
|
// if (msg.hasMedia) {
|
||||||
msg.downloadMedia().then(media => {
|
// msg.downloadMedia().then(media => {
|
||||||
// To better understanding
|
// // To better understanding
|
||||||
// Please look at the console what data we get
|
// // Please look at the console what data we get
|
||||||
console.log(media);
|
// console.log(media);
|
||||||
|
|
||||||
if (media) {
|
// if (media) {
|
||||||
// The folder to store: change as you want!
|
// // The folder to store: change as you want!
|
||||||
// Create if not exists
|
// // Create if not exists
|
||||||
const mediaPath = './downloaded-media/';
|
// const mediaPath = './downloaded-media/';
|
||||||
|
|
||||||
if (!fs.existsSync(mediaPath)) {
|
// if (!fs.existsSync(mediaPath)) {
|
||||||
fs.mkdirSync(mediaPath);
|
// fs.mkdirSync(mediaPath);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Get the file extension by mime-type
|
// // Get the file extension by mime-type
|
||||||
const extension = mime.extension(media.mimetype);
|
// const extension = mime.extension(media.mimetype);
|
||||||
|
|
||||||
// Filename: change as you want!
|
// // Filename: change as you want!
|
||||||
// I will use the time for this example
|
// // I will use the time for this example
|
||||||
// Why not use media.filename? Because the value is not certain exists
|
// // Why not use media.filename? Because the value is not certain exists
|
||||||
const filename = new Date().getTime();
|
// const filename = new Date().getTime();
|
||||||
|
|
||||||
const fullFilename = mediaPath + filename + '.' + extension;
|
// const fullFilename = mediaPath + filename + '.' + extension;
|
||||||
|
|
||||||
// Save to file
|
// // Save to file
|
||||||
try {
|
// try {
|
||||||
fs.writeFileSync(fullFilename, media.data, { encoding: 'base64' });
|
// fs.writeFileSync(fullFilename, media.data, { encoding: 'base64' });
|
||||||
console.log('File downloaded successfully!', fullFilename);
|
// console.log('File downloaded successfully!', fullFilename);
|
||||||
} catch (err) {
|
// } catch (err) {
|
||||||
console.log('Failed to save the file:', err);
|
// console.log('Failed to save the file:', err);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
});
|
});
|
||||||
|
|
||||||
client.initialize();
|
client.initialize();
|
||||||
|
|||||||
Reference in New Issue
Block a user