-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy path_worker.js
1285 lines (1157 loc) · 49.7 KB
/
_worker.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let BOT_TOKEN;
let GROUP_ID;
let MAX_MESSAGES_PER_MINUTE;
let lastCleanupTime = 0;
const CLEANUP_INTERVAL = 24 * 60 * 60 * 1000; // 24 小时
let isInitialized = false;
const processedMessages = new Set();
const processedCallbacks = new Set();
const topicCreationLocks = new Map();
const settingsCache = new Map([
['verification_enabled', null],
['user_raw_enabled', null]
]);
class LRUCache {
constructor(maxSize) {
this.maxSize = maxSize;
this.cache = new Map();
}
get(key) {
const value = this.cache.get(key);
if (value !== undefined) {
this.cache.delete(key);
this.cache.set(key, value);
}
return value;
}
set(key, value) {
if (this.cache.size >= this.maxSize) {
const firstKey = this.cache.keys().next().value;
this.cache.delete(firstKey);
}
this.cache.set(key, value);
}
clear() {
this.cache.clear();
}
}
const userInfoCache = new LRUCache(1000);
const topicIdCache = new LRUCache(1000);
const userStateCache = new LRUCache(1000);
const messageRateCache = new LRUCache(1000);
export default {
async fetch(request, env) {
BOT_TOKEN = env.BOT_TOKEN_ENV || null;
GROUP_ID = env.GROUP_ID_ENV || null;
MAX_MESSAGES_PER_MINUTE = env.MAX_MESSAGES_PER_MINUTE_ENV ? parseInt(env.MAX_MESSAGES_PER_MINUTE_ENV) : 40;
if (!env.D1) {
return new Response('Server configuration error: D1 database is not bound', { status: 500 });
}
if (!isInitialized) {
await initialize(env.D1, request);
isInitialized = true;
}
async function handleRequest(request) {
if (!BOT_TOKEN || !GROUP_ID) {
return new Response('Server configuration error: Missing required environment variables', { status: 500 });
}
const url = new URL(request.url);
if (url.pathname === '/webhook') {
try {
const update = await request.json();
await handleUpdate(update);
return new Response('OK');
} catch (error) {
return new Response('Bad Request', { status: 400 });
}
} else if (url.pathname === '/registerWebhook') {
return await registerWebhook(request);
} else if (url.pathname === '/unRegisterWebhook') {
return await unRegisterWebhook();
} else if (url.pathname === '/checkTables') {
await checkAndRepairTables(env.D1);
return new Response('Database tables checked and repaired', { status: 200 });
}
return new Response('Not Found', { status: 404 });
}
async function initialize(d1, request) {
await Promise.all([
checkAndRepairTables(d1),
autoRegisterWebhook(request),
checkBotPermissions(),
cleanExpiredVerificationCodes(d1)
]);
}
async function autoRegisterWebhook(request) {
const webhookUrl = `${new URL(request.url).origin}/webhook`;
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/setWebhook`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: webhookUrl }),
});
}
async function checkBotPermissions() {
const response = await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/getChat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chat_id: GROUP_ID })
});
const data = await response.json();
if (!data.ok) {
throw new Error(`Failed to access group: ${data.description}`);
}
const memberResponse = await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/getChatMember`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: GROUP_ID,
user_id: (await getBotId())
})
});
const memberData = await memberResponse.json();
if (!memberData.ok) {
throw new Error(`Failed to get bot member status: ${memberData.description}`);
}
}
async function getBotId() {
const response = await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/getMe`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({})
});
const data = await response.json();
if (!data.ok) throw new Error(`Failed to get bot ID: ${data.description}`);
return data.result.id;
}
async function checkAndRepairTables(d1) {
const expectedTables = {
user_states: {
columns: {
chat_id: 'TEXT PRIMARY KEY',
is_blocked: 'BOOLEAN DEFAULT FALSE',
is_verified: 'BOOLEAN DEFAULT FALSE',
verified_expiry: 'INTEGER',
verification_code: 'TEXT',
code_expiry: 'INTEGER',
last_verification_message_id: 'TEXT',
is_first_verification: 'BOOLEAN DEFAULT TRUE',
is_rate_limited: 'BOOLEAN DEFAULT FALSE',
is_verifying: 'BOOLEAN DEFAULT FALSE'
}
},
message_rates: {
columns: {
chat_id: 'TEXT PRIMARY KEY',
message_count: 'INTEGER DEFAULT 0',
window_start: 'INTEGER',
start_count: 'INTEGER DEFAULT 0',
start_window_start: 'INTEGER'
}
},
chat_topic_mappings: {
columns: {
chat_id: 'TEXT PRIMARY KEY',
topic_id: 'TEXT NOT NULL'
}
},
settings: {
columns: {
key: 'TEXT PRIMARY KEY',
value: 'TEXT'
}
}
};
for (const [tableName, structure] of Object.entries(expectedTables)) {
const tableInfo = await d1.prepare(
`SELECT sql FROM sqlite_master WHERE type='table' AND name=?`
).bind(tableName).first();
if (!tableInfo) {
await createTable(d1, tableName, structure);
continue;
}
const columnsResult = await d1.prepare(
`PRAGMA table_info(${tableName})`
).all();
const currentColumns = new Map(
columnsResult.results.map(col => [col.name, {
type: col.type,
notnull: col.notnull,
dflt_value: col.dflt_value
}])
);
for (const [colName, colDef] of Object.entries(structure.columns)) {
if (!currentColumns.has(colName)) {
const columnParts = colDef.split(' ');
const addColumnSQL = `ALTER TABLE ${tableName} ADD COLUMN ${colName} ${columnParts.slice(1).join(' ')}`;
await d1.exec(addColumnSQL);
}
}
if (tableName === 'settings') {
await d1.exec('CREATE INDEX IF NOT EXISTS idx_settings_key ON settings (key)');
}
}
await Promise.all([
d1.prepare('INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)')
.bind('verification_enabled', 'true').run(),
d1.prepare('INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)')
.bind('user_raw_enabled', 'true').run()
]);
settingsCache.set('verification_enabled', (await getSetting('verification_enabled', d1)) === 'true');
settingsCache.set('user_raw_enabled', (await getSetting('user_raw_enabled', d1)) === 'true');
}
async function createTable(d1, tableName, structure) {
const columnsDef = Object.entries(structure.columns)
.map(([name, def]) => `${name} ${def}`)
.join(', ');
const createSQL = `CREATE TABLE ${tableName} (${columnsDef})`;
await d1.exec(createSQL);
}
async function cleanExpiredVerificationCodes(d1) {
const now = Date.now();
if (now - lastCleanupTime < CLEANUP_INTERVAL) {
return;
}
const nowSeconds = Math.floor(now / 1000);
const expiredCodes = await d1.prepare(
'SELECT chat_id FROM user_states WHERE code_expiry IS NOT NULL AND code_expiry < ?'
).bind(nowSeconds).all();
if (expiredCodes.results.length > 0) {
await d1.batch(
expiredCodes.results.map(({ chat_id }) =>
d1.prepare(
'UPDATE user_states SET verification_code = NULL, code_expiry = NULL, is_verifying = FALSE WHERE chat_id = ?'
).bind(chat_id)
)
);
}
lastCleanupTime = now;
}
async function handleUpdate(update) {
if (update.message) {
const messageId = update.message.message_id.toString();
const chatId = update.message.chat.id.toString();
const messageKey = `${chatId}:${messageId}`;
if (processedMessages.has(messageKey)) {
return;
}
processedMessages.add(messageKey);
if (processedMessages.size > 10000) {
processedMessages.clear();
}
await onMessage(update.message);
} else if (update.callback_query) {
await onCallbackQuery(update.callback_query);
}
}
async function onMessage(message) {
const chatId = message.chat.id.toString();
const text = message.text || '';
const messageId = message.message_id;
if (chatId === GROUP_ID) {
const topicId = message.message_thread_id;
if (topicId) {
const privateChatId = await getPrivateChatId(topicId);
if (privateChatId && text === '/admin') {
await sendAdminPanel(chatId, topicId, privateChatId, messageId);
return;
}
if (privateChatId && text.startsWith('/reset_user')) {
await handleResetUser(chatId, topicId, text);
return;
}
if (privateChatId) {
await forwardMessageToPrivateChat(privateChatId, message);
}
}
return;
}
let userState = userStateCache.get(chatId);
if (userState === undefined) {
userState = await env.D1.prepare('SELECT is_blocked, is_first_verification, is_verified, verified_expiry, is_verifying FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first();
if (!userState) {
userState = { is_blocked: false, is_first_verification: true, is_verified: false, verified_expiry: null, is_verifying: false };
await env.D1.prepare('INSERT INTO user_states (chat_id, is_blocked, is_first_verification, is_verified, is_verifying) VALUES (?, ?, ?, ?, ?)')
.bind(chatId, false, true, false, false)
.run();
}
userStateCache.set(chatId, userState);
}
if (userState.is_blocked) {
await sendMessageToUser(chatId, "您已被拉黑,无法发送消息。请联系管理员解除拉黑。");
return;
}
const verificationEnabled = (await getSetting('verification_enabled', env.D1)) === 'true';
if (!verificationEnabled) {
// 验证码关闭时,所有用户都可以直接发送消息
} else {
const nowSeconds = Math.floor(Date.now() / 1000);
const isVerified = userState.is_verified && userState.verified_expiry && nowSeconds < userState.verified_expiry;
const isFirstVerification = userState.is_first_verification;
const isRateLimited = await checkMessageRate(chatId);
const isVerifying = userState.is_verifying || false;
if (!isVerified || (isRateLimited && !isFirstVerification)) {
if (isVerifying) {
// 检查验证码是否已过期
const storedCode = await env.D1.prepare('SELECT verification_code, code_expiry FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first();
const nowSeconds = Math.floor(Date.now() / 1000);
const isCodeExpired = !storedCode?.verification_code || !storedCode?.code_expiry || nowSeconds > storedCode.code_expiry;
if (isCodeExpired) {
// 如果验证码已过期,重新发送验证码
await sendMessageToUser(chatId, '验证码已过期,正在为您发送新的验证码...');
await env.D1.prepare('UPDATE user_states SET verification_code = NULL, code_expiry = NULL, is_verifying = FALSE WHERE chat_id = ?')
.bind(chatId)
.run();
userStateCache.set(chatId, { ...userState, verification_code: null, code_expiry: null, is_verifying: false });
// 删除旧的验证消息(如果存在)
try {
const lastVerification = await env.D1.prepare('SELECT last_verification_message_id FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first();
if (lastVerification?.last_verification_message_id) {
try {
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/deleteMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
message_id: lastVerification.last_verification_message_id
})
});
} catch (deleteError) {
console.log(`删除旧验证消息失败: ${deleteError.message}`);
// 删除失败仍继续处理
}
await env.D1.prepare('UPDATE user_states SET last_verification_message_id = NULL WHERE chat_id = ?')
.bind(chatId)
.run();
}
} catch (error) {
console.log(`查询旧验证消息失败: ${error.message}`);
// 即使出错也继续处理
}
// 立即发送新的验证码
try {
await handleVerification(chatId, 0);
} catch (verificationError) {
console.error(`发送新验证码失败: ${verificationError.message}`);
// 如果发送验证码失败,则再次尝试
setTimeout(async () => {
try {
await handleVerification(chatId, 0);
} catch (retryError) {
console.error(`重试发送验证码仍失败: ${retryError.message}`);
await sendMessageToUser(chatId, '发送验证码失败,请发送任意消息重试');
}
}, 1000);
}
return;
} else {
await sendMessageToUser(chatId, `请完成验证后发送消息"${text || '您的具体信息'}"。`);
}
return;
}
await sendMessageToUser(chatId, `请完成验证后发送消息"${text || '您的具体信息'}"。`);
await handleVerification(chatId, messageId);
return;
}
}
if (text === '/start') {
if (await checkStartCommandRate(chatId)) {
await sendMessageToUser(chatId, "您发送 /start 命令过于频繁,请稍后再试!");
return;
}
const successMessage = await getVerificationSuccessMessage();
await sendMessageToUser(chatId, `${successMessage}\n你好,欢迎使用私聊机器人,现在发送信息吧!`);
const userInfo = await getUserInfo(chatId);
await ensureUserTopic(chatId, userInfo);
return;
}
const userInfo = await getUserInfo(chatId);
if (!userInfo) {
await sendMessageToUser(chatId, "无法获取用户信息,请稍后再试或联系管理员。");
return;
}
let topicId = await ensureUserTopic(chatId, userInfo);
if (!topicId) {
await sendMessageToUser(chatId, "无法创建话题,请稍后再试或联系管理员。");
return;
}
const isTopicValid = await validateTopic(topicId);
if (!isTopicValid) {
await env.D1.prepare('DELETE FROM chat_topic_mappings WHERE chat_id = ?').bind(chatId).run();
topicIdCache.set(chatId, undefined);
topicId = await ensureUserTopic(chatId, userInfo);
if (!topicId) {
await sendMessageToUser(chatId, "无法重新创建话题,请稍后再试或联系管理员。");
return;
}
}
const userName = userInfo.username || `User_${chatId}`;
const nickname = userInfo.nickname || userName;
if (text) {
const formattedMessage = `${nickname}:\n${text}`;
await sendMessageToTopic(topicId, formattedMessage);
} else {
await copyMessageToTopic(topicId, message);
}
}
async function validateTopic(topicId) {
try {
const response = await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: GROUP_ID,
message_thread_id: topicId,
text: "您有新消息!",
disable_notification: true
})
});
const data = await response.json();
if (data.ok) {
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/deleteMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: GROUP_ID,
message_id: data.result.message_id
})
});
return true;
}
return false;
} catch (error) {
return false;
}
}
async function ensureUserTopic(chatId, userInfo) {
let lock = topicCreationLocks.get(chatId);
if (!lock) {
lock = Promise.resolve();
topicCreationLocks.set(chatId, lock);
}
try {
await lock;
let topicId = await getExistingTopicId(chatId);
if (topicId) {
return topicId;
}
const newLock = (async () => {
const userName = userInfo.username || `User_${chatId}`;
const nickname = userInfo.nickname || userName;
topicId = await createForumTopic(nickname, userName, nickname, userInfo.id || chatId);
await saveTopicId(chatId, topicId);
return topicId;
})();
topicCreationLocks.set(chatId, newLock);
return await newLock;
} finally {
if (topicCreationLocks.get(chatId) === lock) {
topicCreationLocks.delete(chatId);
}
}
}
async function handleResetUser(chatId, topicId, text) {
const senderId = chatId;
const isAdmin = await checkIfAdmin(senderId);
if (!isAdmin) {
await sendMessageToTopic(topicId, '只有管理员可以使用此功能。');
return;
}
const parts = text.split(' ');
if (parts.length !== 2) {
await sendMessageToTopic(topicId, '用法:/reset_user <chat_id>');
return;
}
const targetChatId = parts[1];
await env.D1.batch([
env.D1.prepare('DELETE FROM user_states WHERE chat_id = ?').bind(targetChatId),
env.D1.prepare('DELETE FROM message_rates WHERE chat_id = ?').bind(targetChatId),
env.D1.prepare('DELETE FROM chat_topic_mappings WHERE chat_id = ?').bind(targetChatId)
]);
userStateCache.set(targetChatId, undefined);
messageRateCache.set(targetChatId, undefined);
topicIdCache.set(targetChatId, undefined);
await sendMessageToTopic(topicId, `用户 ${targetChatId} 的状态已重置。`);
}
async function sendAdminPanel(chatId, topicId, privateChatId, messageId) {
const verificationEnabled = (await getSetting('verification_enabled', env.D1)) === 'true';
const userRawEnabled = (await getSetting('user_raw_enabled', env.D1)) === 'true';
const buttons = [
[
{ text: '拉黑用户', callback_data: `block_${privateChatId}` },
{ text: '解除拉黑', callback_data: `unblock_${privateChatId}` }
],
[
{ text: verificationEnabled ? '关闭验证码' : '开启验证码', callback_data: `toggle_verification_${privateChatId}` },
{ text: '查询黑名单', callback_data: `check_blocklist_${privateChatId}` }
],
[
{ text: userRawEnabled ? '关闭用户Raw' : '开启用户Raw', callback_data: `toggle_user_raw_${privateChatId}` },
{ text: 'GitHub项目', url: 'https://github.com/iawooo/ctt' }
],
[
{ text: '删除用户', callback_data: `delete_user_${privateChatId}` }
]
];
const adminMessage = '管理员面板:请选择操作';
await Promise.all([
fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
message_thread_id: topicId,
text: adminMessage,
reply_markup: { inline_keyboard: buttons }
})
}),
fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/deleteMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
message_id: messageId
})
})
]);
}
async function getVerificationSuccessMessage() {
const userRawEnabled = (await getSetting('user_raw_enabled', env.D1)) === 'true';
if (!userRawEnabled) return '验证成功!您现在可以与我聊天。';
const response = await fetch('https://raw.githubusercontent.com/iawooo/ctt/refs/heads/main/CFTeleTrans/start.md');
if (!response.ok) return '验证成功!您现在可以与我聊天。';
const message = await response.text();
return message.trim() || '验证成功!您现在可以与我聊天。';
}
async function getNotificationContent() {
const response = await fetch('https://raw.githubusercontent.com/iawooo/ctt/refs/heads/main/CFTeleTrans/notification.md');
if (!response.ok) return '';
const content = await response.text();
return content.trim() || '';
}
async function checkStartCommandRate(chatId) {
const now = Date.now();
const window = 5 * 60 * 1000;
const maxStartsPerWindow = 1;
let data = messageRateCache.get(chatId);
if (data === undefined) {
data = await env.D1.prepare('SELECT start_count, start_window_start FROM message_rates WHERE chat_id = ?')
.bind(chatId)
.first();
if (!data) {
data = { start_count: 0, start_window_start: now };
await env.D1.prepare('INSERT INTO message_rates (chat_id, start_count, start_window_start) VALUES (?, ?, ?)')
.bind(chatId, data.start_count, data.start_window_start)
.run();
}
messageRateCache.set(chatId, data);
}
if (now - data.start_window_start > window) {
data.start_count = 1;
data.start_window_start = now;
await env.D1.prepare('UPDATE message_rates SET start_count = ?, start_window_start = ? WHERE chat_id = ?')
.bind(data.start_count, data.start_window_start, chatId)
.run();
} else {
data.start_count += 1;
await env.D1.prepare('UPDATE message_rates SET start_count = ? WHERE chat_id = ?')
.bind(data.start_count, chatId)
.run();
}
messageRateCache.set(chatId, data);
return data.start_count > maxStartsPerWindow;
}
async function checkMessageRate(chatId) {
const now = Date.now();
const window = 60 * 1000;
let data = messageRateCache.get(chatId);
if (data === undefined) {
data = await env.D1.prepare('SELECT message_count, window_start FROM message_rates WHERE chat_id = ?')
.bind(chatId)
.first();
if (!data) {
data = { message_count: 0, window_start: now };
await env.D1.prepare('INSERT INTO message_rates (chat_id, message_count, window_start) VALUES (?, ?, ?)')
.bind(chatId, data.message_count, data.window_start)
.run();
}
messageRateCache.set(chatId, data);
}
if (now - data.window_start > window) {
data.message_count = 1;
data.window_start = now;
} else {
data.message_count += 1;
}
messageRateCache.set(chatId, data);
await env.D1.prepare('UPDATE message_rates SET message_count = ?, window_start = ? WHERE chat_id = ?')
.bind(data.message_count, data.window_start, chatId)
.run();
return data.message_count > MAX_MESSAGES_PER_MINUTE;
}
async function getSetting(key, d1) {
const result = await d1.prepare('SELECT value FROM settings WHERE key = ?')
.bind(key)
.first();
return result?.value || null;
}
async function setSetting(key, value) {
await env.D1.prepare('INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)')
.bind(key, value)
.run();
if (key === 'verification_enabled') {
settingsCache.set('verification_enabled', value === 'true');
if (value === 'false') {
const nowSeconds = Math.floor(Date.now() / 1000);
const verifiedExpiry = nowSeconds + 3600 * 24;
await env.D1.prepare('UPDATE user_states SET is_verified = ?, verified_expiry = ?, is_verifying = ?, verification_code = NULL, code_expiry = NULL, is_first_verification = ? WHERE chat_id NOT IN (SELECT chat_id FROM user_states WHERE is_blocked = TRUE)')
.bind(true, verifiedExpiry, false, false)
.run();
userStateCache.clear();
}
} else if (key === 'user_raw_enabled') {
settingsCache.set('user_raw_enabled', value === 'true');
}
}
async function onCallbackQuery(callbackQuery) {
const chatId = callbackQuery.message.chat.id.toString();
const topicId = callbackQuery.message.message_thread_id;
const data = callbackQuery.data;
const messageId = callbackQuery.message.message_id;
const callbackKey = `${chatId}:${callbackQuery.id}`;
if (processedCallbacks.has(callbackKey)) {
return;
}
processedCallbacks.add(callbackKey);
const parts = data.split('_');
let action;
let privateChatId;
if (data.startsWith('verify_')) {
action = 'verify';
privateChatId = parts[1];
} else if (data.startsWith('toggle_verification_')) {
action = 'toggle_verification';
privateChatId = parts.slice(2).join('_');
} else if (data.startsWith('toggle_user_raw_')) {
action = 'toggle_user_raw';
privateChatId = parts.slice(3).join('_');
} else if (data.startsWith('check_blocklist_')) {
action = 'check_blocklist';
privateChatId = parts.slice(2).join('_');
} else if (data.startsWith('block_')) {
action = 'block';
privateChatId = parts.slice(1).join('_');
} else if (data.startsWith('unblock_')) {
action = 'unblock';
privateChatId = parts.slice(1).join('_');
} else if (data.startsWith('delete_user_')) {
action = 'delete_user';
privateChatId = parts.slice(2).join('_');
} else {
action = data;
privateChatId = '';
}
if (action === 'verify') {
const [, userChatId, selectedAnswer, result] = data.split('_');
if (userChatId !== chatId) {
return;
}
let verificationState = userStateCache.get(chatId);
if (verificationState === undefined) {
verificationState = await env.D1.prepare('SELECT verification_code, code_expiry, is_verifying FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first();
if (!verificationState) {
verificationState = { verification_code: null, code_expiry: null, is_verifying: false };
}
userStateCache.set(chatId, verificationState);
}
const storedCode = verificationState.verification_code;
const codeExpiry = verificationState.code_expiry;
const nowSeconds = Math.floor(Date.now() / 1000);
if (!storedCode || (codeExpiry && nowSeconds > codeExpiry)) {
await sendMessageToUser(chatId, '验证码已过期,正在为您发送新的验证码...');
await env.D1.prepare('UPDATE user_states SET verification_code = NULL, code_expiry = NULL, is_verifying = FALSE WHERE chat_id = ?')
.bind(chatId)
.run();
userStateCache.set(chatId, { ...verificationState, verification_code: null, code_expiry: null, is_verifying: false });
// 删除旧的验证消息
try {
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/deleteMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
message_id: messageId
})
});
} catch (error) {
console.log(`删除过期验证按钮失败: ${error.message}`);
// 即使删除失败也继续处理
}
// 立即发送新的验证码
try {
await handleVerification(chatId, 0);
} catch (verificationError) {
console.error(`发送新验证码失败: ${verificationError.message}`);
// 如果发送验证码失败,则再次尝试
setTimeout(async () => {
try {
await handleVerification(chatId, 0);
} catch (retryError) {
console.error(`重试发送验证码仍失败: ${retryError.message}`);
await sendMessageToUser(chatId, '发送验证码失败,请发送任意消息重试');
}
}, 1000);
}
return;
}
if (result === 'correct') {
const verifiedExpiry = nowSeconds + 3600 * 24;
await env.D1.prepare('UPDATE user_states SET is_verified = ?, verified_expiry = ?, verification_code = NULL, code_expiry = NULL, last_verification_message_id = NULL, is_first_verification = ?, is_verifying = ? WHERE chat_id = ?')
.bind(true, verifiedExpiry, false, false, chatId)
.run();
verificationState = await env.D1.prepare('SELECT is_verified, verified_expiry, verification_code, code_expiry, last_verification_message_id, is_first_verification, is_verifying FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first();
userStateCache.set(chatId, verificationState);
let rateData = await env.D1.prepare('SELECT message_count, window_start FROM message_rates WHERE chat_id = ?')
.bind(chatId)
.first() || { message_count: 0, window_start: nowSeconds * 1000 };
rateData.message_count = 0;
rateData.window_start = nowSeconds * 1000;
messageRateCache.set(chatId, rateData);
await env.D1.prepare('UPDATE message_rates SET message_count = ?, window_start = ? WHERE chat_id = ?')
.bind(0, nowSeconds * 1000, chatId)
.run();
const successMessage = await getVerificationSuccessMessage();
await sendMessageToUser(chatId, `${successMessage}\n你好,欢迎使用私聊机器人!现在可以发送消息了。`);
const userInfo = await getUserInfo(chatId);
await ensureUserTopic(chatId, userInfo);
} else {
await sendMessageToUser(chatId, '验证失败,请重新尝试。');
await handleVerification(chatId, messageId);
}
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/deleteMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
message_id: messageId
})
});
} else {
const senderId = callbackQuery.from.id.toString();
const isAdmin = await checkIfAdmin(senderId);
if (!isAdmin) {
await sendMessageToTopic(topicId, '只有管理员可以使用此功能。');
await sendAdminPanel(chatId, topicId, privateChatId, messageId);
return;
}
if (action === 'block') {
let state = userStateCache.get(privateChatId);
if (state === undefined) {
state = await env.D1.prepare('SELECT is_blocked FROM user_states WHERE chat_id = ?')
.bind(privateChatId)
.first() || { is_blocked: false };
}
state.is_blocked = true;
userStateCache.set(privateChatId, state);
await env.D1.prepare('INSERT OR REPLACE INTO user_states (chat_id, is_blocked) VALUES (?, ?)')
.bind(privateChatId, true)
.run();
await sendMessageToTopic(topicId, `用户 ${privateChatId} 已被拉黑,消息将不再转发。`);
} else if (action === 'unblock') {
let state = userStateCache.get(privateChatId);
if (state === undefined) {
state = await env.D1.prepare('SELECT is_blocked, is_first_verification FROM user_states WHERE chat_id = ?')
.bind(privateChatId)
.first() || { is_blocked: false, is_first_verification: true };
}
state.is_blocked = false;
state.is_first_verification = true;
userStateCache.set(privateChatId, state);
await env.D1.prepare('INSERT OR REPLACE INTO user_states (chat_id, is_blocked, is_first_verification) VALUES (?, ?, ?)')
.bind(privateChatId, false, true)
.run();
await sendMessageToTopic(topicId, `用户 ${privateChatId} 已解除拉黑,消息将继续转发。`);
} else if (action === 'toggle_verification') {
const currentState = (await getSetting('verification_enabled', env.D1)) === 'true';
const newState = !currentState;
await setSetting('verification_enabled', newState.toString());
await sendMessageToTopic(topicId, `验证码功能已${newState ? '开启' : '关闭'}。`);
} else if (action === 'check_blocklist') {
const blockedUsers = await env.D1.prepare('SELECT chat_id FROM user_states WHERE is_blocked = ?')
.bind(true)
.all();
const blockList = blockedUsers.results.length > 0
? blockedUsers.results.map(row => row.chat_id).join('\n')
: '当前没有被拉黑的用户。';
await sendMessageToTopic(topicId, `黑名单列表:\n${blockList}`);
} else if (action === 'toggle_user_raw') {
const currentState = (await getSetting('user_raw_enabled', env.D1)) === 'true';
const newState = !currentState;
await setSetting('user_raw_enabled', newState.toString());
await sendMessageToTopic(topicId, `用户端 Raw 链接已${newState ? '开启' : '关闭'}。`);
} else if (action === 'delete_user') {
userStateCache.set(privateChatId, undefined);
messageRateCache.set(privateChatId, undefined);
topicIdCache.set(privateChatId, undefined);
await env.D1.batch([
env.D1.prepare('DELETE FROM user_states WHERE chat_id = ?').bind(privateChatId),
env.D1.prepare('DELETE FROM message_rates WHERE chat_id = ?').bind(privateChatId),
env.D1.prepare('DELETE FROM chat_topic_mappings WHERE chat_id = ?').bind(privateChatId)
]);
await sendMessageToTopic(topicId, `用户 ${privateChatId} 的状态、消息记录和话题映射已删除,用户需重新发起会话。`);
} else {
await sendMessageToTopic(topicId, `未知操作:${action}`);
}
await sendAdminPanel(chatId, topicId, privateChatId, messageId);
}
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/answerCallbackQuery`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
callback_query_id: callbackQuery.id
})
});
}
async function handleVerification(chatId, messageId) {
try {
let userState = userStateCache.get(chatId);
if (userState === undefined) {
userState = await env.D1.prepare('SELECT is_blocked, is_first_verification, is_verified, verified_expiry, is_verifying FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first();
if (!userState) {
userState = { is_blocked: false, is_first_verification: true, is_verified: false, verified_expiry: null, is_verifying: false };
}
userStateCache.set(chatId, userState);
}
userState.verification_code = null;
userState.code_expiry = null;
userState.is_verifying = true;
userStateCache.set(chatId, userState);
await env.D1.prepare('UPDATE user_states SET verification_code = NULL, code_expiry = NULL, is_verifying = ? WHERE chat_id = ?')
.bind(true, chatId)
.run();
const lastVerification = userState.last_verification_message_id || (await env.D1.prepare('SELECT last_verification_message_id FROM user_states WHERE chat_id = ?')
.bind(chatId)
.first())?.last_verification_message_id;
if (lastVerification) {
try {
await fetchWithRetry(`https://api.telegram.org/bot${BOT_TOKEN}/deleteMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
message_id: lastVerification
})
});
} catch (deleteError) {
console.log(`删除上一条验证消息失败: ${deleteError.message}`);
// 继续处理,即使删除失败
}
userState.last_verification_message_id = null;
userStateCache.set(chatId, userState);
await env.D1.prepare('UPDATE user_states SET last_verification_message_id = NULL WHERE chat_id = ?')
.bind(chatId)
.run();
}
// 确保发送验证码
await sendVerification(chatId);
} catch (error) {
console.error(`处理验证过程失败: ${error.message}`);
// 重置用户状态以防卡住
try {
await env.D1.prepare('UPDATE user_states SET is_verifying = FALSE WHERE chat_id = ?')
.bind(chatId)
.run();
let currentState = userStateCache.get(chatId);
if (currentState) {
currentState.is_verifying = false;
userStateCache.set(chatId, currentState);
}
} catch (resetError) {
console.error(`重置用户验证状态失败: ${resetError.message}`);
}
throw error; // 向上传递错误以便调用方处理
}
}
async function sendVerification(chatId) {
try {
const num1 = Math.floor(Math.random() * 10);
const num2 = Math.floor(Math.random() * 10);
const operation = Math.random() > 0.5 ? '+' : '-';
const correctResult = operation === '+' ? num1 + num2 : num1 - num2;
const options = new Set([correctResult]);
while (options.size < 4) {
const wrongResult = correctResult + Math.floor(Math.random() * 5) - 2;
if (wrongResult !== correctResult) options.add(wrongResult);
}
const optionArray = Array.from(options).sort(() => Math.random() - 0.5);