-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
167 lines (145 loc) · 4.75 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import * as baileys from 'baileys';
import fs from 'fs-extra';
import pino from 'pino';
import cors from 'cors';
import express from 'express';
import { Boom } from '@hapi/boom';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { encryptSession } from './utils.js';
import { getSession, saveSession } from './db.js';
const app = express();
app.set('json spaces', 2);
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
next();
});
app.use(cors());
let PORT = process.env.PORT || 8000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function generateAccessKey() {
const formatNumber = num => num.toString().padStart(2, '0');
const r1 = formatNumber(Math.floor(Math.random() * 100));
const r2 = formatNumber(Math.floor(Math.random() * 100));
const r3 = formatNumber(Math.floor(Math.random() * 100));
const key = `XSTRO_${r1}_${r2}_${r3}`;
return key;
}
const accessKey = generateAccessKey();
function clearFolder(folderPath) {
if (!fs.existsSync(folderPath)) return;
const contents = fs.readdirSync(folderPath);
for (const item of contents) {
const itemPath = join(folderPath, item);
if (fs.statSync(itemPath).isDirectory()) {
fs.rmSync(itemPath, { recursive: true, force: true });
} else {
fs.unlinkSync(itemPath);
}
}
}
clearFolder('./session');
app.get('/pair', async (req, res) => {
let phone = req.query.phone;
if (!phone) {
return res.json({ error: 'Provide Valid Phone Number' });
}
const code = await getPairingCode(phone);
res.json({ code: code });
});
app.get('/session', async (req, res) => {
const accessKey = req.query.session;
if (!accessKey) {
return res.status(401).json({ error: 'No session provided' });
}
try {
const sessionData = await getSession(accessKey);
if (!sessionData) {
return res.status(401).json({ error: 'Invalid session' });
}
res.json(sessionData);
} catch (error) {
res.status(500).json({ error: 'Server error' });
}
});
async function getPairingCode(phone) {
return new Promise(async (resolve, reject) => {
try {
const logger = pino({ level: 'silent' });
const { state, saveCreds } = await baileys.useMultiFileAuthState('session');
const { version } = await baileys.fetchLatestBaileysVersion();
const buffer = await fetch('https://avatars.githubusercontent.com/u/188756392?v=4')
.then(res => res.arrayBuffer())
.then(Buffer.from);
const conn = baileys.makeWASocket({
version: version,
printQRInTerminal: true,
logger: logger,
browser: baileys.Browsers.ubuntu('Chrome'),
auth: {
creds: state.creds,
keys: baileys.makeCacheableSignalKeyStore(state.keys, logger)
}
});
if (!conn.authState.creds.registered) {
let phoneNumber = phone ? phone.replace(/[^0-9]/g, '') : '';
if (phoneNumber.length < 11) return reject(new Error('Enter Valid Phone Number'));
setTimeout(async () => {
let code = await conn.requestPairingCode(phoneNumber);
resolve(code);
}, 3000);
}
conn.ev.on('creds.update', saveCreds);
conn.ev.on('connection.update', async update => {
console.log('Connection update:', update);
const { connection, lastDisconnect } = update;
if (connection === 'open') {
await baileys.delay(10000);
await conn.sendMessage(conn.user.id,{text: accessKey});
const data = encryptSession('session/creds.json');
await saveSession(accessKey, data);
await baileys.delay(5000);
clearFolder(join(__dirname, 'session'));
process.send('reset');
}
if (connection === 'close') {
const reason = new Boom(lastDisconnect?.error)?.output.statusCode;
const resetReasons = [
baileys.DisconnectReason.connectionClosed,
baileys.DisconnectReason.connectionLost,
baileys.DisconnectReason.timedOut,
baileys.DisconnectReason.connectionReplaced
];
const resetWithClearStateReasons = [
baileys.DisconnectReason.loggedOut,
baileys.DisconnectReason.badSession
];
if (resetReasons.includes(reason)) {
process.send('reset');
} else if (resetWithClearStateReasons.includes(reason)) {
clearFolder('./session');
process.send('reset');
} else if (reason === baileys.DisconnectReason.restartRequired) {
getPairingCode();
} else {
process.send('reset');
}
}
});
conn.ev.on('messages.upsert', msg => {
if (msg.type === 'notify') {
console.log(JSON.parse(JSON.stringify(msg.messages[0])));
}
});
} catch (error) {
console.error('Error occurred:', error);
reject(new Error('An Error Occurred'));
}
});
}
app.listen(PORT, () => {
console.log('Server running at:\nhttp://localhost:' + PORT);
});