Spaces:
Building
Building
Update index.js
Browse files
index.js
CHANGED
@@ -1,43 +1,97 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
sock.ev.on('connection.update', async (update) => {
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
}
|
|
|
|
|
|
|
15 |
});
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
console.error('Error:', error);
|
26 |
-
}
|
27 |
-
}
|
28 |
-
}, 5000);
|
29 |
|
30 |
-
sock.ev.on('messages.upsert', async (
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
}
|
35 |
|
36 |
-
//
|
37 |
-
|
38 |
-
|
|
|
39 |
}
|
40 |
|
41 |
-
|
42 |
-
.then(() => console.log('Bot is running...'))
|
43 |
-
.catch(console.error);
|
|
|
1 |
+
const makeWASocket = require('@whiskeysockets/baileys').default;
|
2 |
+
const {
|
3 |
+
DisconnectReason,
|
4 |
+
useMultiFileAuthState,
|
5 |
+
fetchLatestBaileysVersion
|
6 |
+
} = require('@whiskeysockets/baileys');
|
7 |
+
const fs = require('fs');
|
8 |
+
const path = require('path');
|
9 |
+
const Pino = require('pino');
|
10 |
|
11 |
+
const logger = Pino({ level: 'info' });
|
12 |
+
|
13 |
+
const store = {};
|
14 |
+
|
15 |
+
const getMessage = key => {
|
16 |
+
const { id } = key;
|
17 |
+
if (store[id]) return store[id].message;
|
18 |
+
};
|
19 |
+
|
20 |
+
async function connectWhatsApp() {
|
21 |
+
try {
|
22 |
+
const { state, saveCreds } = await useMultiFileAuthState('auth');
|
23 |
+
const { version } = await fetchLatestBaileysVersion();
|
24 |
+
|
25 |
+
const sock = makeWASocket({
|
26 |
+
auth: state,
|
27 |
+
version: version,
|
28 |
+
getMessage,
|
29 |
+
printQRInTerminal: false // Disable QR code
|
30 |
+
});
|
31 |
+
|
32 |
+
// Request pairing code if not registered
|
33 |
+
if (!sock.authState.creds.registered) {
|
34 |
+
const phoneNumber = '+919709859065'; // Replace with your WhatsApp number
|
35 |
+
try {
|
36 |
+
const code = await sock.requestPairingCode(phoneNumber);
|
37 |
+
console.log(`Pairing Code: ${code}`);
|
38 |
+
console.log('Enter this code in WhatsApp under Linked Devices > Pair a New Device.');
|
39 |
+
} catch (error) {
|
40 |
+
console.error('Error requesting pairing code:', error);
|
41 |
+
}
|
42 |
+
}
|
43 |
|
44 |
sock.ev.on('connection.update', async (update) => {
|
45 |
+
const { connection, lastDisconnect } = update;
|
46 |
+
if (connection === 'close') {
|
47 |
+
if (lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut) {
|
48 |
+
logger.info('Connection lost. Reconnecting...');
|
49 |
+
await connectWhatsApp();
|
50 |
+
} else {
|
51 |
+
logger.info('Disconnected because you logged out.');
|
52 |
}
|
53 |
+
} else if (connection === 'open') {
|
54 |
+
logger.info('Connection established.');
|
55 |
+
}
|
56 |
});
|
57 |
|
58 |
+
sock.ev.on('creds.update', async () => {
|
59 |
+
try {
|
60 |
+
await saveCreds();
|
61 |
+
logger.info('Credentials updated.');
|
62 |
+
} catch (error) {
|
63 |
+
logger.error('Failed to save credentials:', error);
|
64 |
+
}
|
65 |
+
});
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
sock.ev.on('messages.upsert', async (events) => {
|
68 |
+
const { messages } = events;
|
69 |
+
messages.forEach(message => {
|
70 |
+
const maskedNumber = maskPhoneNumber(message.key.remoteJid);
|
71 |
+
logger.info(`Message from ${maskedNumber}:`, message);
|
72 |
+
});
|
73 |
+
});
|
74 |
+
|
75 |
+
} catch (error) {
|
76 |
+
logger.error('Failed to connect to WhatsApp:', error);
|
77 |
+
}
|
78 |
+
}
|
79 |
|
80 |
+
function maskPhoneNumber(phoneNumber) {
|
81 |
+
if (!phoneNumber) return phoneNumber;
|
82 |
+
const parts = phoneNumber.split('@');
|
83 |
+
if (parts.length === 2) {
|
84 |
+
const [number, domain] = parts;
|
85 |
+
const maskedNumber = number.slice(0, 3) + '*****' + number.slice(-3);
|
86 |
+
return `${maskedNumber}@${domain}`;
|
87 |
+
}
|
88 |
+
return phoneNumber;
|
89 |
}
|
90 |
|
91 |
+
// Ensure the auth directory exists
|
92 |
+
const authDir = path.join(__dirname, 'auth');
|
93 |
+
if (!fs.existsSync(authDir)) {
|
94 |
+
fs.mkdirSync(authDir, { recursive: true });
|
95 |
}
|
96 |
|
97 |
+
connectWhatsApp();
|
|
|
|