ahdeveloperai777 commited on
Commit
8489a49
·
verified ·
1 Parent(s): f827bcd

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +35 -0
index.js ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { default: makeWASocket, useMultiFileAuthState } = require('@whiskeysockets/baileys');
2
+ const express = require('express');
3
+ const qrcode = require('qrcode-terminal');
4
+
5
+ const app = express();
6
+ app.use(express.json());
7
+
8
+ async function startWA() {
9
+ const { state, saveCreds } = await useMultiFileAuthState('auth_state');
10
+ const sock = makeWASocket({ auth: state, printQRInTerminal: true });
11
+
12
+ sock.ev.on('creds.update', saveCreds);
13
+ sock.ev.on('connection.update', (update) => {
14
+ const { connection, qr } = update;
15
+ if (qr) {
16
+ console.log("SCAN QR CODE BELOW:");
17
+ qrcode.generate(qr, { small: true });
18
+ }
19
+ if (connection === 'open') console.log("✅ WHATSAPP CONNECTED!");
20
+ });
21
+
22
+ app.post('/send-otp', async (req, res) => {
23
+ const { number, message } = req.body;
24
+ let jid = number.replace(/\D/g, '') + "@s.whatsapp.net";
25
+ if (jid.startsWith('0')) jid = '92' + jid.substring(1); // PK numbers
26
+
27
+ try {
28
+ await sock.sendMessage(jid, { text: message });
29
+ res.json({ success: true });
30
+ } catch (e) { res.status(500).json({ error: e.message }); }
31
+ });
32
+ }
33
+
34
+ startWA();
35
+ app.listen(7860, () => console.log("Listening on Port 7860")); // Hugging Face uses 7860