mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-17 11:26:13 +00:00
Fix for new auth strategy & multi-device beta support
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
node_modules
|
||||
whatsapp-session*.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 socketIO = require('socket.io');
|
||||
const qrcode = require('qrcode');
|
||||
@@ -18,7 +18,7 @@ app.use(express.urlencoded({
|
||||
}));
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile('index-multiple-device.html', {
|
||||
res.sendFile('index-multiple-account.html', {
|
||||
root: __dirname
|
||||
});
|
||||
});
|
||||
@@ -53,12 +53,6 @@ const getSessionsFile = function() {
|
||||
|
||||
const createSession = function(id, description) {
|
||||
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({
|
||||
restartOnAuthFail: true,
|
||||
puppeteer: {
|
||||
@@ -74,7 +68,9 @@ const createSession = function(id, description) {
|
||||
'--disable-gpu'
|
||||
],
|
||||
},
|
||||
session: sessionCfg
|
||||
authStrategy: new LocalAuth({
|
||||
clientId: id
|
||||
})
|
||||
});
|
||||
|
||||
client.initialize();
|
||||
@@ -97,27 +93,17 @@ const createSession = function(id, description) {
|
||||
setSessionsFile(savedSessions);
|
||||
});
|
||||
|
||||
client.on('authenticated', (session) => {
|
||||
client.on('authenticated', () => {
|
||||
io.emit('authenticated', { id: id });
|
||||
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...' });
|
||||
});
|
||||
|
||||
client.on('disconnected', (reason) => {
|
||||
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.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 { body, validationResult } = require('express-validator');
|
||||
const socketIO = require('socket.io');
|
||||
@@ -24,12 +24,6 @@ app.use(fileUpload({
|
||||
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) => {
|
||||
res.sendFile('index.html', {
|
||||
root: __dirname
|
||||
@@ -51,7 +45,7 @@ const client = new Client({
|
||||
'--disable-gpu'
|
||||
],
|
||||
},
|
||||
session: sessionCfg
|
||||
authStrategy: new LocalAuth()
|
||||
});
|
||||
|
||||
client.on('message', msg => {
|
||||
@@ -133,16 +127,10 @@ io.on('connection', function(socket) {
|
||||
socket.emit('message', 'Whatsapp is ready!');
|
||||
});
|
||||
|
||||
client.on('authenticated', (session) => {
|
||||
client.on('authenticated', () => {
|
||||
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);
|
||||
}
|
||||
});
|
||||
console.log('AUTHENTICATED');
|
||||
});
|
||||
|
||||
client.on('auth_failure', function(session) {
|
||||
@@ -151,10 +139,6 @@ io.on('connection', function(socket) {
|
||||
|
||||
client.on('disconnected', (reason) => {
|
||||
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.initialize();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user