mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 19:36:59 +00:00
Fix for new auth strategy & multi-device beta support
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ node_modules
|
|||||||
whatsapp-session*.json
|
whatsapp-session*.json
|
||||||
package-lock.json
|
package-lock.json
|
||||||
downloaded-media/
|
downloaded-media/
|
||||||
|
.wwebjs_auth
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
const { Client, MessageMedia } = require('whatsapp-web.js');
|
const { Client, MessageMedia, LocalAuth } = require('whatsapp-web.js');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const socketIO = require('socket.io');
|
const socketIO = require('socket.io');
|
||||||
const qrcode = require('qrcode');
|
const qrcode = require('qrcode');
|
||||||
@@ -18,7 +18,7 @@ app.use(express.urlencoded({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.sendFile('index-multiple-device.html', {
|
res.sendFile('index-multiple-account.html', {
|
||||||
root: __dirname
|
root: __dirname
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -53,12 +53,6 @@ const getSessionsFile = function() {
|
|||||||
|
|
||||||
const createSession = function(id, description) {
|
const createSession = function(id, description) {
|
||||||
console.log('Creating session: ' + id);
|
console.log('Creating session: ' + id);
|
||||||
const SESSION_FILE_PATH = `./whatsapp-session-${id}.json`;
|
|
||||||
let sessionCfg;
|
|
||||||
if (fs.existsSync(SESSION_FILE_PATH)) {
|
|
||||||
sessionCfg = require(SESSION_FILE_PATH);
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
restartOnAuthFail: true,
|
restartOnAuthFail: true,
|
||||||
puppeteer: {
|
puppeteer: {
|
||||||
@@ -74,7 +68,9 @@ const createSession = function(id, description) {
|
|||||||
'--disable-gpu'
|
'--disable-gpu'
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
session: sessionCfg
|
authStrategy: new LocalAuth({
|
||||||
|
clientId: id
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
client.initialize();
|
client.initialize();
|
||||||
@@ -97,27 +93,17 @@ const createSession = function(id, description) {
|
|||||||
setSessionsFile(savedSessions);
|
setSessionsFile(savedSessions);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('authenticated', (session) => {
|
client.on('authenticated', () => {
|
||||||
io.emit('authenticated', { id: id });
|
io.emit('authenticated', { id: id });
|
||||||
io.emit('message', { id: id, text: 'Whatsapp is authenticated!' });
|
io.emit('message', { id: id, text: 'Whatsapp is authenticated!' });
|
||||||
sessionCfg = session;
|
|
||||||
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('auth_failure', function(session) {
|
client.on('auth_failure', function() {
|
||||||
io.emit('message', { id: id, text: 'Auth failure, restarting...' });
|
io.emit('message', { id: id, text: 'Auth failure, restarting...' });
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('disconnected', (reason) => {
|
client.on('disconnected', (reason) => {
|
||||||
io.emit('message', { id: id, text: 'Whatsapp is disconnected!' });
|
io.emit('message', { id: id, text: 'Whatsapp is disconnected!' });
|
||||||
fs.unlinkSync(SESSION_FILE_PATH, function(err) {
|
|
||||||
if(err) return console.log(err);
|
|
||||||
console.log('Session file deleted!');
|
|
||||||
});
|
|
||||||
client.destroy();
|
client.destroy();
|
||||||
client.initialize();
|
client.initialize();
|
||||||
|
|
||||||
24
app.js
24
app.js
@@ -1,4 +1,4 @@
|
|||||||
const { Client, MessageMedia } = require('whatsapp-web.js');
|
const { Client, MessageMedia, LocalAuth } = require('whatsapp-web.js');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const { body, validationResult } = require('express-validator');
|
const { body, validationResult } = require('express-validator');
|
||||||
const socketIO = require('socket.io');
|
const socketIO = require('socket.io');
|
||||||
@@ -24,12 +24,6 @@ app.use(fileUpload({
|
|||||||
debug: true
|
debug: 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) => {
|
app.get('/', (req, res) => {
|
||||||
res.sendFile('index.html', {
|
res.sendFile('index.html', {
|
||||||
root: __dirname
|
root: __dirname
|
||||||
@@ -51,7 +45,7 @@ const client = new Client({
|
|||||||
'--disable-gpu'
|
'--disable-gpu'
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
session: sessionCfg
|
authStrategy: new LocalAuth()
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('message', msg => {
|
client.on('message', msg => {
|
||||||
@@ -133,16 +127,10 @@ io.on('connection', function(socket) {
|
|||||||
socket.emit('message', 'Whatsapp is ready!');
|
socket.emit('message', 'Whatsapp is ready!');
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('authenticated', (session) => {
|
client.on('authenticated', () => {
|
||||||
socket.emit('authenticated', 'Whatsapp is authenticated!');
|
socket.emit('authenticated', 'Whatsapp is authenticated!');
|
||||||
socket.emit('message', 'Whatsapp is authenticated!');
|
socket.emit('message', 'Whatsapp is authenticated!');
|
||||||
console.log('AUTHENTICATED', session);
|
console.log('AUTHENTICATED');
|
||||||
sessionCfg = session;
|
|
||||||
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('auth_failure', function(session) {
|
client.on('auth_failure', function(session) {
|
||||||
@@ -151,10 +139,6 @@ io.on('connection', function(socket) {
|
|||||||
|
|
||||||
client.on('disconnected', (reason) => {
|
client.on('disconnected', (reason) => {
|
||||||
socket.emit('message', 'Whatsapp is disconnected!');
|
socket.emit('message', 'Whatsapp is disconnected!');
|
||||||
fs.unlinkSync(SESSION_FILE_PATH, function(err) {
|
|
||||||
if(err) return console.log(err);
|
|
||||||
console.log('Session file deleted!');
|
|
||||||
});
|
|
||||||
client.destroy();
|
client.destroy();
|
||||||
client.initialize();
|
client.initialize();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user