Integrating with Express.js & Socket.io

This commit is contained in:
Nur Muhammad
2020-10-08 21:17:15 +08:00
parent 3fe6bf4f16
commit f3fe3d8f78
5 changed files with 1095 additions and 22 deletions

87
app.js
View File

@@ -1,34 +1,29 @@
const { Client } = require('whatsapp-web.js'); const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal'); const express = require('express');
const socketIO = require('socket.io');
const qrcode = require('qrcode');
const http = require('http');
const fs = require('fs'); const fs = require('fs');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const SESSION_FILE_PATH = './whatsapp-session.json'; const SESSION_FILE_PATH = './whatsapp-session.json';
let sessionCfg; let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) { if (fs.existsSync(SESSION_FILE_PATH)) {
sessionCfg = require(SESSION_FILE_PATH); sessionCfg = require(SESSION_FILE_PATH);
} }
app.get('/', (req, res) => {
res.sendFile('index.html', { root: __dirname });
});
const client = new Client({ puppeteer: { headless: true }, session: sessionCfg }); const client = new Client({ puppeteer: { headless: true }, session: sessionCfg });
client.on('qr', (qr) => {
console.log('QR RECEIVED', qr);
qrcode.generate(qr);
});
client.on('authenticated', (session) => {
console.log('AUTHENTICATED', session);
sessionCfg=session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
if (err) {
console.error(err);
}
});
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', msg => { client.on('message', msg => {
if (msg.body == '!ping') { if (msg.body == '!ping') {
msg.reply('pong'); msg.reply('pong');
@@ -38,3 +33,55 @@ client.on('message', msg => {
}); });
client.initialize(); client.initialize();
// Socket IO
io.on('connection', function(socket) {
socket.emit('message', 'Connecting...');
client.on('qr', (qr) => {
console.log('QR RECEIVED', qr);
qrcode.toDataURL(qr, (err, url) => {
socket.emit('qr', url);
socket.emit('message', 'QR Code received, scan please!');
});
});
client.on('ready', () => {
socket.emit('ready', 'Whatsapp is ready!');
socket.emit('message', 'Whatsapp is ready!');
});
client.on('authenticated', (session) => {
socket.emit('authenticated', 'Whatsapp is authenticated!');
socket.emit('message', 'Whatsapp is authenticated!');
console.log('AUTHENTICATED', session);
sessionCfg=session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
if (err) {
console.error(err);
}
});
});
});
// Send message
app.post('/send-message', (req, res) => {
const number = req.body.number;
const message = req.body.message;
client.sendMessage(number, message).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);
});

40
index.html Normal file
View File

@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<title>Whatsapp API by Ngekoding</title>
</head>
<body>
<div id="app">
<h1>Whatsapp API</h1>
<p>Powered by Ngekoding</p>
<img src="" alt="QR Code" id="qrcode">
<h3>Logs:</h3>
<ul class="logs"></ul>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.1/socket.io.min.js" integrity="sha512-/WwtKR6NnHomLo0O4w9QKc1INTPEJs7ko6u2aBTA1paPldhPl8LtXsi7a35iEZ69+9P5dcgVNESG8hrP4Y2t3w==" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
var socket = io.connect('http://localhost:8000', { path: '/socket.io' });
socket.on('message', function(msg) {
$('.logs').append($('<li>').text(msg));
});
socket.on('qr', function(src) {
$('#qrcode').attr('src', src);
});
socket.on('ready', function(data) {
$('#qrcode').hide();
});
socket.on('authenticated', function(data) {
$('#qrcode').hide();
});
});
</script>
</body>
</html>

3
nodemon.json Normal file
View File

@@ -0,0 +1,3 @@
{
"ignore": ["whatsapp-session.json"]
}

979
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,7 @@
"description": "Whatsapp api by Ngekoding", "description": "Whatsapp api by Ngekoding",
"main": "app.js", "main": "app.js",
"scripts": { "scripts": {
"start": "node app.js", "start": "nodemon app.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"keywords": [ "keywords": [
@@ -14,7 +14,11 @@
"author": "Nur Muhammad", "author": "Nur Muhammad",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"express": "^4.17.1",
"http": "0.0.1-security",
"qrcode": "^1.4.4",
"qrcode-terminal": "^0.12.0", "qrcode-terminal": "^0.12.0",
"socket.io": "^2.3.0",
"whatsapp-web.js": "^1.8.2" "whatsapp-web.js": "^1.8.2"
} }
} }