mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 19:36:59 +00:00
Integrating with Express.js & Socket.io
This commit is contained in:
89
app.js
89
app.js
@@ -1,34 +1,29 @@
|
||||
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 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';
|
||||
let sessionCfg;
|
||||
if (fs.existsSync(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 });
|
||||
|
||||
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 => {
|
||||
if (msg.body == '!ping') {
|
||||
msg.reply('pong');
|
||||
@@ -37,4 +32,56 @@ 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
40
index.html
Normal 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
3
nodemon.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"ignore": ["whatsapp-session.json"]
|
||||
}
|
||||
979
package-lock.json
generated
979
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@
|
||||
"description": "Whatsapp api by Ngekoding",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"start": "nodemon app.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [
|
||||
@@ -14,7 +14,11 @@
|
||||
"author": "Nur Muhammad",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.17.1",
|
||||
"http": "0.0.1-security",
|
||||
"qrcode": "^1.4.4",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"socket.io": "^2.3.0",
|
||||
"whatsapp-web.js": "^1.8.2"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user