-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
280 lines (227 loc) · 7.33 KB
/
Copy pathbot.js
File metadata and controls
280 lines (227 loc) · 7.33 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { Telegraf } from "telegraf";
import dotenv from 'dotenv';
import { fetchCallbackMessage, fetchOnBoardMessages, fetchOnBoardMessagesOnRequest } from "./controllers/messageController.js";
import { saveBotUser } from "./controllers/userController.js";
import startDailyAlerts from "./cron/dailyAlerts.js";
import startBroadcast from "./cron/broadcasts.js";
dotenv.config();
const webAppUrl = process.env.WEBAPP_URL;
const welcome = process.env.WELCOME_FILE_ID || "";
const bot = new Telegraf(process.env.BOT_TOKEN);
const managerId = process.env.MANAGER_ID || "000000";
const botRole = process.env.BOT_ROLE || "APP";
const SEEN_USERS_MAX = 500;
const seenUsers = new Map();
// ============================================
// IMPROVED RATE LIMITING
// ============================================
const rateLimitStore = new Map();
const RATE_LIMITS = {
start: { window: 10000, maxAttempts: 3 }, // 3 /start in 10 seconds
callback: { window: 2000, maxAttempts: 5 }, // 5 callbacks in 2 seconds
message: { window: 5000, maxAttempts: 10 } // 10 messages in 5 seconds
};
function checkRateLimit(userId, action = 'message') {
const config = RATE_LIMITS[action];
const now = Date.now();
const key = `${userId}:${action}`;
if (!rateLimitStore.has(key)) {
rateLimitStore.set(key, { attempts: 1, firstAttempt: now });
return { allowed: true, remaining: config.maxAttempts - 1 };
}
const data = rateLimitStore.get(key);
const timeElapsed = now - data.firstAttempt;
// Reset window if expired
if (timeElapsed > config.window) {
rateLimitStore.set(key, { attempts: 1, firstAttempt: now });
return { allowed: true, remaining: config.maxAttempts - 1 };
}
// Check if limit exceeded
if (data.attempts >= config.maxAttempts) {
const waitTime = Math.ceil((config.window - timeElapsed) / 1000);
return {
allowed: false,
remaining: 0,
waitTime,
resetIn: config.window - timeElapsed
};
}
// Increment attempts
data.attempts++;
return { allowed: true, remaining: config.maxAttempts - data.attempts };
}
// Cleanup old entries every 2 minutes
setInterval(() => {
const now = Date.now();
for (const [key, data] of rateLimitStore.entries()) {
const action = key.split(':')[1];
const config = RATE_LIMITS[action] || RATE_LIMITS.message;
if (now - data.firstAttempt > config.window * 2) {
rateLimitStore.delete(key);
}
}
}, 120000);
// ============================================
// SEEN USERS CACHE
// ============================================
function hasSeenUser(userId) {
return seenUsers.has(userId);
}
function markSeenUser(userId) {
if (seenUsers.has(userId)) {
seenUsers.delete(userId);
}
seenUsers.set(userId, true);
if (seenUsers.size > SEEN_USERS_MAX) {
const oldestKey = seenUsers.keys().next().value;
seenUsers.delete(oldestKey);
}
}
// ============================================
// BOT HANDLERS
// ============================================
bot.start(async (ctx) => {
const userId = ctx.from.id;
// Check rate limit for /start
const rateCheck = checkRateLimit(userId, 'start');
if (!rateCheck.allowed) {
console.log(`⏱️ Rate limited /start: ${userId} (wait ${rateCheck.waitTime}s)`);
// Provide helpful feedback
await ctx.reply(
`⏳ Please wait ${rateCheck.waitTime} seconds before using /start again.`,
{ parse_mode: "Markdown" }
).catch(() => {});
return;
}
let caption = `*Welcome aboard, ${ctx.from.first_name}!*
*Manager #${managerId} selected successfully*
👤 *Manager: Calvin Trades*
_You've just joined a transparent, performance-driven trading ecosystem built for long-term consistency._
Tap below to open the WebApp ⬇️`;
try {
if(botRole === "APP") {
welcome ?
await ctx.replyWithVideo(
welcome,
{
caption,
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: [
[{ text: "Open Platform", web_app: { url: webAppUrl } }],
],
},
}
)
: await ctx.reply(
caption,
{
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: [
[{ text: "Open Platform", web_app: { url: webAppUrl } }],
],
},
}
)
}
if (!hasSeenUser(userId)) {
saveBotUser(ctx)
.then((res) => {
if (res) {
markSeenUser(userId);
console.log(`✅ User ${userId} saved`);
}
})
.catch((err) => {
console.error(`❌ Save user ${userId} failed:`, err.message);
});
}
if(botRole === "TRADER"){
fetchOnBoardMessages(ctx).catch((err) => {
console.error(`❌ Onboard messages for ${userId} failed:`, err.message);
});
}
} catch (error) {
console.error("❌ Start command error:", error.message);
ctx.reply("Welcome! Please try again.").catch(() => {});
}
});
bot.on("message", (ctx) => {
const userId = ctx.from.id;
const rateCheck = checkRateLimit(userId, 'message');
if (!rateCheck.allowed) {
console.log(`⏱️ Rate limited message: ${userId}`);
// Don't reply - just ignore excessive messages
return;
}
const msg = ctx.message;
const fileId =
msg.photo?.at(-1)?.file_id ||
msg.document?.file_id ||
msg.video?.file_id ||
msg.audio?.file_id ||
msg.voice?.file_id;
if (fileId) {
console.log("📦 File ID:", fileId);
}
});
bot.on("chat_join_request", async (ctx) => {
await fetchOnBoardMessagesOnRequest(ctx);
});
bot.on("callback_query", async (ctx) => {
const userId = ctx.from.id;
const rateCheck = checkRateLimit(userId, 'callback');
if (!rateCheck.allowed) {
console.log(`⏱️ Rate limited callback: ${userId}`);
await ctx.answerCbQuery(
`⏳ Too many clicks! Wait ${rateCheck.waitTime}s`,
{ show_alert: false }
).catch(() => {});
return;
}
await fetchCallbackMessage(ctx);
});
// Global error handler
bot.catch((err, ctx) => {
console.error(`❌ Bot error for ${ctx.updateType}:`, err);
ctx.reply("Something went wrong. Please try again.").catch(() => {});
});
// Graceful shutdown
const shutdown = async (signal) => {
console.log(`\n🛑 ${signal} received. Shutting down...`);
try {
await bot.stop(signal);
console.log("✅ Bot stopped gracefully");
process.exit(0);
} catch (error) {
console.error("❌ Error during shutdown:", error);
process.exit(1);
}
};
process.once('SIGINT', () => shutdown('SIGINT'));
process.once('SIGTERM', () => shutdown('SIGTERM'));
process.on('uncaughtException', (error) => {
console.error('💥 Uncaught Exception:', error);
shutdown('UNCAUGHT_EXCEPTION');
});
process.on('unhandledRejection', (error) => {
console.error('💥 Unhandled Rejection:', error);
});
console.log("🚀 Starting Telegram Bot...");
console.log(`🔗 WebApp: ${webAppUrl}`);
bot.launch();
bot.telegram.getMe()
.then((me) => {
console.log("✅ Bot is online");
console.log(`🤖 Username: @${me.username}`);
if(botRole === "APP"){
startDailyAlerts(bot);
}
if(botRole === "TRADER"){
startBroadcast(bot);
}
})
.catch(err => {
console.error("❌ Bot startup verification failed:", err.message);
});