I'm using the whatsapp-web.js library and I would like to stay logged in after restarting the script. Currently I have to scan the QR code every time I start. The things I found online couldn't get to work (probably cuz I'm dumb but still). For reference this is the code I am trying to get it working with.
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const fs = require('fs');
const client = new Client({
ffmpegPath: "C:/ffmpeg/bin/ffmpeg.exe"
});
client.on('qr', async qr=> {
qrcode.generate(qr, {small: true});
});
client.on('ready', async function () {
console.log('Client is ready!');
});
Thanks in advance.
I'm using the whatsapp-web.js library and I would like to stay logged in after restarting the script. Currently I have to scan the QR code every time I start. The things I found online couldn't get to work (probably cuz I'm dumb but still). For reference this is the code I am trying to get it working with.
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const fs = require('fs');
const client = new Client({
ffmpegPath: "C:/ffmpeg/bin/ffmpeg.exe"
});
client.on('qr', async qr=> {
qrcode.generate(qr, {small: true});
});
client.on('ready', async function () {
console.log('Client is ready!');
});
Thanks in advance.
Share Improve this question edited Jul 11, 2022 at 23:13 Dymo Kilan asked Jul 11, 2022 at 21:59 Dymo KilanDymo Kilan 611 gold badge1 silver badge6 bronze badges 03 Answers
Reset to default 1I fixed it. For anyone who sees this and has the same issue, this is what worked for me: add this to the client: authStrategy: new LocalAuth()
and after generating the qr code for the first time, wait a couple of minutes before hitting ctrl+c
const fs = require('fs');
const { Client, LegacySessionAuth } = require('whatsapp-web.js');
// Path where the session data will be stored
const SESSION_FILE_PATH = './session.json';
// Load the session data if it has been previously saved
let sessionData;
if(fs.existsSync(SESSION_FILE_PATH)) {
sessionData = require(SESSION_FILE_PATH);
}
// Use the saved values
const client = new Client({
authStrategy: new LegacySessionAuth({
session: sessionData
})
});
// Save session values to the file upon successful auth
client.on('authenticated', (session) => {
sessionData = session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
if (err) {
console.error(err);
}
});
});
In recent versions, you have to manually implement LocalAuth
.
To do this open the "Client.js" file located at "./node_modules/whatssap-web.js/src/Client.js" and implement this line
const LocalAuth = require('./authStrategies/LocalAuth');
and now change the line #68
this.authStrategy = new NoAuth();
to
this.authStrategy = new LocalAuth();
for version 1.16.4-alpha.0
date: 27/08/2022
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745210872a4616831.html
评论列表(0条)