From 2b7756b76443ca849a90cd5123f92d205cdbdad0 Mon Sep 17 00:00:00 2001 From: Nur Muhammad Date: Mon, 2 Aug 2021 15:43:45 +0800 Subject: [PATCH] Add example for downloading message media --- .gitignore | 3 ++- app.js | 40 ++++++++++++++++++++++++++++++++++++++++ package.json | 4 +--- readme.md | 8 ++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 91c6576..d8155c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules whatsapp-session*.json -package-lock.json \ No newline at end of file +package-lock.json +downloaded-media/ \ No newline at end of file diff --git a/app.js b/app.js index f8dfb40..cdb6e35 100644 --- a/app.js +++ b/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(); diff --git a/package.json b/package.json index 7cd34be..21017fe 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/readme.md b/readme.md index 34a427d..72587a0 100644 --- a/readme.md +++ b/readme.md @@ -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 Karya Karsa. Thanks