File size: 4,016 Bytes
b093e0f | 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 | 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);
}); |