mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-19 20:19:14 +00:00
[ADD] Session restore functionality
This commit is contained in:
12
example.js
12
example.js
@@ -1,17 +1,25 @@
|
|||||||
const { Client } = require('./index')
|
const { Client } = require('./index')
|
||||||
|
|
||||||
const client = new Client({puppeteer: {headless: false}});
|
const client = new Client({puppeteer: {headless: false}});
|
||||||
|
// You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options.
|
||||||
|
// This object must include WABrowserId, WASecretBundle, WAToken1 and WAToken2.
|
||||||
|
|
||||||
client.initialize();
|
client.initialize();
|
||||||
|
|
||||||
client.on('qr', (qr) => {
|
client.on('qr', (qr) => {
|
||||||
|
// NOTE: This event will not be fired if a session is specified.
|
||||||
console.log('QR RECEIVED', qr);
|
console.log('QR RECEIVED', qr);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('authenticated', () => {
|
client.on('authenticated', (session) => {
|
||||||
console.log('AUTHENTICATED');
|
console.log('AUTHENTICATED', session);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.on('auth_failure', msg => {
|
||||||
|
// Fired if session restore was unsuccessfull
|
||||||
|
console.error('AUTHENTICATION FAILURE', msg);
|
||||||
|
})
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
console.log('READY');
|
console.log('READY');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ const ChatFactory = require('./factories/ChatFactory');
|
|||||||
const Chat = require('./structures/Chat');
|
const Chat = require('./structures/Chat');
|
||||||
const Message = require('./structures/Message')
|
const Message = require('./structures/Message')
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starting point for interacting with the WhatsApp Web API
|
* Starting point for interacting with the WhatsApp Web API
|
||||||
* @extends {EventEmitter}
|
* @extends {EventEmitter}
|
||||||
@@ -31,18 +33,59 @@ class Client extends EventEmitter {
|
|||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
page.setUserAgent(UserAgent);
|
page.setUserAgent(UserAgent);
|
||||||
|
|
||||||
await page.goto(WhatsWebURL);
|
if(this.options.session) {
|
||||||
await page.evaluate(ExposeStore);
|
await page.evaluateOnNewDocument (
|
||||||
|
session => {
|
||||||
// Wait for QR Code
|
localStorage.clear();
|
||||||
await page.waitForSelector('._1jjYO');
|
localStorage.setItem("WABrowserId", session.WABrowserId);
|
||||||
const qr = await page.$eval('._2EZ_m', node => node.getAttribute('data-ref'));
|
localStorage.setItem("WASecretBundle", session.WASecretBundle);
|
||||||
|
localStorage.setItem("WAToken1", session.WAToken1);
|
||||||
|
localStorage.setItem("WAToken2", session.WAToken2);
|
||||||
|
}, this.options.session);
|
||||||
|
}
|
||||||
|
|
||||||
this.emit(Events.QR_RECEIVED, qr);
|
await page.goto(WhatsWebURL);
|
||||||
|
|
||||||
// Wait for Auth
|
if(this.options.session) {
|
||||||
await page.waitForSelector('._2Uo0Z', {timeout: 0});
|
// Check if session restore was successfull
|
||||||
this.emit(Events.AUTHENTICATED);
|
try {
|
||||||
|
await page.waitForSelector('._2Uo0Z', {timeout: 5000});
|
||||||
|
} catch(err) {
|
||||||
|
if(err.name === 'TimeoutError') {
|
||||||
|
this.emit(Events.AUTHENTICATION_FAILURE, 'Unable to log in. Are the session details valid?');
|
||||||
|
browser.close();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Wait for QR Code
|
||||||
|
await page.waitForSelector('._1jjYO');
|
||||||
|
const qr = await page.$eval('._2EZ_m', node => node.getAttribute('data-ref'));
|
||||||
|
this.emit(Events.QR_RECEIVED, qr);
|
||||||
|
|
||||||
|
// Wait for code scan
|
||||||
|
await page.waitForSelector('._2Uo0Z', {timeout: 0});
|
||||||
|
}
|
||||||
|
|
||||||
|
await page.evaluate(ExposeStore);
|
||||||
|
|
||||||
|
// Get session tokens
|
||||||
|
const localStorage = JSON.parse(await page.evaluate(() => {
|
||||||
|
return JSON.stringify(window.localStorage);
|
||||||
|
}));
|
||||||
|
|
||||||
|
const session = {
|
||||||
|
WABrowserId: localStorage.WABrowserId,
|
||||||
|
WASecretBundle: localStorage.WASecretBundle,
|
||||||
|
WAToken1: localStorage.WAToken1,
|
||||||
|
WAToken2: localStorage.WAToken2
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit(Events.AUTHENTICATED, session);
|
||||||
|
|
||||||
// Check Store Injection
|
// Check Store Injection
|
||||||
await page.waitForFunction('window.Store != undefined');
|
await page.waitForFunction('window.Store != undefined');
|
||||||
@@ -118,7 +161,6 @@ class Client extends EventEmitter {
|
|||||||
return ChatFactory.create(this, chat);
|
return ChatFactory.create(this, chat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Client;
|
module.exports = Client;
|
||||||
@@ -7,7 +7,8 @@ exports.UserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit
|
|||||||
exports.DefaultOptions = {
|
exports.DefaultOptions = {
|
||||||
puppeteer: {
|
puppeteer: {
|
||||||
headless: true
|
headless: true
|
||||||
}
|
},
|
||||||
|
session: false
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.Status = {
|
exports.Status = {
|
||||||
@@ -18,6 +19,7 @@ exports.Status = {
|
|||||||
|
|
||||||
exports.Events = {
|
exports.Events = {
|
||||||
AUTHENTICATED: 'authenticated',
|
AUTHENTICATED: 'authenticated',
|
||||||
|
AUTHENTICATION_FAILURE: 'auth_failure',
|
||||||
READY: 'ready',
|
READY: 'ready',
|
||||||
MESSAGE_CREATE: 'message',
|
MESSAGE_CREATE: 'message',
|
||||||
QR_RECEIVED: 'qr',
|
QR_RECEIVED: 'qr',
|
||||||
|
|||||||
Reference in New Issue
Block a user