mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 19:36:59 +00:00
Add validation, formatter & some configs
This commit is contained in:
62
app.js
62
app.js
@@ -1,9 +1,11 @@
|
|||||||
const { Client } = require('whatsapp-web.js');
|
const { Client } = require('whatsapp-web.js');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const { body, validationResult } = require('express-validator');
|
||||||
const socketIO = require('socket.io');
|
const socketIO = require('socket.io');
|
||||||
const qrcode = require('qrcode');
|
const qrcode = require('qrcode');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const { phoneNumberFormatter } = require('./helpers/formatter');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
@@ -22,7 +24,22 @@ app.get('/', (req, res) => {
|
|||||||
res.sendFile('index.html', { root: __dirname });
|
res.sendFile('index.html', { root: __dirname });
|
||||||
});
|
});
|
||||||
|
|
||||||
const client = new Client({ puppeteer: { headless: true }, session: sessionCfg });
|
const client = new Client({
|
||||||
|
puppeteer: {
|
||||||
|
headless: true,
|
||||||
|
args: [
|
||||||
|
'--no-sandbox',
|
||||||
|
'--disable-setuid-sandbox',
|
||||||
|
'--disable-dev-shm-usage',
|
||||||
|
'--disable-accelerated-2d-canvas',
|
||||||
|
'--no-first-run',
|
||||||
|
'--no-zygote',
|
||||||
|
'--single-process', // <- this one doesn't works in Windows
|
||||||
|
'--disable-gpu'
|
||||||
|
],
|
||||||
|
},
|
||||||
|
session: sessionCfg
|
||||||
|
});
|
||||||
|
|
||||||
client.on('message', msg => {
|
client.on('message', msg => {
|
||||||
if (msg.body == '!ping') {
|
if (msg.body == '!ping') {
|
||||||
@@ -62,13 +79,52 @@ io.on('connection', function(socket) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.on('auth_failure', function(session) {
|
||||||
|
socket.emit('message', 'Auth failure, restarting...');
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('disconnected', (reason) => {
|
||||||
|
socket.emit('message', 'Whatsapp is disconnected!');
|
||||||
|
client.destroy();
|
||||||
|
client.initialize();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const checkRegisteredNumber = async function(number) {
|
||||||
|
const isRegistered = await client.isRegisteredUser(number);
|
||||||
|
return isRegistered;
|
||||||
|
}
|
||||||
|
|
||||||
// Send message
|
// Send message
|
||||||
app.post('/send-message', (req, res) => {
|
app.post('/send-message', [
|
||||||
const number = req.body.number;
|
body('number').notEmpty(),
|
||||||
|
body('message').notEmpty(),
|
||||||
|
], async (req, res) => {
|
||||||
|
const errors = validationResult(req).formatWith(({ msg }) => {
|
||||||
|
return msg;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!errors.isEmpty()) {
|
||||||
|
return res.status(422).json({
|
||||||
|
status: false,
|
||||||
|
message: errors.mapped()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const number = phoneNumberFormatter(req.body.number);
|
||||||
const message = req.body.message;
|
const message = req.body.message;
|
||||||
|
|
||||||
|
const isRegisteredNumber = await checkRegisteredNumber(number);
|
||||||
|
|
||||||
|
if (!isRegisteredNumber) {
|
||||||
|
return res.status(422).json({
|
||||||
|
status: false,
|
||||||
|
message: 'The number is not registered'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
client.sendMessage(number, message).then(response => {
|
client.sendMessage(number, message).then(response => {
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
status: true,
|
status: true,
|
||||||
|
|||||||
20
helpers/formatter.js
Normal file
20
helpers/formatter.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
const phoneNumberFormatter = function(number) {
|
||||||
|
// 1. Menghilangkan karakter selain angka
|
||||||
|
let formatted = number.replace(/\D/g, '');
|
||||||
|
|
||||||
|
// 2. Menghilangkan angka 0 di depan (prefix)
|
||||||
|
// Kemudian diganti dengan 62
|
||||||
|
if (formatted.startsWith('0')) {
|
||||||
|
formatted = '62' + formatted.substr(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!formatted.endsWith('@c.us')) {
|
||||||
|
formatted += '@c.us';
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatted;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
phoneNumberFormatter
|
||||||
|
}
|
||||||
19
package-lock.json
generated
19
package-lock.json
generated
@@ -510,6 +510,15 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"express-validator": {
|
||||||
|
"version": "6.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/express-validator/-/express-validator-6.6.1.tgz",
|
||||||
|
"integrity": "sha512-+MrZKJ3eGYXkNF9p9Zf7MS7NkPJFg9MDYATU5c80Cf4F62JdLBIjWxy6481tRC0y1NnC9cgOw8FuN364bWaGhA==",
|
||||||
|
"requires": {
|
||||||
|
"lodash": "^4.17.19",
|
||||||
|
"validator": "^13.1.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"extract-zip": {
|
"extract-zip": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz",
|
||||||
@@ -719,6 +728,11 @@
|
|||||||
"p-locate": "^4.1.0"
|
"p-locate": "^4.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"lodash": {
|
||||||
|
"version": "4.17.20",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
|
||||||
|
"integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
|
||||||
|
},
|
||||||
"media-typer": {
|
"media-typer": {
|
||||||
"version": "0.3.0",
|
"version": "0.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
|
||||||
@@ -1274,6 +1288,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
|
||||||
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
|
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
|
||||||
},
|
},
|
||||||
|
"validator": {
|
||||||
|
"version": "13.1.17",
|
||||||
|
"resolved": "https://registry.npmjs.org/validator/-/validator-13.1.17.tgz",
|
||||||
|
"integrity": "sha512-zL5QBoemJ3jYFb2/j38y7ljhwYGXVLUp8H6W1nVxadnAOvUOytec+L7BHh1oBQ82/TzWXHd+GSaxUWp4lROkLg=="
|
||||||
|
},
|
||||||
"vary": {
|
"vary": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
|
"express-validator": "^6.6.1",
|
||||||
"http": "0.0.1-security",
|
"http": "0.0.1-security",
|
||||||
"qrcode": "^1.4.4",
|
"qrcode": "^1.4.4",
|
||||||
"socket.io": "^2.3.0",
|
"socket.io": "^2.3.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user