Spaces:
Sleeping
Sleeping
File size: 3,013 Bytes
d270624 8489a49 d270624 8489a49 6f1ac03 d270624 6f1ac03 d270624 6f1ac03 8489a49 6f1ac03 8489a49 d270624 8489a49 d270624 6f1ac03 8489a49 6f1ac03 8489a49 d270624 6f1ac03 8489a49 d270624 8489a49 d270624 8489a49 6f1ac03 8489a49 6f1ac03 8489a49 6f1ac03 8489a49 d270624 6f1ac03 | 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 | const { default: makeWASocket, useMultiFileAuthState, fetchLatestBaileysVersion, DisconnectReason } = require('@whiskeysockets/baileys');
const express = require('express');
const qrcode = require('qrcode-terminal');
const pino = require('pino');
const app = express();
app.use(express.json());
async function startWA() {
const { state, saveCreds } = await useMultiFileAuthState('auth_state');
const { version } = await fetchLatestBaileysVersion();
console.log("Using WhatsApp Web Version:", version.join('.'));
const sock = makeWASocket({
version,
auth: state,
logger: pino({ level: 'silent' }), // Hide messy logs
browser: ["AutoFlowLearn", "Chrome", "1.0.0"],
// DNS aur Connection issues ke liye settings
connectTimeoutMs: 60000,
defaultQueryTimeoutMs: 0,
keepAliveIntervalMs: 10000,
});
sock.ev.on('creds.update', saveCreds);
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect, qr } = update;
// Manual QR Handling (Fixed warning)
if (qr) {
console.clear();
console.log("----------------------------------------");
console.log("SCAN QR CODE BELOW:");
console.log("----------------------------------------");
qrcode.generate(qr, { small: true });
}
if (connection === 'close') {
const shouldReconnect = (lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut;
console.log('β Connection closed due to ', lastDisconnect?.error, ', reconnecting: ', shouldReconnect);
if (shouldReconnect) {
// Thora wait karke dobara try karein agar network ka masla ho
setTimeout(() => startWA(), 5000);
}
} else if (connection === 'open') {
console.log("β
WHATSAPP CONNECTED SUCCESSFULLY!");
}
});
// API Endpoint for Python
app.post('/send-otp', async (req, res) => {
try {
let { number, message } = req.body;
let cleanNumber = number.replace(/\D/g, '');
if (cleanNumber.startsWith('03')) {
cleanNumber = '92' + cleanNumber.substring(1);
}
const jid = cleanNumber + "@s.whatsapp.net";
await sock.sendMessage(jid, { text: message });
console.log(`β
Message sent to ${cleanNumber}`);
res.json({ success: true });
} catch (e) {
console.error("β Send Error:", e.message);
res.status(500).json({ error: e.message });
}
});
app.get('/', (req, res) => res.send("WhatsApp OTP Service is Running!"));
}
// Start with error handling
startWA().catch(err => {
console.error("Critical Startup Error:", err);
setTimeout(() => startWA(), 10000); // Retry after 10s
});
app.listen(7860, () => console.log("π Server running on Port 7860")); |