-
Notifications
You must be signed in to change notification settings - Fork 0
/
waweb.js
65 lines (52 loc) · 1.6 KB
/
waweb.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
62
63
64
const env = require('dotenv').config().parsed;
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal'); // Import the QR code package
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const SOCK_FILE = 'run/whatsapp.sock';
async function load() {
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
// HTTP API endpoint for evaluating JavaScript
app.post('/eval', async (req, res) => {
const input = req.body.input;
console.log(`Running ${input}...`);
try {
const ret = await eval(`(async () => { return ${input}; })()`);
res.send(ret);
} catch (e) {
res.send(e.message);
}
return res.end();
});
app.listen(env.WHATSAPP_API_PORT || 2002, () => {});
// Create client with persistent session using LocalAuth
const client = new Client({
authStrategy: new LocalAuth(), // Stores session data
puppeteer: {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-extensions',
],
},
});
// Display the QR code in the terminal
client.on('qr', (qr) => {
qrcode.generate(qr, { small: true }); // Generate the QR code in terminal
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', async (msg) => {
console.log('Received message:', msg.body);
// You can handle incoming messages here
});
await client.initialize();
}
(async () => {
await load();
})();
process.on('SIGINT', () => fs.unlinkSync(SOCK_FILE));