@celljs/ta-bots 4.15.0 → 4.15.1
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/host/index.mjs +33 -4
- package/package.json +3 -3
package/host/index.mjs
CHANGED
|
@@ -61,8 +61,16 @@ class BotStore {
|
|
|
61
61
|
async secret(id) {
|
|
62
62
|
return (await this.allSecrets())[id];
|
|
63
63
|
}
|
|
64
|
-
async tokens() {
|
|
65
|
-
|
|
64
|
+
async tokens(bots) {
|
|
65
|
+
const secrets = await this.allSecrets();
|
|
66
|
+
return bots.flatMap((bot) => {
|
|
67
|
+
const secret = secrets[bot.id];
|
|
68
|
+
return secret?.kind === "weixin" ? [secret.token] : [];
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async orphans(bots) {
|
|
72
|
+
const known = new Set(bots.map((bot) => bot.id));
|
|
73
|
+
return Object.entries(await this.allSecrets()).filter(([id]) => !known.has(id));
|
|
66
74
|
}
|
|
67
75
|
async setSecret(id, secret) {
|
|
68
76
|
const secrets = { ...await this.allSecrets() };
|
|
@@ -2268,6 +2276,7 @@ class WeixinChannel {
|
|
|
2268
2276
|
texts.push(item.text_item.text);
|
|
2269
2277
|
} else if (item.type === WeixinItemType.VOICE && item.voice_item?.text) {
|
|
2270
2278
|
texts.push(item.voice_item.text);
|
|
2279
|
+
continue;
|
|
2271
2280
|
}
|
|
2272
2281
|
const file = await this.media(item).catch((error) => {
|
|
2273
2282
|
texts.push(`[${WeixinChannel.label(item.type)}: ${errorText(error)}]`);
|
|
@@ -2454,6 +2463,7 @@ class BotService {
|
|
|
2454
2463
|
}
|
|
2455
2464
|
async start() {
|
|
2456
2465
|
this.bots = await this.store.bots();
|
|
2466
|
+
await this.recover();
|
|
2457
2467
|
this.status = {};
|
|
2458
2468
|
for (const bot of this.bots) {
|
|
2459
2469
|
await this.connect(bot);
|
|
@@ -2479,7 +2489,7 @@ class BotService {
|
|
|
2479
2489
|
}
|
|
2480
2490
|
async startWeixinLogin(target) {
|
|
2481
2491
|
const where = BotTargetUtil.parse(target);
|
|
2482
|
-
return this.logins.start(await this.store.tokens(), (signIn) => this.signedIn(signIn, where));
|
|
2492
|
+
return this.logins.start(await this.store.tokens(this.bots), (signIn) => this.signedIn(signIn, where));
|
|
2483
2493
|
}
|
|
2484
2494
|
async startWecomLogin(target) {
|
|
2485
2495
|
const where = BotTargetUtil.parse(target);
|
|
@@ -2509,7 +2519,7 @@ class BotService {
|
|
|
2509
2519
|
id,
|
|
2510
2520
|
kind: "wecom",
|
|
2511
2521
|
name: typeof name === "string" && name.trim() || before?.name || BotServerTexts.defaultName("wecom", this.names()),
|
|
2512
|
-
account:
|
|
2522
|
+
account: BotService.wecomAccount(botId),
|
|
2513
2523
|
target: target ?? before?.target,
|
|
2514
2524
|
createdAt: before?.createdAt ?? Date.now()
|
|
2515
2525
|
};
|
|
@@ -2608,6 +2618,22 @@ class BotService {
|
|
|
2608
2618
|
return chat ? [{ botId: bot.id, chat }] : [];
|
|
2609
2619
|
});
|
|
2610
2620
|
}
|
|
2621
|
+
async recover() {
|
|
2622
|
+
const lost = await this.store.orphans(this.bots);
|
|
2623
|
+
if (!lost.length) {
|
|
2624
|
+
return;
|
|
2625
|
+
}
|
|
2626
|
+
for (const [id, secret] of lost) {
|
|
2627
|
+
this.bots.push({
|
|
2628
|
+
id,
|
|
2629
|
+
kind: secret.kind,
|
|
2630
|
+
name: BotServerTexts.defaultName(secret.kind, this.names()),
|
|
2631
|
+
account: secret.kind === "weixin" ? BotService.short(secret.userId ?? id.slice("weixin:".length)) : BotService.wecomAccount(secret.botId),
|
|
2632
|
+
createdAt: Date.now()
|
|
2633
|
+
});
|
|
2634
|
+
}
|
|
2635
|
+
await this.store.saveBots(this.bots);
|
|
2636
|
+
}
|
|
2611
2637
|
async signedIn(signIn, target) {
|
|
2612
2638
|
const id = `weixin:${signIn.botId}`;
|
|
2613
2639
|
const secrets = await Promise.all(this.bots.map(async (bot2) => [bot2, await this.store.secret(bot2.id)]));
|
|
@@ -2781,6 +2807,9 @@ ${origin.reply ?? ""}`;
|
|
|
2781
2807
|
return !seen.has(key) && seen.add(key) !== undefined;
|
|
2782
2808
|
});
|
|
2783
2809
|
}
|
|
2810
|
+
static wecomAccount(botId) {
|
|
2811
|
+
return botId.length > 10 ? `${botId.slice(0, 6)}…${botId.slice(-4)}` : botId;
|
|
2812
|
+
}
|
|
2784
2813
|
static short(account) {
|
|
2785
2814
|
const name = account.split("@")[0];
|
|
2786
2815
|
return name.length > 10 ? `${name.slice(0, 4)}…${name.slice(-4)}` : name;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-bots",
|
|
3
|
-
"version": "4.15.
|
|
3
|
+
"version": "4.15.1",
|
|
4
4
|
"description": "A pi package for Ta: bots of WeChat (ClawBot) and WeCom connected to the double, an agent or a conversation, messages and files both ways",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@celljs/ta-protocol": "4.15.
|
|
31
|
-
"@celljs/ta-web": "4.15.
|
|
30
|
+
"@celljs/ta-protocol": "4.15.1",
|
|
31
|
+
"@celljs/ta-web": "4.15.1",
|
|
32
32
|
"@types/react": "^19.3.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|