forked from wayangkulit95/iptv-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (99 loc) · 3.85 KB
/
app.js
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
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./database');
const fetch = require('node-fetch');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
// Function to send notifications to Telegram
const sendTelegramNotification = async (message) => {
const botToken = process.env.TELEGRAM_BOT_TOKEN;
const chatId = process.env.TELEGRAM_CHAT_ID;
// Corrected line with proper URL construction
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
await fetch(url, {
method: 'POST',
body: JSON.stringify({ chat_id: chatId, text: message }),
headers: { 'Content-Type': 'application/json' },
});
};
// Add new user
app.post('/admin/addUser', (req, res) => {
const { userId, userCode, expiryDate } = req.body;
// Corrected line with proper SQL query and parameters
db.run(`INSERT INTO users (userId, userCode, expiryDate) VALUES (?, ?, ?)`,
[userId, userCode, expiryDate], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`New user added: ${userId}`);
res.status(201).send({ userId });
});
});
// Check user login details
app.get('/admin/userDetails/:userId', (req, res) => {
const { userId } = req.params;
// Corrected line with proper SQL query
db.get(`SELECT * FROM users WHERE userId = ?`, [userId], (err, row) => {
if (err) {
return res.status(500).send(err.message);
}
res.send(row);
});
});
// Ban user
app.post('/admin/banUser', (req, res) => {
const { userId } = req.body;
// Corrected line with proper SQL query
db.run(`UPDATE users SET status = 'banned' WHERE userId = ?`, [userId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`User banned: ${userId}`);
res.send({ message: 'User banned successfully.' });
});
});
// Unban user
app.post('/admin/unbanUser', (req, res) => {
const { userId } = req.body;
// Corrected line with proper SQL query
db.run(`UPDATE users SET status = 'active' WHERE userId = ?`, [userId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`User unbanned: ${userId}`);
res.send({ message: 'User unbanned successfully.' });
});
});
// Renew user subscription
app.post('/admin/renewUser', (req, res) => {
const { userId, newExpiryDate } = req.body;
// Corrected line with proper SQL query
db.run(`UPDATE users SET expiryDate = ? WHERE userId = ?`, [newExpiryDate, userId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`User subscription renewed: ${userId}`);
res.send({ message: 'User subscription renewed successfully.' });
});
});
// Add credit for reseller
app.post('/reseller/addCredit', (req, res) => {
const { resellerId, amount } = req.body;
// Corrected line with proper SQL query
db.run(`UPDATE resellers SET credit = credit + ? WHERE resellerId = ?`, [amount, resellerId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
// Sending notification with the reseller ID and amount
sendTelegramNotification(`Reseller ${resellerId} credited: ${amount}`);
res.send({ message: 'Credit added successfully.' });
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});