@xmanrui/dsh-im 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +341 -37
- package/lib/index.js +148 -148
- package/package.json +1 -1
- package/plugin-src/client/channels/feishu/api.js +23 -5
- package/plugin-src/client/channels/feishu/index.js +291 -39
- package/plugin-src/client/channels/feishu/styles.js +46 -0
- package/plugin-src/client/i18n.js +50 -0
- package/plugin-src/host/channels/feishu/production.mjs +2 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +61 -7
- package/src/channels/discord/discord-api.mjs +1 -1
- package/src/channels/feishu/bridge.mjs +29 -4
- package/src/channels/feishu/feishu-runtime.mjs +14 -0
- package/src/channels/feishu/group-message-permission-manager.mjs +71 -0
- package/src/channels/feishu/group-response-mode.mjs +15 -0
- package/src/channels/feishu/multi-bot-controller.mjs +258 -9
- package/src/channels/feishu/plugin-config-store.mjs +3 -0
- package/src/channels/feishu/repair-manager.mjs +17 -3
- package/src/channels/telegram/telegram-api.mjs +31 -0
- package/src/channels/telegram/telegram-runtime.mjs +25 -1
- package/src/channels/weixin/weixin-api.mjs +1 -1
|
@@ -5,12 +5,24 @@ import {
|
|
|
5
5
|
CALLBACK_REPAIR_OPERATION,
|
|
6
6
|
CallbackRepairManager,
|
|
7
7
|
} from './repair-manager.mjs';
|
|
8
|
+
import {
|
|
9
|
+
GROUP_MESSAGE_PERMISSION_OPERATION,
|
|
10
|
+
GroupMessagePermissionManager,
|
|
11
|
+
} from './group-message-permission-manager.mjs';
|
|
8
12
|
import { REQUIRED_TENANT_SCOPES } from './plugin-controller.mjs';
|
|
13
|
+
import {
|
|
14
|
+
isFeishuGroupResponseMode,
|
|
15
|
+
normalizeFeishuGroupResponseMode,
|
|
16
|
+
} from './group-response-mode.mjs';
|
|
9
17
|
|
|
10
18
|
const ACTIVE_REGISTRATION_STATES = new Set([
|
|
11
19
|
'starting', 'qr_ready', 'polling', 'slow_down', 'domain_switched',
|
|
12
20
|
]);
|
|
13
21
|
const MUTABLE_REGISTRATION_STATES = new Set([...ACTIVE_REGISTRATION_STATES, 'saving']);
|
|
22
|
+
const TARGETED_APP_UPDATE_OPERATIONS = new Set([
|
|
23
|
+
CALLBACK_REPAIR_OPERATION,
|
|
24
|
+
GROUP_MESSAGE_PERMISSION_OPERATION,
|
|
25
|
+
]);
|
|
14
26
|
const ALL_VISIBLE_SENDERS = '*';
|
|
15
27
|
const DEFAULT_CALLBACK_PROBE_TIMEOUT_MS = 120_000;
|
|
16
28
|
const MAX_CALLBACK_PROBE_TIMEOUT_MS = 600_000;
|
|
@@ -76,6 +88,8 @@ function configuredBotFingerprint(config) {
|
|
|
76
88
|
botName: config.botName,
|
|
77
89
|
botOpenId: config.botOpenId,
|
|
78
90
|
activated: config.activated,
|
|
91
|
+
groupResponseMode: normalizeFeishuGroupResponseMode(config.groupResponseMode),
|
|
92
|
+
groupMessagePermissionGranted: config.groupMessagePermissionGranted === true,
|
|
79
93
|
deletionPending: config.deletionPending === true,
|
|
80
94
|
connectedAt: config.connectedAt ?? null,
|
|
81
95
|
createdAt: config.createdAt ?? null,
|
|
@@ -103,7 +117,7 @@ export class MultiBotDshFeishuController {
|
|
|
103
117
|
#runtimes = new Map();
|
|
104
118
|
#botErrors = new Map();
|
|
105
119
|
#registrations = new Map();
|
|
106
|
-
#
|
|
120
|
+
#activeAppUpdates = new Map();
|
|
107
121
|
#botOwnership = new Map();
|
|
108
122
|
#latestRegistrationId = null;
|
|
109
123
|
#configTransition = Promise.resolve();
|
|
@@ -234,12 +248,15 @@ export class MultiBotDshFeishuController {
|
|
|
234
248
|
const target = this.#requireBot(botId);
|
|
235
249
|
if (target.deletionPending) throw new Error('Cannot repair a Feishu bot pending deletion');
|
|
236
250
|
|
|
237
|
-
const activeId = this.#
|
|
251
|
+
const activeId = this.#activeAppUpdates.get(botId);
|
|
238
252
|
const active = activeId ? this.#registrations.get(activeId) : null;
|
|
239
253
|
if (active && MUTABLE_REGISTRATION_STATES.has(active.manager.status().state)) {
|
|
254
|
+
if (active.operation !== CALLBACK_REPAIR_OPERATION) {
|
|
255
|
+
throw new Error('Another Feishu app update is already active for this bot');
|
|
256
|
+
}
|
|
240
257
|
return this.registrationStatus(active.id);
|
|
241
258
|
}
|
|
242
|
-
this.#
|
|
259
|
+
this.#activeAppUpdates.delete(botId);
|
|
243
260
|
|
|
244
261
|
const id = this.#createRegistrationId();
|
|
245
262
|
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(id) || this.#registrations.has(id)) {
|
|
@@ -278,7 +295,66 @@ export class MultiBotDshFeishuController {
|
|
|
278
295
|
},
|
|
279
296
|
});
|
|
280
297
|
this.#registrations.set(id, record);
|
|
281
|
-
this.#
|
|
298
|
+
this.#activeAppUpdates.set(botId, id);
|
|
299
|
+
this.#latestRegistrationId = id;
|
|
300
|
+
this.#trimRegistrations();
|
|
301
|
+
record.manager.start();
|
|
302
|
+
this.#touch();
|
|
303
|
+
return this.registrationStatus(id);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
startGroupMessagePermission(botId) {
|
|
307
|
+
this.#assertOpen();
|
|
308
|
+
const target = this.#requireBot(botId);
|
|
309
|
+
if (target.deletionPending) {
|
|
310
|
+
throw new Error('Cannot update permissions for a Feishu bot pending deletion');
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const activeId = this.#activeAppUpdates.get(botId);
|
|
314
|
+
const active = activeId ? this.#registrations.get(activeId) : null;
|
|
315
|
+
if (active && MUTABLE_REGISTRATION_STATES.has(active.manager.status().state)) {
|
|
316
|
+
if (active.operation !== GROUP_MESSAGE_PERMISSION_OPERATION) {
|
|
317
|
+
throw new Error('Another Feishu app update is already active for this bot');
|
|
318
|
+
}
|
|
319
|
+
return this.registrationStatus(active.id);
|
|
320
|
+
}
|
|
321
|
+
this.#activeAppUpdates.delete(botId);
|
|
322
|
+
|
|
323
|
+
const id = this.#createRegistrationId();
|
|
324
|
+
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(id) || this.#registrations.has(id)) {
|
|
325
|
+
throw new Error('Registration id generator returned an invalid or duplicate id');
|
|
326
|
+
}
|
|
327
|
+
const record = {
|
|
328
|
+
id,
|
|
329
|
+
operation: GROUP_MESSAGE_PERMISSION_OPERATION,
|
|
330
|
+
manager: null,
|
|
331
|
+
botId,
|
|
332
|
+
createdNew: false,
|
|
333
|
+
cancelled: false,
|
|
334
|
+
remoteCommitted: false,
|
|
335
|
+
processing: null,
|
|
336
|
+
publicError: null,
|
|
337
|
+
stage: 'authorizing',
|
|
338
|
+
target: structuredClone(target),
|
|
339
|
+
targetFingerprint: configuredBotFingerprint(target),
|
|
340
|
+
initiator: { actorOpenId: null, chatId: null },
|
|
341
|
+
};
|
|
342
|
+
record.manager = new GroupMessagePermissionManager({
|
|
343
|
+
registerApp: this.#registerApp,
|
|
344
|
+
appId: target.appId,
|
|
345
|
+
domain: target.domain,
|
|
346
|
+
onCredentials: (result) => {
|
|
347
|
+
record.remoteCommitted = true;
|
|
348
|
+
const processing = this.#acceptGroupMessagePermission(record, result);
|
|
349
|
+
const tracked = processing.finally(() => {
|
|
350
|
+
if (record.processing === tracked) record.processing = null;
|
|
351
|
+
});
|
|
352
|
+
record.processing = tracked;
|
|
353
|
+
return tracked;
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
this.#registrations.set(id, record);
|
|
357
|
+
this.#activeAppUpdates.set(botId, id);
|
|
282
358
|
this.#latestRegistrationId = id;
|
|
283
359
|
this.#trimRegistrations();
|
|
284
360
|
record.manager.start();
|
|
@@ -303,7 +379,7 @@ export class MultiBotDshFeishuController {
|
|
|
303
379
|
// Once registerApp has returned, the platform-side update has committed.
|
|
304
380
|
// A repair must finish converging the returned credential and callback
|
|
305
381
|
// probe; cancelling here cannot roll that remote mutation back.
|
|
306
|
-
if (record.operation
|
|
382
|
+
if (TARGETED_APP_UPDATE_OPERATIONS.has(record.operation) && state === 'saving') {
|
|
307
383
|
return this.registrationStatus(attemptId);
|
|
308
384
|
}
|
|
309
385
|
if (!MUTABLE_REGISTRATION_STATES.has(state)) {
|
|
@@ -473,6 +549,25 @@ export class MultiBotDshFeishuController {
|
|
|
473
549
|
});
|
|
474
550
|
}
|
|
475
551
|
|
|
552
|
+
async updateGroupResponseMode(botId, groupResponseMode) {
|
|
553
|
+
this.#assertOpen();
|
|
554
|
+
if (!isFeishuGroupResponseMode(groupResponseMode)) {
|
|
555
|
+
throw new TypeError('Invalid Feishu group response mode');
|
|
556
|
+
}
|
|
557
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
558
|
+
const config = this.#requireBot(botId);
|
|
559
|
+
if (groupResponseMode === 'all' && config.groupMessagePermissionGranted !== true) {
|
|
560
|
+
const error = new Error('Authorize im:message.group_msg before enabling all group messages');
|
|
561
|
+
error.code = 'group_message_permission_required';
|
|
562
|
+
throw error;
|
|
563
|
+
}
|
|
564
|
+
const saved = await this.#configStore.saveBot({ ...config, groupResponseMode });
|
|
565
|
+
this.#runtimes.get(botId)?.setGroupResponseMode?.(saved.groupResponseMode);
|
|
566
|
+
this.#touch();
|
|
567
|
+
return this.status(botId);
|
|
568
|
+
}));
|
|
569
|
+
}
|
|
570
|
+
|
|
476
571
|
async deleteBot(botId) {
|
|
477
572
|
this.#assertOpen();
|
|
478
573
|
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
@@ -497,14 +592,14 @@ export class MultiBotDshFeishuController {
|
|
|
497
592
|
async close() {
|
|
498
593
|
if (this.#closed) return;
|
|
499
594
|
this.#closed = true;
|
|
500
|
-
const
|
|
595
|
+
const appUpdateProcessing = [];
|
|
501
596
|
for (const record of this.#registrations.values()) {
|
|
502
597
|
const state = record.manager.status().state;
|
|
503
|
-
if (record.operation
|
|
598
|
+
if (TARGETED_APP_UPDATE_OPERATIONS.has(record.operation) && state === 'saving') {
|
|
504
599
|
// Stop projecting an in-flight repair, but do not request its local
|
|
505
600
|
// credential rollback after the remote update has committed.
|
|
506
601
|
record.manager.cancel();
|
|
507
|
-
if (record.processing)
|
|
602
|
+
if (record.processing) appUpdateProcessing.push(record.processing);
|
|
508
603
|
} else if (MUTABLE_REGISTRATION_STATES.has(state)) {
|
|
509
604
|
record.cancelled = true;
|
|
510
605
|
record.manager.cancel();
|
|
@@ -513,7 +608,7 @@ export class MultiBotDshFeishuController {
|
|
|
513
608
|
await this.#configTransition;
|
|
514
609
|
await Promise.allSettled([...this.#botTransitions.values()]);
|
|
515
610
|
await Promise.allSettled([...this.#runtimes.keys()].map((id) => this.#stopRuntime(id)));
|
|
516
|
-
await Promise.allSettled(
|
|
611
|
+
await Promise.allSettled(appUpdateProcessing);
|
|
517
612
|
// A committed repair can be between SDK completion and its serialized
|
|
518
613
|
// credential/runtime transition when close begins. Waiting for the repair
|
|
519
614
|
// can therefore create a replacement runtime after the first drain. Drain
|
|
@@ -534,6 +629,8 @@ export class MultiBotDshFeishuController {
|
|
|
534
629
|
phase: botPhase({ connected, error, connection }),
|
|
535
630
|
connected,
|
|
536
631
|
configured: true,
|
|
632
|
+
groupResponseMode: normalizeFeishuGroupResponseMode(config.groupResponseMode),
|
|
633
|
+
groupMessagePermissionGranted: config.groupMessagePermissionGranted === true,
|
|
537
634
|
bot: publicBot(config),
|
|
538
635
|
connection,
|
|
539
636
|
error,
|
|
@@ -585,6 +682,158 @@ export class MultiBotDshFeishuController {
|
|
|
585
682
|
};
|
|
586
683
|
}
|
|
587
684
|
|
|
685
|
+
async #acceptGroupMessagePermission(record, result) {
|
|
686
|
+
const appId = result.client_id;
|
|
687
|
+
const appSecret = result.client_secret;
|
|
688
|
+
const ownerOpenId = optionalNonEmptyString(result.user_info?.open_id);
|
|
689
|
+
const tenantBrand = result.user_info?.tenant_brand;
|
|
690
|
+
const target = record.target;
|
|
691
|
+
|
|
692
|
+
if (record.cancelled) {
|
|
693
|
+
throw this.#callbackRepairError(
|
|
694
|
+
record,
|
|
695
|
+
'abort',
|
|
696
|
+
'Group message permission update was cancelled before local activation.',
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
if (appId !== target.appId) {
|
|
700
|
+
throw this.#callbackRepairError(
|
|
701
|
+
record,
|
|
702
|
+
'repair_app_mismatch',
|
|
703
|
+
'Feishu returned credentials for a different app.',
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
if (!ownerOpenId) {
|
|
707
|
+
throw this.#callbackRepairError(
|
|
708
|
+
record,
|
|
709
|
+
'repair_owner_missing',
|
|
710
|
+
'Feishu returned no permission operator identity.',
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
if (tenantBrand !== undefined && tenantBrand !== target.domain) {
|
|
714
|
+
throw this.#callbackRepairError(
|
|
715
|
+
record,
|
|
716
|
+
'repair_domain_mismatch',
|
|
717
|
+
'Feishu returned credentials for a different account domain.',
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
if (!target.ownerOpenIds.includes(ALL_VISIBLE_SENDERS)
|
|
721
|
+
&& !target.ownerOpenIds.includes(ownerOpenId)) {
|
|
722
|
+
throw this.#callbackRepairError(
|
|
723
|
+
record,
|
|
724
|
+
'repair_owner_mismatch',
|
|
725
|
+
'The Feishu permission operator is not an owner of this configured bot.',
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
record.stage = 'verifying_identity';
|
|
730
|
+
let verified;
|
|
731
|
+
try {
|
|
732
|
+
verified = await this.#verifyApp({ appId, appSecret, domain: target.domain });
|
|
733
|
+
} catch (error) {
|
|
734
|
+
throw this.#callbackRepairError(
|
|
735
|
+
record,
|
|
736
|
+
'repair_credentials_invalid',
|
|
737
|
+
'Feishu could not verify the updated app credentials.',
|
|
738
|
+
error,
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
if (target.botOpenId && verified?.openId !== target.botOpenId) {
|
|
742
|
+
throw this.#callbackRepairError(
|
|
743
|
+
record,
|
|
744
|
+
'repair_bot_mismatch',
|
|
745
|
+
'The updated Feishu app belongs to a different bot identity.',
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
await this.#serializeConfig(() => this.#withBotTransition(record.botId, async () => {
|
|
750
|
+
const current = this.#configStore.getBot(record.botId);
|
|
751
|
+
if (!current
|
|
752
|
+
|| current.deletionPending
|
|
753
|
+
|| configuredBotFingerprint(current) !== record.targetFingerprint) {
|
|
754
|
+
throw this.#callbackRepairError(
|
|
755
|
+
record,
|
|
756
|
+
'repair_target_changed',
|
|
757
|
+
'The Feishu bot changed while its permission update was in progress.',
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
let previous;
|
|
762
|
+
try {
|
|
763
|
+
previous = await this.#credentials.resolve(current.secretRef);
|
|
764
|
+
} catch (error) {
|
|
765
|
+
throw this.#callbackRepairError(
|
|
766
|
+
record,
|
|
767
|
+
'credential_update_failed',
|
|
768
|
+
'Unable to read the current Feishu credential.',
|
|
769
|
+
error,
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
if (previous?.value !== appSecret) {
|
|
773
|
+
record.stage = 'persisting_secret';
|
|
774
|
+
try {
|
|
775
|
+
await this.#credentials.set(current.secretRef, appSecret);
|
|
776
|
+
} catch (writeError) {
|
|
777
|
+
const observed = await this.#credentials.resolve(current.secretRef).catch(() => null);
|
|
778
|
+
if (observed?.value !== appSecret) {
|
|
779
|
+
throw this.#callbackRepairError(
|
|
780
|
+
record,
|
|
781
|
+
'credential_update_failed',
|
|
782
|
+
'Unable to store the updated Feishu credential.',
|
|
783
|
+
writeError,
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
const persisted = await this.#credentials.resolve(current.secretRef).catch(() => null);
|
|
788
|
+
if (persisted?.value !== appSecret) {
|
|
789
|
+
throw this.#callbackRepairError(
|
|
790
|
+
record,
|
|
791
|
+
'credential_state_unknown',
|
|
792
|
+
'The updated Feishu credential could not be confirmed after writing.',
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
record.stage = 'enabling_all_messages';
|
|
798
|
+
let saved;
|
|
799
|
+
try {
|
|
800
|
+
saved = await this.#configStore.saveBot({
|
|
801
|
+
...current,
|
|
802
|
+
groupMessagePermissionGranted: true,
|
|
803
|
+
groupResponseMode: 'all',
|
|
804
|
+
});
|
|
805
|
+
} catch (error) {
|
|
806
|
+
throw this.#callbackRepairError(
|
|
807
|
+
record,
|
|
808
|
+
'group_message_permission_save_failed',
|
|
809
|
+
'The permission was accepted, but all-message mode could not be saved.',
|
|
810
|
+
error,
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
record.stage = 'restarting';
|
|
815
|
+
try {
|
|
816
|
+
await this.#startRuntime(saved, appSecret);
|
|
817
|
+
} catch (error) {
|
|
818
|
+
this.#botErrors.set(record.botId, {
|
|
819
|
+
code: 'connection_failed',
|
|
820
|
+
message: '群消息权限已开通,但机器人长连接未就绪,请点击重试。',
|
|
821
|
+
});
|
|
822
|
+
this.#touch();
|
|
823
|
+
throw this.#callbackRepairError(
|
|
824
|
+
record,
|
|
825
|
+
'group_message_permission_connection_failed',
|
|
826
|
+
'The permission was accepted, but the Feishu runtime could not restart.',
|
|
827
|
+
error,
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
this.#botErrors.delete(record.botId);
|
|
831
|
+
record.stage = 'verified';
|
|
832
|
+
record.publicError = null;
|
|
833
|
+
this.#touch();
|
|
834
|
+
}));
|
|
835
|
+
}
|
|
836
|
+
|
|
588
837
|
async #acceptCallbackRepair(record, result) {
|
|
589
838
|
const appId = result.client_id;
|
|
590
839
|
const appSecret = result.client_secret;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { dirname } from 'node:path';
|
|
4
|
+
import { normalizeFeishuGroupResponseMode } from './group-response-mode.mjs';
|
|
4
5
|
|
|
5
6
|
export const LEGACY_FEISHU_SECRET_REF = 'DSH_FEISHU_APP_SECRET';
|
|
6
7
|
|
|
@@ -43,6 +44,8 @@ function normalizeBot(value, { legacy = false } = {}) {
|
|
|
43
44
|
botName: cleanString(value.botName),
|
|
44
45
|
botOpenId: cleanString(value.botOpenId),
|
|
45
46
|
activated: value.activated ?? null,
|
|
47
|
+
groupResponseMode: normalizeFeishuGroupResponseMode(value.groupResponseMode),
|
|
48
|
+
groupMessagePermissionGranted: value.groupMessagePermissionGranted === true,
|
|
46
49
|
deletionPending: value.deletionPending === true,
|
|
47
50
|
connectedAt: cleanString(value.connectedAt),
|
|
48
51
|
createdAt: cleanString(value.createdAt) ?? cleanString(value.connectedAt),
|
|
@@ -17,12 +17,17 @@ function launcherDomain(domain) {
|
|
|
17
17
|
* never fall back to the create-only flow. This catches regressions such as a
|
|
18
18
|
* literal `{{client_id}}` before the broken URL reaches the browser.
|
|
19
19
|
*/
|
|
20
|
-
export function
|
|
20
|
+
export function assertTargetedAppUpdateUrl(
|
|
21
|
+
value,
|
|
22
|
+
expectedAppId,
|
|
23
|
+
domain = 'feishu',
|
|
24
|
+
operationLabel = 'Feishu app update',
|
|
25
|
+
) {
|
|
21
26
|
let url;
|
|
22
27
|
try {
|
|
23
28
|
url = new URL(value);
|
|
24
29
|
} catch {
|
|
25
|
-
throw new Error(
|
|
30
|
+
throw new Error(`${operationLabel} returned an invalid verification URL`);
|
|
26
31
|
}
|
|
27
32
|
const supportedDomain = domain === 'feishu' || domain === 'lark';
|
|
28
33
|
const clientIds = url.searchParams.getAll('clientID');
|
|
@@ -49,11 +54,20 @@ export function assertCallbackRepairUrl(value, expectedAppId, domain = 'feishu')
|
|
|
49
54
|
|| addons.length !== 1
|
|
50
55
|
|| !addons[0]?.trim()
|
|
51
56
|
|| hasPlaceholder) {
|
|
52
|
-
throw new Error(
|
|
57
|
+
throw new Error(`${operationLabel} returned an unsafe verification URL`);
|
|
53
58
|
}
|
|
54
59
|
return url.toString();
|
|
55
60
|
}
|
|
56
61
|
|
|
62
|
+
export function assertCallbackRepairUrl(value, expectedAppId, domain = 'feishu') {
|
|
63
|
+
return assertTargetedAppUpdateUrl(
|
|
64
|
+
value,
|
|
65
|
+
expectedAppId,
|
|
66
|
+
domain,
|
|
67
|
+
'Feishu callback repair',
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
57
71
|
/**
|
|
58
72
|
* One targeted update attempt for an existing Feishu app. It intentionally
|
|
59
73
|
* shares RegistrationManager's polling/state implementation while fixing the
|
|
@@ -16,6 +16,18 @@ export function validTelegramToken(value) {
|
|
|
16
16
|
return typeof value === 'string' && /^\d{5,20}:[A-Za-z0-9_-]{20,}$/.test(value.trim());
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
const TELEGRAM_COMMAND_NAME = /^[a-z0-9_]{1,32}$/;
|
|
20
|
+
|
|
21
|
+
function validBotCommand(value) {
|
|
22
|
+
return Boolean(value)
|
|
23
|
+
&& typeof value === 'object' && !Array.isArray(value)
|
|
24
|
+
&& typeof value.command === 'string' && TELEGRAM_COMMAND_NAME.test(value.command)
|
|
25
|
+
&& typeof value.description === 'string' && value.description.length >= 1
|
|
26
|
+
&& value.description.length <= 256;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const COMMANDS_MENU_BUTTON = Object.freeze({ type: 'commands' });
|
|
30
|
+
|
|
19
31
|
export class TelegramApi {
|
|
20
32
|
#token;
|
|
21
33
|
#fetch;
|
|
@@ -113,6 +125,25 @@ export class TelegramApi {
|
|
|
113
125
|
}, { signal });
|
|
114
126
|
}
|
|
115
127
|
|
|
128
|
+
async setMyCommands({ commands, scope, languageCode, signal } = {}) {
|
|
129
|
+
if (!Array.isArray(commands) || commands.length === 0
|
|
130
|
+
|| commands.some((command) => !validBotCommand(command))) {
|
|
131
|
+
throw new TypeError('Telegram bot commands are invalid');
|
|
132
|
+
}
|
|
133
|
+
return this.#call('setMyCommands', {
|
|
134
|
+
commands,
|
|
135
|
+
...(scope ? { scope } : {}),
|
|
136
|
+
...(cleanString(languageCode) ? { language_code: cleanString(languageCode) } : {}),
|
|
137
|
+
}, { signal });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async setChatMenuButton({ menuButton = COMMANDS_MENU_BUTTON, signal } = {}) {
|
|
141
|
+
if (!menuButton || typeof menuButton !== 'object' || Array.isArray(menuButton)) {
|
|
142
|
+
throw new TypeError('Telegram menu button is invalid');
|
|
143
|
+
}
|
|
144
|
+
return this.#call('setChatMenuButton', { menu_button: menuButton }, { signal });
|
|
145
|
+
}
|
|
146
|
+
|
|
116
147
|
async #call(method, payload, { signal, timeoutMs = 15_000 } = {}) {
|
|
117
148
|
const url = new URL(this.#baseUrl);
|
|
118
149
|
url.pathname = `${url.pathname.replace(/\/$/, '')}/bot${this.#token}/${method}`;
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import { createEditableMessageStream, splitMessageText } from '../shared/editable-message-stream.mjs';
|
|
2
|
-
import { TelegramApi } from './telegram-api.mjs';
|
|
2
|
+
import { COMMANDS_MENU_BUTTON, TelegramApi } from './telegram-api.mjs';
|
|
3
3
|
import { createTelegramBridgeStatus, TelegramHarnessBridge } from './telegram-bridge.mjs';
|
|
4
4
|
import {
|
|
5
5
|
TELEGRAM_ACCESS_MODES,
|
|
6
6
|
normalizeTelegramAccessPolicy,
|
|
7
7
|
} from './config-store.mjs';
|
|
8
8
|
|
|
9
|
+
export const TELEGRAM_COMMAND_MENU = Object.freeze([
|
|
10
|
+
{ command: 'new', description: '开启一个全新会话' },
|
|
11
|
+
{ command: 'compact', description: '压缩当前会话的较早上下文' },
|
|
12
|
+
{ command: 'workspace', description: '切换工作区' },
|
|
13
|
+
{ command: 'workspacelist', description: '列出工作区绝对路径' },
|
|
14
|
+
{ command: 'sessionlist', description: '列出会话 ID 和标题' },
|
|
15
|
+
{ command: 'session', description: '将当前聊天绑定到指定会话' },
|
|
16
|
+
{ command: 'models', description: '按序号列出所有可用模型' },
|
|
17
|
+
{ command: 'model', description: '查看或切换当前会话模型' },
|
|
18
|
+
{ command: 'stop', description: '停止当前任务' },
|
|
19
|
+
{ command: 'steer', description: '纠偏当前任务' },
|
|
20
|
+
{ command: 'status', description: '检查连接状态' },
|
|
21
|
+
{ command: 'help', description: '显示帮助' },
|
|
22
|
+
]);
|
|
23
|
+
|
|
9
24
|
function escaped(value) {
|
|
10
25
|
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
11
26
|
}
|
|
@@ -290,6 +305,15 @@ export class TelegramRuntime {
|
|
|
290
305
|
error.code = 'webhook-configured';
|
|
291
306
|
throw error;
|
|
292
307
|
}
|
|
308
|
+
try {
|
|
309
|
+
await api.setMyCommands({ commands: TELEGRAM_COMMAND_MENU, signal: controller.signal });
|
|
310
|
+
await api.setChatMenuButton({ menuButton: COMMANDS_MENU_BUTTON, signal: controller.signal });
|
|
311
|
+
} catch (error) {
|
|
312
|
+
this.#logger.warn?.(
|
|
313
|
+
`[dsh-im:telegram] bot ${this.#config.botId} command menu setup failed:`,
|
|
314
|
+
error,
|
|
315
|
+
);
|
|
316
|
+
}
|
|
293
317
|
const client = new TelegramBotClient({ api, signal: controller.signal });
|
|
294
318
|
this.#bridge = new TelegramHarnessBridge({
|
|
295
319
|
bot: client,
|