admin管理员组文章数量:1414858
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
本文标签: javascriptWhatsappWebjs restore sessionStack Overflow
版权声明:本文标题:javascript - Whatsapp-Web.js restore session - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745210872a2647877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论