Bsbb / server.js
Jsjsjj8383's picture
Create server.js
b093e0f verified
Raw
History Blame Contribute Delete
4.02 kB
const http = require("http");
const fs = require("fs");
const {
default: makeWASocket,
useMultiFileAuthState,
fetchLatestBaileysVersion
} = require("@whiskeysockets/baileys");
const PORT = process.env.PORT || 5900;
let sock;
let initialized = false;
// ๐ŸŒ HTML UI
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Baileys Linker</title>
<style>
body {
font-family: system-ui, sans-serif;
background: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.box {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
width: 100%;
max-width: 360px;
text-align: center;
}
input, select {
width: 90%;
padding: 12px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 16px;
}
button {
background: #25D366;
color: white;
border: none;
padding: 12px;
width: 97%;
border-radius: 6px;
font-size: 16px;
cursor: pointer;
font-weight: bold;
}
button:hover {
background: #1ebd56;
}
</style>
</head>
<body>
<div class="box">
<h2>Link WhatsApp Account</h2>
<p>Enter your number with country code</p>
<form method="GET" action="/get-code">
<select name="country">
<option value="373">๐Ÿ‡ฒ๐Ÿ‡ฉ Moldova (+373)</option>
<option value="1">๐Ÿ‡บ๐Ÿ‡ธ United States (+1)</option>
<option value="44">๐Ÿ‡ฌ๐Ÿ‡ง United Kingdom (+44)</option>
<option value="91">๐Ÿ‡ฎ๐Ÿ‡ณ India (+91)</option>
</select>
<input type="text" name="phone" placeholder="Phone number" required>
<button type="submit">Generate Pairing Code</button>
</form>
</div>
</body>
</html>
`;
// ๐Ÿš€ HTTP SERVER (NO url module)
const server = http.createServer(async (req, res) => {
// HOME PAGE
if (req.url === "/") {
res.writeHead(200, { "Content-Type": "text/html" });
return res.end(htmlContent);
}
// GET CODE (manual parsing, NO url module)
if (req.url.startsWith("/get-code")) {
// extract query manually
const queryString = req.url.split("?")[1] || "";
const params = {};
queryString.split("&").forEach(pair => {
const [key, value] = pair.split("=");
if (key) params[key] = decodeURIComponent(value || "");
});
const country = params.country;
const phone = params.phone;
const number = country + phone;
try {
if (!initialized) {
const { state, saveCreds } = await useMultiFileAuthState("auth");
const { version } = await fetchLatestBaileysVersion();
sock = makeWASocket({
version,
auth: state,
printQRInTerminal: false
});
sock.ev.on("creds.update", saveCreds);
sock.ev.on("connection.update", (update) => {
if (update.connection === "open") {
console.log("WhatsApp Connected");
}
});
initialized = true;
}
const code = await sock.requestPairingCode(number);
console.log("PAIR CODE:", code);
res.writeHead(200, { "Content-Type": "text/html" });
return res.end(`
<h2>Pairing Code Generated</h2>
<h1>${code}</h1>
<a href="/">Back</a>
`);
} catch (err) {
console.log(err);
res.writeHead(500);
return res.end("Error generating pairing code");
}
}
res.writeHead(404);
res.end("Not Found");
});
// ๐ŸŒ START SERVER
server.listen(PORT, "0.0.0.0", () => {
console.log("server running on http://127.0.0.1:" + PORT);
});