File size: 1,863 Bytes
69582e9
 
 
 
 
89a3ff1
69582e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaaa928
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
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] = [];

    const newEntry = { amount: parseFloat(amount), timestamp: new Date().toISOString() };
    data[chatid].push(newEntry);
    writeData(data);

    // Call /api/new internally
    res.redirect(`/api/new?chatid=${chatid}&apikey=${req.query.apikey}`);
});

// /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] || [];

    res.json({
        chatid,
        amounts: chatData.map((entry, index) => ({
            amount: entry.amount,
            amountId: index + 1,
            timestamp: entry.timestamp
        }))
    });
});

// Start the server
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));