dsh-message-gateway 0.1.19 → 0.1.20
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/index.js +241 -5
- package/lib/index.js.map +3 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -3201,22 +3201,199 @@ function sanitizeSecrets(text) {
|
|
|
3201
3201
|
|
|
3202
3202
|
// src/host/wecom-bridge.ts
|
|
3203
3203
|
import { WSClient } from "@wecom/aibot-node-sdk";
|
|
3204
|
+
|
|
3205
|
+
// src/host/wecom-smartsheet.ts
|
|
3206
|
+
var WecomSmartsheetClient = class {
|
|
3207
|
+
constructor(creds) {
|
|
3208
|
+
this.creds = creds;
|
|
3209
|
+
}
|
|
3210
|
+
token = null;
|
|
3211
|
+
tokenExpiresAt = 0;
|
|
3212
|
+
/**
|
|
3213
|
+
* 获取有效的 access_token(自动内存缓存与续期)。
|
|
3214
|
+
*/
|
|
3215
|
+
async getAccessToken() {
|
|
3216
|
+
const now = Date.now();
|
|
3217
|
+
if (this.token && now < this.tokenExpiresAt - 6e4) {
|
|
3218
|
+
return this.token;
|
|
3219
|
+
}
|
|
3220
|
+
const corpId = this.creds.corpId || this.creds.botId || "";
|
|
3221
|
+
const secret = this.creds.secret || "";
|
|
3222
|
+
if (!corpId || !secret) {
|
|
3223
|
+
throw new Error("\u4F01\u4E1A\u5FAE\u4FE1\u51ED\u636E\u4E0D\u5B8C\u6574\uFF08\u7F3A\u5C11 botId/corpId \u6216 secret\uFF09");
|
|
3224
|
+
}
|
|
3225
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${encodeURIComponent(corpId)}&corpsecret=${encodeURIComponent(secret)}`;
|
|
3226
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(1e4) });
|
|
3227
|
+
const data = await res.json();
|
|
3228
|
+
if (data.errcode !== 0 || !data.access_token) {
|
|
3229
|
+
throw new Error(`\u83B7\u53D6\u4F01\u4E1A\u5FAE\u4FE1 access_token \u5931\u8D25 [${data.errcode}]: ${data.errmsg || "\u672A\u77E5\u9519\u8BEF"}`);
|
|
3230
|
+
}
|
|
3231
|
+
this.token = data.access_token;
|
|
3232
|
+
this.tokenExpiresAt = now + (data.expires_in ?? 7200) * 1e3;
|
|
3233
|
+
return this.token;
|
|
3234
|
+
}
|
|
3235
|
+
/**
|
|
3236
|
+
* 映射字段类型为企微官方标准枚举。
|
|
3237
|
+
*/
|
|
3238
|
+
mapFieldType(type) {
|
|
3239
|
+
switch (type?.toLowerCase()) {
|
|
3240
|
+
case "number":
|
|
3241
|
+
return "FIELD_TYPE_NUMBER";
|
|
3242
|
+
case "single_select":
|
|
3243
|
+
case "select":
|
|
3244
|
+
return "FIELD_TYPE_SINGLE_SELECT";
|
|
3245
|
+
case "multi_select":
|
|
3246
|
+
return "FIELD_TYPE_MULTI_SELECT";
|
|
3247
|
+
case "date":
|
|
3248
|
+
case "date_time":
|
|
3249
|
+
case "datetime":
|
|
3250
|
+
return "FIELD_TYPE_DATE_TIME";
|
|
3251
|
+
case "user":
|
|
3252
|
+
case "member":
|
|
3253
|
+
return "FIELD_TYPE_USER";
|
|
3254
|
+
case "checkbox":
|
|
3255
|
+
return "FIELD_TYPE_CHECKBOX";
|
|
3256
|
+
case "text":
|
|
3257
|
+
default:
|
|
3258
|
+
return "FIELD_TYPE_TEXT";
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3261
|
+
/**
|
|
3262
|
+
* 创建一份全新的在线智能表格(Smartsheet)。
|
|
3263
|
+
*/
|
|
3264
|
+
async createSmartsheet(options) {
|
|
3265
|
+
try {
|
|
3266
|
+
const token = await this.getAccessToken();
|
|
3267
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/wedoc/smartsheet/create?access_token=${encodeURIComponent(token)}`;
|
|
3268
|
+
const fields = options.fields && options.fields.length > 0 ? options.fields.map((f) => {
|
|
3269
|
+
const fieldType = this.mapFieldType(f.type);
|
|
3270
|
+
const item = {
|
|
3271
|
+
field_title: f.title,
|
|
3272
|
+
field_type: fieldType
|
|
3273
|
+
};
|
|
3274
|
+
if ((fieldType === "FIELD_TYPE_SINGLE_SELECT" || fieldType === "FIELD_TYPE_MULTI_SELECT") && f.options) {
|
|
3275
|
+
item.property = {
|
|
3276
|
+
options: f.options.map((opt) => ({ text: String(opt) }))
|
|
3277
|
+
};
|
|
3278
|
+
}
|
|
3279
|
+
return item;
|
|
3280
|
+
}) : [
|
|
3281
|
+
{ field_title: "\u6807\u9898", field_type: "FIELD_TYPE_TEXT" },
|
|
3282
|
+
{ field_title: "\u72B6\u6001", field_type: "FIELD_TYPE_SINGLE_SELECT", property: { options: [{ text: "\u672A\u5F00\u59CB" }, { text: "\u8FDB\u884C\u4E2D" }, { text: "\u5DF2\u5B8C\u6210" }] } },
|
|
3283
|
+
{ field_title: "\u8D1F\u8D23\u4EBA", field_type: "FIELD_TYPE_USER" },
|
|
3284
|
+
{ field_title: "\u622A\u6B62\u65E5\u671F", field_type: "FIELD_TYPE_DATE_TIME" }
|
|
3285
|
+
];
|
|
3286
|
+
const payload = {
|
|
3287
|
+
doc_name: options.title || "\u65B0\u5EFA\u667A\u80FD\u8868\u683C",
|
|
3288
|
+
sheet_title: options.sheetTitle || "\u6570\u636E\u8868",
|
|
3289
|
+
fields
|
|
3290
|
+
};
|
|
3291
|
+
if (options.adminUsers && options.adminUsers.length > 0) {
|
|
3292
|
+
payload.admin_users = options.adminUsers;
|
|
3293
|
+
}
|
|
3294
|
+
const res = await fetch(url, {
|
|
3295
|
+
method: "POST",
|
|
3296
|
+
headers: { "content-type": "application/json" },
|
|
3297
|
+
body: JSON.stringify(payload),
|
|
3298
|
+
signal: AbortSignal.timeout(15e3)
|
|
3299
|
+
});
|
|
3300
|
+
const data = await res.json();
|
|
3301
|
+
if (data.errcode !== 0 || !data.docid) {
|
|
3302
|
+
return {
|
|
3303
|
+
ok: false,
|
|
3304
|
+
error: `\u521B\u5EFA\u667A\u80FD\u8868\u683C\u5931\u8D25 [${data.errcode}]: ${data.errmsg || "\u672A\u77E5\u9519\u8BEF"}`
|
|
3305
|
+
};
|
|
3306
|
+
}
|
|
3307
|
+
if (options.records && options.records.length > 0) {
|
|
3308
|
+
await this.addRecords(data.docid, options.records).catch((err) => {
|
|
3309
|
+
console.warn("[WecomSmartsheetClient] \u5199\u5165\u521D\u59CB\u6570\u636E\u5931\u8D25\uFF08\u8868\u683C\u5DF2\u521B\u5EFA\u6210\u529F\uFF09:", err);
|
|
3310
|
+
});
|
|
3311
|
+
}
|
|
3312
|
+
return {
|
|
3313
|
+
ok: true,
|
|
3314
|
+
docid: data.docid,
|
|
3315
|
+
url: data.url || `https://doc.weixin.qq.com/smartsheet/${data.docid}`
|
|
3316
|
+
};
|
|
3317
|
+
} catch (error) {
|
|
3318
|
+
return {
|
|
3319
|
+
ok: false,
|
|
3320
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3321
|
+
};
|
|
3322
|
+
}
|
|
3323
|
+
}
|
|
3324
|
+
/**
|
|
3325
|
+
* 向指定的智能表格中追加行记录。
|
|
3326
|
+
*/
|
|
3327
|
+
async addRecords(docid, records, sheetId) {
|
|
3328
|
+
try {
|
|
3329
|
+
const token = await this.getAccessToken();
|
|
3330
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/wedoc/smartsheet/add_records?access_token=${encodeURIComponent(token)}`;
|
|
3331
|
+
const recordItems = records.map((rec) => ({
|
|
3332
|
+
values: rec
|
|
3333
|
+
}));
|
|
3334
|
+
const payload = {
|
|
3335
|
+
docid,
|
|
3336
|
+
records: recordItems
|
|
3337
|
+
};
|
|
3338
|
+
if (sheetId) {
|
|
3339
|
+
payload.sheet_id = sheetId;
|
|
3340
|
+
}
|
|
3341
|
+
const res = await fetch(url, {
|
|
3342
|
+
method: "POST",
|
|
3343
|
+
headers: { "content-type": "application/json" },
|
|
3344
|
+
body: JSON.stringify(payload),
|
|
3345
|
+
signal: AbortSignal.timeout(15e3)
|
|
3346
|
+
});
|
|
3347
|
+
const data = await res.json();
|
|
3348
|
+
if (data.errcode !== 0) {
|
|
3349
|
+
return { ok: false, error: `\u6DFB\u52A0\u8BB0\u5F55\u5931\u8D25 [${data.errcode}]: ${data.errmsg || "\u672A\u77E5\u9519\u8BEF"}` };
|
|
3350
|
+
}
|
|
3351
|
+
return { ok: true };
|
|
3352
|
+
} catch (error) {
|
|
3353
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
3354
|
+
}
|
|
3355
|
+
}
|
|
3356
|
+
};
|
|
3357
|
+
|
|
3358
|
+
// src/host/wecom-bridge.ts
|
|
3204
3359
|
var WecomBridge = class {
|
|
3205
3360
|
constructor(cred, callbacks) {
|
|
3206
3361
|
this.cred = cred;
|
|
3207
3362
|
this.callbacks = callbacks;
|
|
3363
|
+
this.smartsheetClient = new WecomSmartsheetClient({
|
|
3364
|
+
botId: cred.botId,
|
|
3365
|
+
secret: cred.secret
|
|
3366
|
+
});
|
|
3208
3367
|
}
|
|
3209
3368
|
client = null;
|
|
3369
|
+
smartsheetClient = null;
|
|
3210
3370
|
started = false;
|
|
3211
3371
|
status = { state: "idle", detail: "", connectedAt: null };
|
|
3212
3372
|
setStatus(state, detail = "") {
|
|
3213
3373
|
this.status = {
|
|
3214
3374
|
state,
|
|
3215
3375
|
detail,
|
|
3216
|
-
connectedAt: state === "connected" ? Date.now() :
|
|
3376
|
+
connectedAt: state === "connected" ? this.status.connectedAt ?? Date.now() : null
|
|
3217
3377
|
};
|
|
3218
3378
|
this.callbacks.onStatus(this.status);
|
|
3219
3379
|
}
|
|
3380
|
+
/** 获取智能表格操作客户端 */
|
|
3381
|
+
getSmartsheetClient() {
|
|
3382
|
+
return this.smartsheetClient;
|
|
3383
|
+
}
|
|
3384
|
+
/** 获取底层已认证连接的 WSClient 实例 */
|
|
3385
|
+
getClient() {
|
|
3386
|
+
return this.client;
|
|
3387
|
+
}
|
|
3388
|
+
/**
|
|
3389
|
+
* 创建企业微信智能表格。
|
|
3390
|
+
*/
|
|
3391
|
+
async createSmartsheet(options) {
|
|
3392
|
+
if (!this.smartsheetClient) {
|
|
3393
|
+
return { ok: false, error: "\u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u8868\u683C\u5BA2\u6237\u7AEF\u672A\u521D\u59CB\u5316" };
|
|
3394
|
+
}
|
|
3395
|
+
return await this.smartsheetClient.createSmartsheet(options);
|
|
3396
|
+
}
|
|
3220
3397
|
/** 建立常驻连接(断线由 SDK 自动指数退避重连)。 */
|
|
3221
3398
|
start() {
|
|
3222
3399
|
if (this.started) return;
|
|
@@ -3224,7 +3401,9 @@ var WecomBridge = class {
|
|
|
3224
3401
|
this.setStatus("connecting");
|
|
3225
3402
|
const client = new WSClient({ botId: this.cred.botId, secret: this.cred.secret });
|
|
3226
3403
|
this.client = client;
|
|
3227
|
-
client.on("authenticated", () =>
|
|
3404
|
+
client.on("authenticated", () => {
|
|
3405
|
+
this.setStatus("connected", `botId ${this.cred.botId}`);
|
|
3406
|
+
});
|
|
3228
3407
|
client.on("message.text", (frame) => {
|
|
3229
3408
|
const content = frame.body?.text?.content ?? "";
|
|
3230
3409
|
if (content.trim() !== "") this.callbacks.onText(content, frame);
|
|
@@ -6759,9 +6938,9 @@ function apply(ctx, config = Config({})) {
|
|
|
6759
6938
|
}
|
|
6760
6939
|
});
|
|
6761
6940
|
const disposeRoutes = registerGatewayRoutes(ctx, manager);
|
|
6762
|
-
let
|
|
6941
|
+
let disposeTools;
|
|
6763
6942
|
if (ctx.tools) {
|
|
6764
|
-
|
|
6943
|
+
const disposeSend = ctx.tools.register(defineTool({
|
|
6765
6944
|
name: "send_chat_message",
|
|
6766
6945
|
description: "\u5411\u5DF2\u8FDE\u63A5\u7684\u6D88\u606F\u5E73\u53F0\uFF08Telegram / Discord / \u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u673A\u5668\u4EBA / Email \u7B49\uFF09\u4E3B\u52A8\u63A8\u9001\u6587\u672C\u6D88\u606F\u3002\u4F8B\u5982\u628A\u4EE3\u7801\u603B\u7ED3\u3001\u4EFB\u52A1\u7ED3\u679C\u63A8\u9001\u81F3\u6307\u5B9A\u7684\u7FA4\u804A\u6216\u79C1\u4FE1\u9891\u9053\u3002",
|
|
6767
6946
|
parameters: {
|
|
@@ -6825,9 +7004,66 @@ function apply(ctx, config = Config({})) {
|
|
|
6825
7004
|
return `\u5DF2\u6210\u529F\u63A8\u9001\u81F3 [${platform}] \u76EE\u6807 ${target}`;
|
|
6826
7005
|
}
|
|
6827
7006
|
}));
|
|
7007
|
+
const disposeSmartsheet = ctx.tools.register(defineTool({
|
|
7008
|
+
name: "wecom_create_smartsheet",
|
|
7009
|
+
description: "\u5728\u4F01\u4E1A\u5FAE\u4FE1\u4E2D\u521B\u5EFA\u4E00\u4EFD\u5168\u65B0\u7684\u5728\u7EBF\u667A\u80FD\u8868\u683C\uFF08Smartsheet\uFF09\uFF0C\u652F\u6301\u5B9A\u4E49\u8868\u683C\u6807\u9898\u3001\u5B57\u6BB5\u5217\uFF08\u6587\u672C/\u5355\u9009/\u591A\u9009/\u4EBA\u5458/\u65E5\u671F/\u6570\u5B57\u7B49\uFF09\u4E0E\u521D\u59CB\u6570\u636E\u884C\uFF0C\u5E76\u8FD4\u56DE\u667A\u80FD\u8868\u683C\u5728\u7EBF\u8BBF\u95EE\u94FE\u63A5\u3002",
|
|
7010
|
+
parameters: {
|
|
7011
|
+
title: {
|
|
7012
|
+
type: "string",
|
|
7013
|
+
required: true,
|
|
7014
|
+
description: "\u667A\u80FD\u8868\u683C\u6587\u6863\u6807\u9898\uFF08\u5982\uFF1A2026\u5E74Q3\u9879\u76EE\u5F00\u53D1\u6392\u671F\u8868\uFF09"
|
|
7015
|
+
},
|
|
7016
|
+
sheetTitle: {
|
|
7017
|
+
type: "string",
|
|
7018
|
+
description: "\u9996\u4E2A\u5DE5\u4F5C\u8868\u540D\u79F0\uFF08\u9ED8\u8BA4\uFF1A\u6570\u636E\u8868\uFF09"
|
|
7019
|
+
},
|
|
7020
|
+
fields: {
|
|
7021
|
+
type: "array",
|
|
7022
|
+
description: '\u5217\u5B57\u6BB5\u5B9A\u4E49\u5217\u8868\uFF0C\u5982 [{"title": "\u4EFB\u52A1\u540D\u79F0", "type": "text"}, {"title": "\u72B6\u6001", "type": "single_select", "options": ["\u5F85\u529E", "\u8FDB\u884C\u4E2D", "\u5DF2\u5B8C\u6210"]}]'
|
|
7023
|
+
},
|
|
7024
|
+
records: {
|
|
7025
|
+
type: "array",
|
|
7026
|
+
description: '\u521D\u59CB\u586B\u5165\u7684\u6570\u636E\u884C\u5217\u8868\uFF0C\u5982 [{"\u4EFB\u52A1\u540D\u79F0": "\u9700\u6C42\u8BC4\u5BA1", "\u72B6\u6001": "\u5DF2\u5B8C\u6210"}]'
|
|
7027
|
+
}
|
|
7028
|
+
},
|
|
7029
|
+
output: {
|
|
7030
|
+
schema: { type: "string" },
|
|
7031
|
+
render: (_args, value) => [{ type: "text", text: String(value) }]
|
|
7032
|
+
},
|
|
7033
|
+
async execute(args) {
|
|
7034
|
+
const title = String(args.title ?? "").trim();
|
|
7035
|
+
if (!title) return "\u9519\u8BEF\uFF1A\u8868\u683C\u6807\u9898 title \u4E3A\u5FC5\u586B\u9879";
|
|
7036
|
+
const store = await loadStore();
|
|
7037
|
+
const wecomCred = store.platforms["wecom-aibot"] || store.platforms["wecom"];
|
|
7038
|
+
if (!wecomCred || !wecomCred.secret && !wecomCred.corpId && !wecomCred.botId) {
|
|
7039
|
+
return "\u9519\u8BEF\uFF1A\u672A\u5728\u6D88\u606F\u5E73\u53F0\u4E2D\u914D\u7F6E\u4F01\u4E1A\u5FAE\u4FE1\u51ED\u636E\uFF0C\u8BF7\u5148\u5728\u4FA7\u8FB9\u680F\u300C\u6D88\u606F\u5E73\u53F0\u300D\u4E2D\u914D\u7F6E\u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u673A\u5668\u4EBA\u6216\u81EA\u5EFA\u5E94\u7528\u51ED\u636E";
|
|
7040
|
+
}
|
|
7041
|
+
const client = new WecomSmartsheetClient({
|
|
7042
|
+
botId: wecomCred.botId,
|
|
7043
|
+
secret: wecomCred.secret,
|
|
7044
|
+
corpId: wecomCred.corpId
|
|
7045
|
+
});
|
|
7046
|
+
const res = await client.createSmartsheet({
|
|
7047
|
+
title,
|
|
7048
|
+
sheetTitle: args.sheetTitle,
|
|
7049
|
+
fields: args.fields,
|
|
7050
|
+
records: args.records
|
|
7051
|
+
});
|
|
7052
|
+
if (!res.ok) {
|
|
7053
|
+
return `\u521B\u5EFA\u667A\u80FD\u8868\u683C\u5931\u8D25\uFF1A${res.error}`;
|
|
7054
|
+
}
|
|
7055
|
+
return `\u2705 \u667A\u80FD\u8868\u683C\u300C${title}\u300D\u521B\u5EFA\u6210\u529F\uFF01
|
|
7056
|
+
- \u6587\u6863 ID: ${res.docid}
|
|
7057
|
+
- \u5728\u7EBF\u8BBF\u95EE\u94FE\u63A5: ${res.url}`;
|
|
7058
|
+
}
|
|
7059
|
+
}));
|
|
7060
|
+
disposeTools = () => {
|
|
7061
|
+
disposeSend();
|
|
7062
|
+
disposeSmartsheet();
|
|
7063
|
+
};
|
|
6828
7064
|
}
|
|
6829
7065
|
return () => {
|
|
6830
|
-
|
|
7066
|
+
disposeTools?.();
|
|
6831
7067
|
disposeRoutes();
|
|
6832
7068
|
void manager.dispose();
|
|
6833
7069
|
};
|