Add sending media files

This commit is contained in:
Nur Muhammad
2020-10-13 14:49:40 +08:00
parent d73bbc6507
commit 30609891d3
5 changed files with 81 additions and 1 deletions

37
app.js
View File

@@ -1,4 +1,4 @@
const { Client } = require('whatsapp-web.js');
const { Client, MessageMedia } = require('whatsapp-web.js');
const express = require('express');
const { body, validationResult } = require('express-validator');
const socketIO = require('socket.io');
@@ -6,6 +6,8 @@ const qrcode = require('qrcode');
const http = require('http');
const fs = require('fs');
const { phoneNumberFormatter } = require('./helpers/formatter');
const fileUpload = require('express-fileupload');
const axios = require('axios');
const app = express();
const server = http.createServer(app);
@@ -13,6 +15,9 @@ const io = socketIO(server);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(fileUpload({
debug: true
}));
const SESSION_FILE_PATH = './whatsapp-session.json';
let sessionCfg;
@@ -138,6 +143,36 @@ app.post('/send-message', [
});
});
// Send media
app.post('/send-media', async (req, res) => {
const number = phoneNumberFormatter(req.body.number);
const caption = req.body.caption;
const fileUrl = req.body.file;
// const media = MessageMedia.fromFilePath('./image-example.png');
// const file = req.files.file;
// const media = new MessageMedia(file.mimetype, file.data.toString('base64'), file.name);
let mimetype;
const attachment = await axios.get(fileUrl, { responseType: 'arraybuffer' }).then(response => {
mimetype = response.headers['content-type'];
return response.data.toString('base64');
});
const media = new MessageMedia(mimetype, attachment, 'Media');
client.sendMessage(number, media, { caption: caption }).then(response => {
res.status(200).json({
status: true,
response: response
});
}).catch(err => {
res.status(500).json({
status: false,
response: err
});
});
});
server.listen(8000, function() {
console.log('App running on *: ' + 8000);
});

BIN
image-example.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

42
package-lock.json generated
View File

@@ -71,6 +71,14 @@
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
},
"axios": {
"version": "0.20.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz",
"integrity": "sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"backo2": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
@@ -198,6 +206,14 @@
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A=="
},
"busboy": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/busboy/-/busboy-0.3.1.tgz",
"integrity": "sha512-y7tTxhGKXcyBxRKAni+awqx8uqaJKrSFSNFSeRG5CsWNdmy2BIK+6VGWEW7TZnIO/533mtMEA4rOevQV815YJw==",
"requires": {
"dicer": "0.3.0"
}
},
"bytes": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
@@ -319,6 +335,14 @@
"resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.799653.tgz",
"integrity": "sha512-t1CcaZbvm8pOlikqrsIM9GOa7Ipp07+4h/q9u0JXBWjPCjHdBl9KkddX87Vv9vBHoBGtwV79sYQNGnQM6iS5gg=="
},
"dicer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dicer/-/dicer-0.3.0.tgz",
"integrity": "sha512-MdceRRWqltEG2dZqO769g27N/3PXfcKl04VhYnBlo2YhH7zPi88VebsjTKclaOyiuMaGU72hTfw3VkUitGcVCA==",
"requires": {
"streamsearch": "0.1.2"
}
},
"dijkstrajs": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dijkstrajs/-/dijkstrajs-1.0.1.tgz",
@@ -510,6 +534,14 @@
}
}
},
"express-fileupload": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.2.0.tgz",
"integrity": "sha512-oe4WpKcSppXnl5peornawWUa6tKmIc1/kJxMNRGJR1A0v4zyLL6VsFR6wZ8P2a4Iq3aGx8xae3Vlr+MOMQhFPw==",
"requires": {
"busboy": "^0.3.1"
}
},
"express-validator": {
"version": "6.6.1",
"resolved": "https://registry.npmjs.org/express-validator/-/express-validator-6.6.1.tgz",
@@ -576,6 +608,11 @@
"path-exists": "^4.0.0"
}
},
"follow-redirects": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz",
"integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA=="
},
"forwarded": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
@@ -1191,6 +1228,11 @@
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
},
"streamsearch": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
"integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo="
},
"string-width": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",

View File

@@ -14,7 +14,9 @@
"author": "Nur Muhammad",
"license": "MIT",
"dependencies": {
"axios": "^0.20.0",
"express": "^4.17.1",
"express-fileupload": "^1.2.0",
"express-validator": "^6.6.1",
"http": "0.0.1-security",
"qrcode": "^1.4.4",

View File

@@ -6,6 +6,7 @@ Watch the tutorials:
- <a href="https://youtu.be/IRRiN2ZQDc8">Whatsapp API Tutorial: Part 1</a>
- <a href="https://youtu.be/hYpRQ_FE1JI">Whatsapp API Tutorial: Part 2</a>
- <a href="https://youtu.be/uBu7Zfba1zA">Whatsapp API Tutorial: Tips & Tricks</a>
- <a href="https://youtu.be/ksVBXF-6Jtc">Whatsapp API Tutorial: Sending Media File</a>
### How to use?
- Clone or download this repo