File size: 4,504 Bytes
c2c05f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bd62cc
7abdd5a
7bd62cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2c05f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7abdd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2c05f9
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const express = require("express");
const fs = require("fs");
const app = express();

const PORT = 7860;
const CODES_FILE = "codes.json";

// Function to generate a 6-character alphanumeric code
function generateCode() {
    return Math.random().toString(36).substring(2, 8).toUpperCase();
}
// Function to get current Unix timestamp
function getUnixTimestamp() {
    return Math.floor(Date.now() / 1000);
}

// Ensure codes.json exists
if (!fs.existsSync(CODES_FILE)) {
    fs.writeFileSync(CODES_FILE, JSON.stringify({}), "utf8");
}

const AMOUNTS_FILE = "amounts.json";
const AMOUNTS_FILE_2 = "amounts2.json";

// Function to get current Unix timestamp
function getUnixTimestamp() {
    return Math.floor(Date.now() / 1000);
}

// Ensure amounts.json exists
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();

    // Load existing amounts
    let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8"));

    // Store the new amount
    if (!amounts[chatId]) {
        amounts[chatId] = [];
    }
    amounts[chatId].push({ amount, timestamp });

    // Save to amounts.json
    fs.writeFileSync(AMOUNTS_FILE, JSON.stringify(amounts, null, 2), "utf8");

    res.json({ chatId, amount, timestamp });
});

// API to get stored amounts for a chat ID
app.get("/get", (req, res) => {
    const chatId = req.query.chatid;

    if (!chatId) {
        return res.status(400).json({ error: "chatid is required" });
    }

    // Load existing amounts
    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] });
});

// API to generate a new code
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();

    // Load existing codes
    let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8"));

    // Store the new code
    if (!codes[chatId]) {
        codes[chatId] = [];
    }
    codes[chatId].push({ code, timestamp });

    // Save to codes.json
    fs.writeFileSync(CODES_FILE, JSON.stringify(codes, null, 2), "utf8");

    res.json({ chatId, code, timestamp });
});

// API to get recent codes for a chat ID
app.get("/api/get", (req, res) => {
    const chatId = req.query.q;
    
    if (!chatId) {
        return res.status(400).json({ error: "q (chatid) is required" });
    }

    // Load existing codes
    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();

    // Load existing amounts2
    let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE_2, "utf8"));

    // Store the new amount
    if (!amounts[chatId]) {
        amounts[chatId] = [];
    }
    amounts[chatId].push({ amount, timestamp });

    // Save to amounts2.json
    fs.writeFileSync(AMOUNTS_FILE_2, JSON.stringify(amounts, null, 2), "utf8");

    res.json({ chatId, amount, timestamp });
});

// API to get stored amounts for a chat ID (Version 2)
app.get("/get2", (req, res) => {
    const chatId = req.query.chatid;

    if (!chatId) {
        return res.status(400).json({ error: "chatid is required" });
    }

    // Load existing amounts2
    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] });
});

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