| const express = require("express"); |
| const fs = require("fs"); |
| const app = express(); |
|
|
| const PORT = 7860; |
| const CODES_FILE = "codes.json"; |
|
|
| |
| function generateCode() { |
| return Math.random().toString(36).substring(2, 8).toUpperCase(); |
| } |
| |
| function getUnixTimestamp() { |
| return Math.floor(Date.now() / 1000); |
| } |
|
|
| |
| if (!fs.existsSync(CODES_FILE)) { |
| fs.writeFileSync(CODES_FILE, JSON.stringify({}), "utf8"); |
| } |
|
|
| const AMOUNTS_FILE = "amounts.json"; |
| const AMOUNTS_FILE_2 = "amounts2.json"; |
|
|
| |
| function getUnixTimestamp() { |
| return Math.floor(Date.now() / 1000); |
| } |
|
|
| |
| if (!fs.existsSync(AMOUNTS_FILE)) { |
| fs.writeFileSync(AMOUNTS_FILE, JSON.stringify({}), "utf8"); |
| } |
|
|
| app.get("/add", (req, res) => { |
| const chatId = req.query.chatid; |
| const amount = parseInt(req.query.amount, 10); |
|
|
| if (!chatId || isNaN(amount) || amount <= 0) { |
| return res.status(400).json({ error: "Valid chatid and amount are required" }); |
| } |
|
|
| const timestamp = getUnixTimestamp(); |
|
|
| |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8")); |
|
|
| |
| if (!amounts[chatId]) { |
| amounts[chatId] = []; |
| } |
| amounts[chatId].push({ amount, timestamp }); |
|
|
| |
| fs.writeFileSync(AMOUNTS_FILE, JSON.stringify(amounts, null, 2), "utf8"); |
|
|
| res.json({ chatId, amount, timestamp }); |
| }); |
|
|
| |
| app.get("/get", (req, res) => { |
| const chatId = req.query.chatid; |
|
|
| if (!chatId) { |
| return res.status(400).json({ error: "chatid is required" }); |
| } |
|
|
| |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8")); |
|
|
| if (!amounts[chatId]) { |
| return res.status(404).json({ error: "No amounts found for this chat ID" }); |
| } |
|
|
| res.json({ chatId, amounts: amounts[chatId] }); |
| }); |
|
|
| |
| app.get("/api/generate", (req, res) => { |
| const chatId = req.query.chatid; |
| |
| if (!chatId) { |
| return res.status(400).json({ error: "chatid is required" }); |
| } |
|
|
| const code = generateCode(); |
| const timestamp = getUnixTimestamp(); |
|
|
| |
| let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8")); |
|
|
| |
| if (!codes[chatId]) { |
| codes[chatId] = []; |
| } |
| codes[chatId].push({ code, timestamp }); |
|
|
| |
| fs.writeFileSync(CODES_FILE, JSON.stringify(codes, null, 2), "utf8"); |
|
|
| res.json({ chatId, code, timestamp }); |
| }); |
|
|
| |
| app.get("/api/get", (req, res) => { |
| const chatId = req.query.q; |
| |
| if (!chatId) { |
| return res.status(400).json({ error: "q (chatid) is required" }); |
| } |
|
|
| |
| let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8")); |
|
|
| if (!codes[chatId]) { |
| return res.status(404).json({ error: "No codes found for this chat ID" }); |
| } |
|
|
| res.json({ chatId, codes: codes[chatId] }); |
| }); |
|
|
| app.get("/add2", (req, res) => { |
| const chatId = req.query.chatid; |
| const amount = parseInt(req.query.amount, 10); |
|
|
| if (!chatId || isNaN(amount) || amount <= 0) { |
| return res.status(400).json({ error: "Valid chatid and amount are required" }); |
| } |
|
|
| const timestamp = getUnixTimestamp(); |
|
|
| |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE_2, "utf8")); |
|
|
| |
| if (!amounts[chatId]) { |
| amounts[chatId] = []; |
| } |
| amounts[chatId].push({ amount, timestamp }); |
|
|
| |
| fs.writeFileSync(AMOUNTS_FILE_2, JSON.stringify(amounts, null, 2), "utf8"); |
|
|
| res.json({ chatId, amount, timestamp }); |
| }); |
|
|
| |
| app.get("/get2", (req, res) => { |
| const chatId = req.query.chatid; |
|
|
| if (!chatId) { |
| return res.status(400).json({ error: "chatid is required" }); |
| } |
|
|
| |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE_2, "utf8")); |
|
|
| if (!amounts[chatId]) { |
| return res.status(404).json({ error: "No amounts found for this chat ID" }); |
| } |
|
|
| res.json({ chatId, amounts: amounts[chatId] }); |
| }); |
|
|
| |
| app.listen(PORT, () => { |
| console.log(`Server running on port ${PORT}`); |
| }); |
|
|