File size: 1,888 Bytes
69582e9
 
 
 
 
89a3ff1
69582e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaaa928
69582e9
 
 
 
 
 
 
 
 
 
 
 
 
39e1aab
 
 
 
69582e9
 
 
e0be89e
 
69582e9
 
 
 
 
 
 
 
 
 
39e1aab
 
 
69582e9
 
39e1aab
69582e9
 
 
 
 
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
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}`));