Closed fakhryhizballah closed 2 years ago
You should upgrade whatsapp-web.js to latest version and use one of authenticated method present here. https://wwebjs.dev/guide/authentication.html
You have to change your authentifcation mode and use localAuth. As example you can do this :
``const { Client, LocalAuth } = require("whatsapp-web.js");
console.log("Connection to Whatsapp Web Client");
const client = new Client({
puppeteer: {
executablePath: '/usr/bin/brave-browser-stable',
},
authStrategy: new LocalAuth({
clientId: "client-one"
}),
puppeteer: {
headless: false,
}
});
client.initialize();
client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
client.on('authenticated', (session) => {
console.log('WHATSAPP WEB => Authenticated');
});
client.on("ready", async () => {
console.log("WHATSAPP WEB => Ready");
resolve(client)
});
});``
This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
You have to change your authentifcation mode and use localAuth. As example you can do this :
``const { Client, LocalAuth } = require("whatsapp-web.js");
console.log("Connection to Whatsapp Web Client");
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, authStrategy: new LocalAuth({ clientId: "client-one" }), puppeteer: { headless: false, } }); client.initialize(); client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); }); client.on('authenticated', (session) => { console.log('WHATSAPP WEB => Authenticated'); }); client.on("ready", async () => { console.log("WHATSAPP WEB => Ready"); resolve(client) });
});``
This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
in which line we have to change the code.
I have same problem, idk why i can't connect now. is it because I updated the whatsapp app from my mobile phone?
You have to change your authentifcation mode and use localAuth. As example you can do this :
``const { Client, LocalAuth } = require("whatsapp-web.js");
console.log("Connection to Whatsapp Web Client");
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, authStrategy: new LocalAuth({ clientId: "client-one" }), puppeteer: { headless: false, } }); client.initialize(); client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); }); client.on('authenticated', (session) => { console.log('WHATSAPP WEB => Authenticated'); }); client.on("ready", async () => { console.log("WHATSAPP WEB => Ready"); resolve(client) });
});``
This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
Thanks this work! and this the recover session when node start again.
has the solution been found, I use the code above, it doesn't work in my code
const createSession = function(id,fk_id_users,description) { const SESSION_FILE_PATH =
./App/whatsapp/sessions/whatsapp-session-${id}.json`;
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) { sessionCfg = require(SESSION_FILE_PATH); }
const client = new Client({ restartOnAuthFail: true, puppeteer: { headless: true, args: [ '--no-sandbox', '--unhandled-rejections=strict', '--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, authStrategy: new LegacySessionAuth({ session: sessionCfg }) });
client.initialize(); let no = 1; client.on('qr', (qr) => { const savedSessions = getSessionsFile(); const ceksessionIndex = savedSessions.findIndex(sess => sess.id == id); if (!!ceksessionIndex) { console.log('true') } else { console.log('false'); }
/* ini setiap kelipatan 5 qr code akan otomatis delete*/
if (no%6 === 0) {
if (fs.existsSync(SESSION_FILE_PATH)) {
fs.unlinkSync(SESSION_FILE_PATH, function(err) {
if(err) return console.log(err);
console.log('Session file deleted!');
});
}
savedSessions.splice(sessionIndex, 1);
setSessionsFile(savedSessions);
io.emit('reset-kelipatan',{id:id, fk_id_users:fk_id_users})
console.log('ID_WA:'+id+',berhasil dihapus sudah kelipatan 5')
}
if (ceksessionIndex == 0) {
console.log(no++ + ',' +'QR RECEIVED ID_WA: '+id+', fk_id_users: ', fk_id_users);
qrcode.toDataURL(qr, (err, url) => {
io.emit('qr', { id: id, fk_id_users : fk_id_users ,src: url });
});
} else if (ceksessionIndex){
console.log(no++ + ',' +'QR RECEIVED ID_WA: '+id+', fk_id_users: ', fk_id_users);
qrcode.toDataURL(qr, (err, url) => {
io.emit('qr', { id: id, fk_id_users : fk_id_users ,src: url });
});
}
client.removeAllListeners('qr');
});
client.on('message', (message) => { console.log('Message Recived'); io.emit('menerima-pesan','ada pesan goblok'); });
client.on('ready', () => { io.emit('ready', { id: id, fk_id_users : fk_id_users ,description: description }); // io.emit('message', { id: id, text: 'Whatsapp is ready!' }); // io.emit('wa-ready','Wa Ready id: '+ id + ', Description: ' + description); console.log('Whatsapp Ready id: '+ id + ', fk_id_users:' + fk_id_users + ', Description: ' + description);
try {
const savedSessions = getSessionsFile();
const sessionIndex = savedSessions.findIndex(sess => sess.id == id);
savedSessions[sessionIndex].ready = true;
setSessionsFile(savedSessions);
} catch (error) {
if (fs.existsSync(SESSION_FILE_PATH)) {
/* untuk menghandle ketika barcode masih ada ketika sudah disconnect */
fs.unlinkSync(SESSION_FILE_PATH, function(err) {
if(err) return console.log(err);
console.log('Delete error but delete file hehe')
});
}
}
});
client.on('authenticated', (session) => { 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) { io.emit('message', { id: id, text: 'Auth failure, restarting...' }); });
client.on('disconnected', (reason) => { io.emit('message', { id: id, text: 'Whatsapp is disconnected!' }); console.log('Whatsapp Disconnected:' + id); if (fs.existsSync(SESSION_FILE_PATH)) { fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Session file deleted!'); }); }
client.destroy();
client.initialize();
// Menghapus pada file sessions
try {
const savedSessions = getSessionsFile();
const sessionIndex = savedSessions.findIndex(sess => sess.id == id);
savedSessions.splice(sessionIndex, 1);
setSessionsFile(savedSessions);
} catch (error) {
console.log('disconnect error:'+id)
}
io.emit('remove-session', id);
});
// Tambahkan client ke sessions sessions.push({ id: id, fk_id_users :fk_id_users, description: description, client: client });
// Menambahkan session ke file const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id);
if (sessionIndex == -1) { savedSessions.push({ id: id, fk_id_users:fk_id_users, description: description, ready: false, }); setSessionsFile(savedSessions); } } `
has the solution been found, I use the code above, it doesn't work in my code
const createSession = function(id,fk_id_users,description) { const SESSION_FILE_PATH =
./App/whatsapp/sessions/whatsapp-session-${id}.json`; let sessionCfg;if (fs.existsSync(SESSION_FILE_PATH)) { sessionCfg = require(SESSION_FILE_PATH); }
const client = new Client({ restartOnAuthFail: true, puppeteer: { headless: true, args: [ '--no-sandbox', '--unhandled-rejections=strict', '--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, authStrategy: new LegacySessionAuth({ session: sessionCfg }) });
client.initialize(); let no = 1; client.on('qr', (qr) => { const savedSessions = getSessionsFile(); const ceksessionIndex = savedSessions.findIndex(sess => sess.id == id); if (!!ceksessionIndex) { console.log('true') } else { console.log('false'); }
/* ini setiap kelipatan 5 qr code akan otomatis delete*/ if (no%6 === 0) { if (fs.existsSync(SESSION_FILE_PATH)) { fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Session file deleted!'); }); } savedSessions.splice(sessionIndex, 1); setSessionsFile(savedSessions); io.emit('reset-kelipatan',{id:id, fk_id_users:fk_id_users}) console.log('ID_WA:'+id+',berhasil dihapus sudah kelipatan 5') } if (ceksessionIndex == 0) { console.log(no++ + ',' +'QR RECEIVED ID_WA: '+id+', fk_id_users: ', fk_id_users); qrcode.toDataURL(qr, (err, url) => { io.emit('qr', { id: id, fk_id_users : fk_id_users ,src: url }); }); } else if (ceksessionIndex){ console.log(no++ + ',' +'QR RECEIVED ID_WA: '+id+', fk_id_users: ', fk_id_users); qrcode.toDataURL(qr, (err, url) => { io.emit('qr', { id: id, fk_id_users : fk_id_users ,src: url }); }); } client.removeAllListeners('qr');
});
client.on('message', (message) => { console.log('Message Recived'); io.emit('menerima-pesan','ada pesan goblok'); });
client.on('ready', () => { io.emit('ready', { id: id, fk_id_users : fk_id_users ,description: description }); // io.emit('message', { id: id, text: 'Whatsapp is ready!' }); // io.emit('wa-ready','Wa Ready id: '+ id + ', Description: ' + description); console.log('Whatsapp Ready id: '+ id + ', fk_id_users:' + fk_id_users + ', Description: ' + description);
try { const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id); savedSessions[sessionIndex].ready = true; setSessionsFile(savedSessions); } catch (error) { if (fs.existsSync(SESSION_FILE_PATH)) { /* untuk menghandle ketika barcode masih ada ketika sudah disconnect */ fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Delete error but delete file hehe') }); } }
});
client.on('authenticated', (session) => { 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) { io.emit('message', { id: id, text: 'Auth failure, restarting...' }); });
client.on('disconnected', (reason) => { io.emit('message', { id: id, text: 'Whatsapp is disconnected!' }); console.log('Whatsapp Disconnected:' + id); if (fs.existsSync(SESSION_FILE_PATH)) { fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Session file deleted!'); }); }
client.destroy(); client.initialize(); // Menghapus pada file sessions try { const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id); savedSessions.splice(sessionIndex, 1); setSessionsFile(savedSessions); } catch (error) { console.log('disconnect error:'+id) } io.emit('remove-session', id);
});
// Tambahkan client ke sessions sessions.push({ id: id, fk_id_users :fk_id_users, description: description, client: client });
// Menambahkan session ke file const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id);
if (sessionIndex == -1) { savedSessions.push({ id: id, fk_id_users:fk_id_users, description: description, ready: false, }); setSessionsFile(savedSessions); } } `
i was try LegacySessionAuth but not work to, then i move to localauth, everything work fine
You have to change your authentifcation mode and use localAuth. As example you can do this :
``const { Client, LocalAuth } = require("whatsapp-web.js");
console.log("Connection to Whatsapp Web Client");
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, authStrategy: new LocalAuth({ clientId: "client-one" }), puppeteer: { headless: false, } }); client.initialize(); client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); }); client.on('authenticated', (session) => { console.log('WHATSAPP WEB => Authenticated'); }); client.on("ready", async () => { console.log("WHATSAPP WEB => Ready"); resolve(client) });
});``
This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
use pm2 to keep your node is running, install npm install pm2 -g
and how to usage: [https://www.npmjs.com/package/pm2](from here!)
If you want to use LegacySessionAuth (basically you don't need to change anything in your server code) but still want to be able to connect to multi-device numbers, you can comment the following line in src/authStrategies/LegacySessionAuth.js
in which the Error is thrown:
I decided that a console.log would be better intead of an error. This solution works well with the previous and the new whatsapp version. The only disadvantage is that the multi-device session is not saved, and the QR must be scanned on every restart.
has the solution been found, I use the code above, it doesn't work in my code
const createSession = function(id,fk_id_users,description) { const SESSION_FILE_PATH =
./App/whatsapp/sessions/whatsapp-session-${id}.json`; let sessionCfg; if (fs.existsSync(SESSION_FILE_PATH)) { sessionCfg = require(SESSION_FILE_PATH); } const client = new Client({ restartOnAuthFail: true, puppeteer: { headless: true, args: [ '--no-sandbox', '--unhandled-rejections=strict', '--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, authStrategy: new LegacySessionAuth({ session: sessionCfg }) }); client.initialize(); let no = 1; client.on('qr', (qr) => { const savedSessions = getSessionsFile(); const ceksessionIndex = savedSessions.findIndex(sess => sess.id == id); if (!!ceksessionIndex) { console.log('true') } else { console.log('false'); }/* ini setiap kelipatan 5 qr code akan otomatis delete*/ if (no%6 === 0) { if (fs.existsSync(SESSION_FILE_PATH)) { fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Session file deleted!'); }); } savedSessions.splice(sessionIndex, 1); setSessionsFile(savedSessions); io.emit('reset-kelipatan',{id:id, fk_id_users:fk_id_users}) console.log('ID_WA:'+id+',berhasil dihapus sudah kelipatan 5') } if (ceksessionIndex == 0) { console.log(no++ + ',' +'QR RECEIVED ID_WA: '+id+', fk_id_users: ', fk_id_users); qrcode.toDataURL(qr, (err, url) => { io.emit('qr', { id: id, fk_id_users : fk_id_users ,src: url }); }); } else if (ceksessionIndex){ console.log(no++ + ',' +'QR RECEIVED ID_WA: '+id+', fk_id_users: ', fk_id_users); qrcode.toDataURL(qr, (err, url) => { io.emit('qr', { id: id, fk_id_users : fk_id_users ,src: url }); }); } client.removeAllListeners('qr');
}); client.on('message', (message) => { console.log('Message Recived'); io.emit('menerima-pesan','ada pesan goblok'); }); client.on('ready', () => { io.emit('ready', { id: id, fk_id_users : fk_id_users ,description: description }); // io.emit('message', { id: id, text: 'Whatsapp is ready!' }); // io.emit('wa-ready','Wa Ready id: '+ id + ', Description: ' + description); console.log('Whatsapp Ready id: '+ id + ', fk_id_users:' + fk_id_users + ', Description: ' + description);
try { const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id); savedSessions[sessionIndex].ready = true; setSessionsFile(savedSessions); } catch (error) { if (fs.existsSync(SESSION_FILE_PATH)) { /* untuk menghandle ketika barcode masih ada ketika sudah disconnect */ fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Delete error but delete file hehe') }); } }
}); client.on('authenticated', (session) => { 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) { io.emit('message', { id: id, text: 'Auth failure, restarting...' }); }); client.on('disconnected', (reason) => { io.emit('message', { id: id, text: 'Whatsapp is disconnected!' }); console.log('Whatsapp Disconnected:' + id); if (fs.existsSync(SESSION_FILE_PATH)) { fs.unlinkSync(SESSION_FILE_PATH, function(err) { if(err) return console.log(err); console.log('Session file deleted!'); }); }
client.destroy(); client.initialize(); // Menghapus pada file sessions try { const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id); savedSessions.splice(sessionIndex, 1); setSessionsFile(savedSessions); } catch (error) { console.log('disconnect error:'+id) } io.emit('remove-session', id);
}); // Tambahkan client ke sessions sessions.push({ id: id, fk_id_users :fk_id_users, description: description, client: client }); // Menambahkan session ke file const savedSessions = getSessionsFile(); const sessionIndex = savedSessions.findIndex(sess => sess.id == id); if (sessionIndex == -1) { savedSessions.push({ id: id, fk_id_users:fk_id_users, description: description, ready: false, }); setSessionsFile(savedSessions); } } `
i was try LegacySessionAuth but not work to, then i move to localauth, everything work fine
it doesn't work in my code, for the qr code it appears but when the qr is scanned, whatsapp can't enter ? is there a coding solution from you?
You have to change your authentifcation mode and use localAuth. As example you can do this : ``const { Client, LocalAuth } = require("whatsapp-web.js"); console.log("Connection to Whatsapp Web Client");
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, authStrategy: new LocalAuth({ clientId: "client-one" }), puppeteer: { headless: false, } }); client.initialize(); client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); }); client.on('authenticated', (session) => { console.log('WHATSAPP WEB => Authenticated'); }); client.on("ready", async () => { console.log("WHATSAPP WEB => Ready"); resolve(client) });
});`` This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
use pm2 to keep your node is running, install
npm install pm2 -g
and how to usage: [https://www.npmjs.com/package/pm2](from here!)
i see sir
You have to change your authentifcation mode and use localAuth. As example you can do this : ``const { Client, LocalAuth } = require("whatsapp-web.js"); console.log("Connection to Whatsapp Web Client");
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, authStrategy: new LocalAuth({ clientId: "client-one" }), puppeteer: { headless: false, } }); client.initialize(); client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); }); client.on('authenticated', (session) => { console.log('WHATSAPP WEB => Authenticated'); }); client.on("ready", async () => { console.log("WHATSAPP WEB => Ready"); resolve(client) });
});`` This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
Thanks this work! and this the recover session when node start again.
How can recover session? Please help me
In which file we need to do these changes?
;
This line of code has caught my attention, the puppeteer options 'executablePath' :
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, });
Do you think you can solve with the deployment in a hosting with cPanel?
You have to change your authentifcation mode and use localAuth. As example you can do this : ``const { Client, LocalAuth } = require("whatsapp-web.js"); console.log("Connection to Whatsapp Web Client");
const client = new Client({ puppeteer: { executablePath: '/usr/bin/brave-browser-stable', }, authStrategy: new LocalAuth({ clientId: "client-one" }), puppeteer: { headless: false, } }); client.initialize(); client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); }); client.on('authenticated', (session) => { console.log('WHATSAPP WEB => Authenticated'); }); client.on("ready", async () => { console.log("WHATSAPP WEB => Ready"); resolve(client) });
});`` This will work for connect to WWjs but i don't know how to recover the session once node process exit. If anyone has an idea i'm listening to :)
Thanks this work! and this the recover session when node start again.
How can recover session? Please help me
Hi, we were able to persist the session using volumes in dockers, It works well.
Specific temporary message Hide confidential data
Greetings Pedros and other experts, I have a chatBot where I ask the user to enter their PIN code in the chat, I would like that if the message has the PIN code in its body, that it be sent temporarily or automatically deleted in 2 minutes in the chat for the security of the credential data of the user that my chatBot is using, it is a thought in case it is possible for the library to do this taking advantage of the fact that WhatsApp has the option of temporary messages.
What is the possibility to be able to control a type of message that carries a data that we would like to get out of the chat?
Also, if someone comes up with any other security solution for this PIN data, it is sensitive and confidential data, please, do not hesitate to contact me, I will be very grateful and invite you for a coffee.
If you want to use LegacySessionAuth (basically you don't need to change anything in your server code) but still want to be able to connect to multi-device numbers, you can comment the following line in
src/authStrategies/LegacySessionAuth.js
in which the Error is thrown:I decided that a console.log would be better intead of an error. This solution works well with the previous and the new whatsapp version. The only disadvantage is that the multi-device session is not saved, and the QR must be scanned on every restart.
Will be deprecated after legacy is no longer a thing
Ola! Nao cheguei a ler todos os comentarios, mas pra mim resolveu excluir o diretorio .wwebjs_cache e o diretorio de sessoes. Ao registrar novamente usando o qrcode voltou a funcionar. Tambem usei o parametro headless: 'new', mas nao sei se implicou em algo, pois tive que por em producao e nao tive um tempo para testar mais.
Is there an existing issue for this?
Describe the bug
Authenticating via JSON session is not supported for MultiDevice-enabled WhatsApp accounts Business
Expected behavior
i just upgraded whatsapp Business and there is no option for single device
Steps to Reproduce the Bug or Issue
Error: Authenticating via JSON session is not supported for MultiDevice-enabled WhatsApp accounts.
Relevant Code
No response
Browser Type
Google Chrome
WhatsApp Account Type
WhatsApp Business
Does your WhatsApp account have multidevice enabled?
Yes, I am using Multi Device
Environment
Windows, Linux, Ubuntu 20
Additional context
No response