Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const fs = require("fs");
|
| 3 |
+
const app = express();
|
| 4 |
+
|
| 5 |
+
const PORT = 7860;
|
| 6 |
+
const CODES_FILE = "codes.json";
|
| 7 |
+
|
| 8 |
+
// Function to generate a 6-character alphanumeric code
|
| 9 |
+
function generateCode() {
|
| 10 |
+
return Math.random().toString(36).substring(2, 8).toUpperCase();
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
// Function to get current Unix timestamp
|
| 14 |
+
function getUnixTimestamp() {
|
| 15 |
+
return Math.floor(Date.now() / 1000);
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
// Ensure codes.json exists
|
| 19 |
+
if (!fs.existsSync(CODES_FILE)) {
|
| 20 |
+
fs.writeFileSync(CODES_FILE, JSON.stringify({}), "utf8");
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// API to generate a new code
|
| 24 |
+
app.get("/api/generate", (req, res) => {
|
| 25 |
+
const chatId = req.query.chatid;
|
| 26 |
+
|
| 27 |
+
if (!chatId) {
|
| 28 |
+
return res.status(400).json({ error: "chatid is required" });
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
const code = generateCode();
|
| 32 |
+
const timestamp = getUnixTimestamp();
|
| 33 |
+
|
| 34 |
+
// Load existing codes
|
| 35 |
+
let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8"));
|
| 36 |
+
|
| 37 |
+
// Store the new code
|
| 38 |
+
if (!codes[chatId]) {
|
| 39 |
+
codes[chatId] = [];
|
| 40 |
+
}
|
| 41 |
+
codes[chatId].push({ code, timestamp });
|
| 42 |
+
|
| 43 |
+
// Save to codes.json
|
| 44 |
+
fs.writeFileSync(CODES_FILE, JSON.stringify(codes, null, 2), "utf8");
|
| 45 |
+
|
| 46 |
+
res.json({ chatId, code, timestamp });
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
// API to get recent codes for a chat ID
|
| 50 |
+
app.get("/api/get", (req, res) => {
|
| 51 |
+
const chatId = req.query.q;
|
| 52 |
+
|
| 53 |
+
if (!chatId) {
|
| 54 |
+
return res.status(400).json({ error: "q (chatid) is required" });
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Load existing codes
|
| 58 |
+
let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8"));
|
| 59 |
+
|
| 60 |
+
if (!codes[chatId]) {
|
| 61 |
+
return res.status(404).json({ error: "No codes found for this chat ID" });
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
res.json({ chatId, codes: codes[chatId] });
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
// Start server
|
| 68 |
+
app.listen(PORT, () => {
|
| 69 |
+
console.log(`Server running on port ${PORT}`);
|
| 70 |
+
});
|