const express = require("express"); const fs = require("fs"); require("dotenv").config(); const app = express(); const PORT = 7860; const DATA_FILE = "amounts.json"; // Middleware to verify API key from query params function authenticate(req, res, next) { const apiKey = req.query.apikey; if (!apiKey || apiKey !== process.env.API_KEY) { return res.status(403).json({ error: "Invalid API key" }); } next(); } // Function to read stored data function readData() { if (!fs.existsSync(DATA_FILE)) return {}; return JSON.parse(fs.readFileSync(DATA_FILE)); } // Function to write stored data function writeData(data) { fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2)); } // /api/amount?chatid=123&amount=50&apikey=yourkey app.get("/api/amount", authenticate, (req, res) => { const { chatid, amount } = req.query; if (!chatid || !amount) return res.status(400).json({ error: "chatid and amount required" }); const data = readData(); if (!data[chatid]) data[chatid] = []; // Generate UNIX timestamp for unique ID const timestamp = Date.now() / 1000; const newEntry = { amount: parseFloat(amount), amountId: timestamp, timestamp }; data[chatid].push(newEntry); writeData(data); // Respond with transaction status res.json({ message: "Transaction on the way" }); }); // /api/new?chatid=123&apikey=yourkey app.get("/api/new", authenticate, (req, res) => { const { chatid } = req.query; if (!chatid) return res.status(400).json({ error: "chatid required" }); const data = readData(); const chatData = data[chatid] || []; // Sort by timestamp to maintain order chatData.sort((a, b) => a.timestamp - b.timestamp); res.json({ chatid, amounts: chatData }); }); // Start the server app.listen(PORT, () => console.log(`Server running on port ${PORT}`));