mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 11:26:13 +00:00
Add example for downloading message media
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
whatsapp-session*.json
|
||||
package-lock.json
|
||||
package-lock.json
|
||||
downloaded-media/
|
||||
40
app.js
40
app.js
@@ -8,6 +8,9 @@ const fs = require('fs');
|
||||
const { phoneNumberFormatter } = require('./helpers/formatter');
|
||||
const fileUpload = require('express-fileupload');
|
||||
const axios = require('axios');
|
||||
const mime = require('mime-types');
|
||||
const { Z_ERRNO } = require('zlib');
|
||||
|
||||
const port = process.env.PORT || 8000;
|
||||
|
||||
const app = express();
|
||||
@@ -73,6 +76,43 @@ client.on('message', msg => {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Downloading media
|
||||
if (msg.hasMedia) {
|
||||
msg.downloadMedia().then(media => {
|
||||
// To better understanding
|
||||
// Please look at the console what data we get
|
||||
console.log(media);
|
||||
|
||||
if (media) {
|
||||
// The folder to store: change as you want!
|
||||
// Create if not exists
|
||||
const mediaPath = './downloaded-media/';
|
||||
|
||||
if (!fs.existsSync(mediaPath)) {
|
||||
fs.mkdirSync(mediaPath);
|
||||
}
|
||||
|
||||
// Get the file extension by mime-type
|
||||
const extension = mime.extension(media.mimetype);
|
||||
|
||||
// Filename: change as you want!
|
||||
// I will use the time for this example
|
||||
// Why not use media.filename? Because the value is not certain exists
|
||||
const filename = new Date().getTime();
|
||||
|
||||
const fullFilename = mediaPath + filename + '.' + extension;
|
||||
|
||||
// Save to file
|
||||
try {
|
||||
fs.writeFileSync(fullFilename, media.data, { encoding: 'base64' });
|
||||
console.log('File downloaded successfully!', fullFilename);
|
||||
} catch (err) {
|
||||
console.log('Failed to save the file:', err);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
client.initialize();
|
||||
|
||||
@@ -20,13 +20,11 @@
|
||||
"express-fileupload": "^1.2.0",
|
||||
"express-validator": "^6.9.2",
|
||||
"http": "0.0.1-security",
|
||||
"mime-types": "^2.1.32",
|
||||
"qrcode": "^1.4.4",
|
||||
"socket.io": "2.3.0",
|
||||
"whatsapp-web.js": "latest"
|
||||
},
|
||||
"engines": {
|
||||
"node": "v12.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
}
|
||||
|
||||
@@ -42,6 +42,14 @@ Here the way to get the groups info (including ID & name):
|
||||
- The API will replying with the groups info
|
||||
- Use the ID to send a message
|
||||
|
||||
### Downloading media
|
||||
|
||||
I add an example to downloading the message media if exists. Please check it in `on message` event!
|
||||
|
||||
We use `mime-types` package to get the file extension by it's mimetype, so we can download all of the type of media message.
|
||||
|
||||
And we decided (for this example) to use time as the filename, because the media filename is not certain exists.
|
||||
|
||||
## Support Me
|
||||
|
||||
You can make a support for this work by <a href="https://karyakarsa.com/ngekoding/">Karya Karsa</a>. Thanks
|
||||
|
||||
Reference in New Issue
Block a user