-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (52 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const fs = require('fs');
const puppeteer = require('puppeteer');
// Path where the session data will be stored
const SESSION_FILE_PATH = './session.json';
// Function to check if the session file exists
function checkForSessionFile() {
if (fs.existsSync(SESSION_FILE_PATH)) {
console.log('**Session file found.**');
return true;
} else {
console.log('**Session file not found.** A new session will be created.');
return false;
}
}
// Initialize the client with LocalAuth strategy and Puppeteer configuration
const client = new Client({
authStrategy: new LocalAuth({
dataPath: './.wwebjs_auth' // Specify the path to store the session data
}),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox']
},
webVersionCache: {
type: 'remote',
remotePath: 'https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html'
}
});
client.on('qr', (qr) => {
// Generate and display the QR code in the terminal
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('**Client is ready!**');
// Check for the session file when the client is ready
checkForSessionFile();
});
client.on('authenticated', (session) => {
console.log('**Authenticated!**');
// Save session values to the file upon successful auth
fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session));
});
client.on('auth_failure', msg => {
console.error('**Authentication failure:**', msg);
});
client.on('message', message => {
message.reply(message.body);
});
// Check for the session file before initializing the client
checkForSessionFile();
client.initialize();