dsh-message-gateway 0.1.30 → 0.1.31
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 +35 -6
- package/lib/client.js.map +2 -2
- package/lib/index.js +173 -25
- package/lib/index.js.map +4 -4
- package/package.json +3 -2
package/lib/index.js
CHANGED
|
@@ -2019,6 +2019,110 @@ var FeishuBridge = class {
|
|
|
2019
2019
|
}
|
|
2020
2020
|
};
|
|
2021
2021
|
|
|
2022
|
+
// src/host/dingtalk-bridge.ts
|
|
2023
|
+
import { DWClient, TOPIC_ROBOT } from "dingtalk-stream";
|
|
2024
|
+
var DingTalkBridge = class {
|
|
2025
|
+
constructor(cred, callbacks) {
|
|
2026
|
+
this.cred = cred;
|
|
2027
|
+
this.callbacks = callbacks;
|
|
2028
|
+
}
|
|
2029
|
+
client = null;
|
|
2030
|
+
started = false;
|
|
2031
|
+
status = { state: "idle", detail: "", connectedAt: null };
|
|
2032
|
+
setStatus(state, detail = "") {
|
|
2033
|
+
this.status = {
|
|
2034
|
+
state,
|
|
2035
|
+
detail,
|
|
2036
|
+
connectedAt: state === "connected" ? this.status.connectedAt ?? Date.now() : null
|
|
2037
|
+
};
|
|
2038
|
+
this.callbacks.onStatus(this.status);
|
|
2039
|
+
}
|
|
2040
|
+
/** 建立长连接。 */
|
|
2041
|
+
start() {
|
|
2042
|
+
if (this.started) return;
|
|
2043
|
+
this.started = true;
|
|
2044
|
+
this.setStatus("connecting", "\u6B63\u5728\u8FDE\u63A5\u9489\u9489 Stream \u957F\u8FDE\u63A5\u2026");
|
|
2045
|
+
try {
|
|
2046
|
+
this.client = new DWClient({
|
|
2047
|
+
clientId: this.cred.clientId,
|
|
2048
|
+
clientSecret: this.cred.clientSecret,
|
|
2049
|
+
debug: false
|
|
2050
|
+
});
|
|
2051
|
+
this.client.registerCallbackListener(TOPIC_ROBOT, async (downstream) => {
|
|
2052
|
+
try {
|
|
2053
|
+
const raw = JSON.parse(downstream.data || "{}");
|
|
2054
|
+
this.client?.socketCallBackResponse(downstream.headers.messageId, {
|
|
2055
|
+
response: {
|
|
2056
|
+
statusLine: { code: 200, reasonPhrase: "OK" },
|
|
2057
|
+
headers: {},
|
|
2058
|
+
body: JSON.stringify({ status: "SUCCESS" })
|
|
2059
|
+
}
|
|
2060
|
+
});
|
|
2061
|
+
const text = raw.text?.content?.trim() || "";
|
|
2062
|
+
if (!text) return;
|
|
2063
|
+
const isGroup = raw.conversationType === "2";
|
|
2064
|
+
const sessionWebhook = raw.sessionWebhook || "";
|
|
2065
|
+
this.callbacks.onText(text, {
|
|
2066
|
+
conversationId: raw.conversationId || "",
|
|
2067
|
+
msgId: raw.msgId || downstream.headers.messageId,
|
|
2068
|
+
senderNick: raw.senderNick || "",
|
|
2069
|
+
senderStaffId: raw.senderStaffId || "",
|
|
2070
|
+
sessionWebhook,
|
|
2071
|
+
chatType: isGroup ? "group" : "single"
|
|
2072
|
+
});
|
|
2073
|
+
} catch (err) {
|
|
2074
|
+
console.error("[dsh-message-gateway] dingtalk message parsing error", err);
|
|
2075
|
+
}
|
|
2076
|
+
});
|
|
2077
|
+
this.client.connect().then(() => {
|
|
2078
|
+
this.setStatus("connected", `Client ID: ${this.cred.clientId}`);
|
|
2079
|
+
console.log("[dsh-message-gateway] dingtalk stream connected for", this.cred.clientId);
|
|
2080
|
+
}).catch((error) => {
|
|
2081
|
+
console.error("[dsh-message-gateway] dingtalk connect failed", error);
|
|
2082
|
+
this.setStatus("error", error instanceof Error ? error.message : String(error));
|
|
2083
|
+
});
|
|
2084
|
+
} catch (error) {
|
|
2085
|
+
console.error("[dsh-message-gateway] dingtalk start failed", error);
|
|
2086
|
+
this.setStatus("error", error instanceof Error ? error.message : String(error));
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
/**
|
|
2090
|
+
* 回复单次消息(通过 sessionWebhook 回复)。
|
|
2091
|
+
*/
|
|
2092
|
+
async replySession(sessionWebhook, content) {
|
|
2093
|
+
if (!sessionWebhook) return false;
|
|
2094
|
+
try {
|
|
2095
|
+
const resp = await fetch(sessionWebhook, {
|
|
2096
|
+
method: "POST",
|
|
2097
|
+
headers: { "content-type": "application/json" },
|
|
2098
|
+
body: JSON.stringify({
|
|
2099
|
+
msgtype: "markdown",
|
|
2100
|
+
markdown: {
|
|
2101
|
+
title: "DSH \u52A9\u624B\u56DE\u590D",
|
|
2102
|
+
text: content
|
|
2103
|
+
}
|
|
2104
|
+
})
|
|
2105
|
+
});
|
|
2106
|
+
const data = await resp.json().catch(() => ({}));
|
|
2107
|
+
return data.errcode === 0;
|
|
2108
|
+
} catch (err) {
|
|
2109
|
+
console.error("[dsh-message-gateway] dingtalk replySession error", err);
|
|
2110
|
+
return false;
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
/** 优雅停止长连接。 */
|
|
2114
|
+
stop() {
|
|
2115
|
+
this.started = false;
|
|
2116
|
+
try {
|
|
2117
|
+
this.client?.disconnect();
|
|
2118
|
+
} catch (e) {
|
|
2119
|
+
console.warn("[dsh-message-gateway] dingtalk disconnect warning", e);
|
|
2120
|
+
}
|
|
2121
|
+
this.client = null;
|
|
2122
|
+
this.setStatus("idle");
|
|
2123
|
+
}
|
|
2124
|
+
};
|
|
2125
|
+
|
|
2022
2126
|
// src/host/bridge-manager.ts
|
|
2023
2127
|
var DEFAULT_MAX_CHAT_AGENTS = 40;
|
|
2024
2128
|
var STREAM_PUSH_INTERVAL = 600;
|
|
@@ -2269,6 +2373,8 @@ var BridgeManager = class {
|
|
|
2269
2373
|
this.email = null;
|
|
2270
2374
|
this.feishu?.stop();
|
|
2271
2375
|
this.feishu = null;
|
|
2376
|
+
this.dingtalk?.stop();
|
|
2377
|
+
this.dingtalk = null;
|
|
2272
2378
|
this.finishAllPending();
|
|
2273
2379
|
const entries = [...this.agents.values()];
|
|
2274
2380
|
this.agents.clear();
|
|
@@ -2735,6 +2841,40 @@ ${secret}`;
|
|
|
2735
2841
|
qq = null;
|
|
2736
2842
|
email = null;
|
|
2737
2843
|
feishu = null;
|
|
2844
|
+
dingtalk = null;
|
|
2845
|
+
/** 启动 DingTalk 桥(企业自建应用 Stream 模式 WebSocket 长连接)。 */
|
|
2846
|
+
startDingTalk(cred) {
|
|
2847
|
+
this.dingtalk?.stop();
|
|
2848
|
+
const bridge = new DingTalkBridge(
|
|
2849
|
+
{ clientId: cred.clientId ?? "", clientSecret: cred.clientSecret ?? "", robotCode: cred.robotCode },
|
|
2850
|
+
{
|
|
2851
|
+
onStatus: (status) => this.onStatusCallback?.(status),
|
|
2852
|
+
onText: (text, frame) => {
|
|
2853
|
+
const identity = {
|
|
2854
|
+
key: `dingtalk:${frame.conversationId}`,
|
|
2855
|
+
frame,
|
|
2856
|
+
sink: {
|
|
2857
|
+
stream: (_f, _streamId, content, finish) => {
|
|
2858
|
+
if (finish && content.trim() && frame.sessionWebhook) {
|
|
2859
|
+
void bridge.replySession(frame.sessionWebhook, content);
|
|
2860
|
+
}
|
|
2861
|
+
},
|
|
2862
|
+
ack: false
|
|
2863
|
+
},
|
|
2864
|
+
chatType: frame.chatType
|
|
2865
|
+
};
|
|
2866
|
+
void this.handleExternalMessage(identity, text);
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
);
|
|
2870
|
+
this.dingtalk = bridge;
|
|
2871
|
+
bridge.start();
|
|
2872
|
+
}
|
|
2873
|
+
/** 停止 DingTalk 桥。 */
|
|
2874
|
+
stopDingTalk() {
|
|
2875
|
+
this.dingtalk?.stop();
|
|
2876
|
+
this.dingtalk = null;
|
|
2877
|
+
}
|
|
2738
2878
|
/** 启动 Feishu 桥(企业自建应用 WebSocket 长连接)。 */
|
|
2739
2879
|
startFeishu(cred) {
|
|
2740
2880
|
this.feishu?.stop();
|
|
@@ -2838,13 +2978,14 @@ ${secret}`;
|
|
|
2838
2978
|
this.email?.stop();
|
|
2839
2979
|
this.email = null;
|
|
2840
2980
|
}
|
|
2841
|
-
/** 任意桥接平台的状态(telegram/discord/qq/email/feishu)。 */
|
|
2981
|
+
/** 任意桥接平台的状态(telegram/discord/qq/email/feishu/dingtalk)。 */
|
|
2842
2982
|
bridgeStatus(id) {
|
|
2843
2983
|
if (id === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2844
2984
|
if (id === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2845
2985
|
if (id === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2846
2986
|
if (id === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2847
2987
|
if (id === "feishu") return this.feishu?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2988
|
+
if (id === "dingtalk") return this.dingtalk?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2848
2989
|
return { state: "idle", detail: "", connectedAt: null };
|
|
2849
2990
|
}
|
|
2850
2991
|
/** 将桥状态并入存储状态。 */
|
|
@@ -2919,7 +3060,8 @@ ${secret}`;
|
|
|
2919
3060
|
bridgeLine("Discord", this.bridgeStatus("discord")),
|
|
2920
3061
|
bridgeLine("QQ", this.bridgeStatus("qq")),
|
|
2921
3062
|
bridgeLine("Email", this.bridgeStatus("email")),
|
|
2922
|
-
bridgeLine("Feishu", this.bridgeStatus("feishu"))
|
|
3063
|
+
bridgeLine("Feishu", this.bridgeStatus("feishu")),
|
|
3064
|
+
bridgeLine("DingTalk", this.bridgeStatus("dingtalk"))
|
|
2923
3065
|
];
|
|
2924
3066
|
reply(lines.join("\n"));
|
|
2925
3067
|
return true;
|
|
@@ -3077,8 +3219,9 @@ var PLATFORMS = [
|
|
|
3077
3219
|
testable: true,
|
|
3078
3220
|
hintKey: "platform.dingtalk.hint",
|
|
3079
3221
|
fields: [
|
|
3080
|
-
{ key: "
|
|
3081
|
-
{ key: "
|
|
3222
|
+
{ key: "clientId", labelKey: "field.dingtalkClientId", placeholderKey: "field.dingtalkClientId.ph", kind: "text" },
|
|
3223
|
+
{ key: "clientSecret", labelKey: "field.dingtalkClientSecret", placeholderKey: "field.dingtalkClientSecret.ph", kind: "secret" },
|
|
3224
|
+
{ key: "robotCode", labelKey: "field.dingtalkRobotCode", placeholderKey: "field.dingtalkRobotCode.ph", kind: "text" }
|
|
3082
3225
|
]
|
|
3083
3226
|
},
|
|
3084
3227
|
{
|
|
@@ -3239,28 +3382,19 @@ async function testPlatform(id, cred) {
|
|
|
3239
3382
|
return { ok: false, detail: banner === "" ? `\u65E0\u6CD5\u8FDE\u63A5 ${cred.imapHost}:${port}` : banner };
|
|
3240
3383
|
}
|
|
3241
3384
|
case "dingtalk": {
|
|
3242
|
-
const
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
3247
|
-
const timestamp = Date.now();
|
|
3248
|
-
const stringToSign = `${timestamp}
|
|
3249
|
-
${cred.secret}`;
|
|
3250
|
-
const sign2 = encodeURIComponent(createHmac2("sha256", cred.secret).update(stringToSign, "utf8").digest("base64"));
|
|
3251
|
-
targetUrl += `${rawUrl.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${sign2}`;
|
|
3252
|
-
}
|
|
3253
|
-
const { status, body } = await httpJson(targetUrl, {
|
|
3385
|
+
const clientId = cred.clientId ?? "";
|
|
3386
|
+
const clientSecret = cred.clientSecret ?? "";
|
|
3387
|
+
if (!clientId || !clientSecret) return { ok: false, detail: "\u8BF7\u586B\u5199 Client ID (AppKey) \u4E0E Client Secret (AppSecret)" };
|
|
3388
|
+
const { status, body } = await httpJson("https://api.dingtalk.com/v1.0/oauth2/accessToken", {
|
|
3254
3389
|
method: "POST",
|
|
3255
3390
|
headers: { "content-type": "application/json" },
|
|
3256
|
-
body: JSON.stringify({
|
|
3257
|
-
msgtype: "markdown",
|
|
3258
|
-
markdown: { title: "DSH \u8FDE\u63A5\u6D4B\u8BD5", text: "### \u{1F50C} DSH Message Gateway\n\u9489\u9489\u81EA\u5B9A\u4E49\u673A\u5668\u4EBA\u8FDE\u63A5\u6D4B\u8BD5\u6210\u529F\uFF01" }
|
|
3259
|
-
})
|
|
3391
|
+
body: JSON.stringify({ appKey: clientId, appSecret: clientSecret })
|
|
3260
3392
|
});
|
|
3261
3393
|
const r = body;
|
|
3262
|
-
if (r.
|
|
3263
|
-
|
|
3394
|
+
if (r.accessToken !== void 0) {
|
|
3395
|
+
return { ok: true, detail: `\u8BA4\u8BC1\u6210\u529F \xB7 Client ID: ${clientId}` };
|
|
3396
|
+
}
|
|
3397
|
+
return { ok: false, detail: r.message ?? `HTTP ${status}` };
|
|
3264
3398
|
}
|
|
3265
3399
|
case "feishu": {
|
|
3266
3400
|
const appId = cred.appId ?? "";
|
|
@@ -3652,7 +3786,7 @@ async function buildView(manager) {
|
|
|
3652
3786
|
let state = stored?.state ?? (def.id === "wechat" || def.id === "webhooks" ? "manual" : "none");
|
|
3653
3787
|
let detail = stored?.detail ?? "";
|
|
3654
3788
|
let testedAt = stored?.testedAt ?? null;
|
|
3655
|
-
if (def.id === "wecom-aibot" || def.id === "telegram" || def.id === "discord" || def.id === "qq" || def.id === "email" || def.id === "feishu") {
|
|
3789
|
+
if (def.id === "wecom-aibot" || def.id === "telegram" || def.id === "discord" || def.id === "qq" || def.id === "email" || def.id === "feishu" || def.id === "dingtalk") {
|
|
3656
3790
|
const merged = manager.mergeStatus(stored, def.id);
|
|
3657
3791
|
state = merged.state;
|
|
3658
3792
|
detail = merged.detail;
|
|
@@ -4002,6 +4136,7 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
4002
4136
|
if (platform === "qq") manager.stopQQ();
|
|
4003
4137
|
if (platform === "email") manager.stopEmail();
|
|
4004
4138
|
if (platform === "feishu") manager.stopFeishu();
|
|
4139
|
+
if (platform === "dingtalk") manager.stopDingTalk();
|
|
4005
4140
|
json(res, { ok: true, value: await buildView(manager) });
|
|
4006
4141
|
return;
|
|
4007
4142
|
}
|
|
@@ -4051,6 +4186,13 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
4051
4186
|
manager.stopFeishu();
|
|
4052
4187
|
}
|
|
4053
4188
|
}
|
|
4189
|
+
if (platform === "dingtalk") {
|
|
4190
|
+
if (credentials2.clientId !== void 0 && credentials2.clientId !== "" && credentials2.clientSecret !== void 0 && credentials2.clientSecret !== "") {
|
|
4191
|
+
manager.startDingTalk(credentials2);
|
|
4192
|
+
} else {
|
|
4193
|
+
manager.stopDingTalk();
|
|
4194
|
+
}
|
|
4195
|
+
}
|
|
4054
4196
|
json(res, { ok: true, value: await buildView(manager) });
|
|
4055
4197
|
return;
|
|
4056
4198
|
}
|
|
@@ -4091,6 +4233,7 @@ var Config = Schema.object({
|
|
|
4091
4233
|
autoStartQQ: Schema.boolean().default(true).description("\u542F\u52A8\u65F6\u81EA\u52A8\u7528\u5DF2\u4FDD\u5B58\u7684 QQ \u51ED\u636E\u5EFA\u7ACB\u5E38\u9A7B\u8FDE\u63A5"),
|
|
4092
4234
|
autoStartEmail: Schema.boolean().default(true).description("\u542F\u52A8\u65F6\u81EA\u52A8\u7528\u5DF2\u4FDD\u5B58\u7684 Email \u51ED\u636E\u5EFA\u7ACB\u5E38\u9A7B\u8FDE\u63A5\uFF08IMAP \u8F6E\u8BE2\uFF09"),
|
|
4093
4235
|
autoStartFeishu: Schema.boolean().default(true).description("\u542F\u52A8\u65F6\u81EA\u52A8\u7528\u5DF2\u4FDD\u5B58\u7684\u98DE\u4E66\u5E94\u7528\u51ED\u636E\u5EFA\u7ACB\u5E38\u9A7B\u8FDE\u63A5\uFF08WebSocket \u957F\u8FDE\u63A5\uFF09"),
|
|
4236
|
+
autoStartDingtalk: Schema.boolean().default(true).description("\u542F\u52A8\u65F6\u81EA\u52A8\u7528\u5DF2\u4FDD\u5B58\u7684\u9489\u9489\u5E94\u7528\u51ED\u636E\u5EFA\u7ACB\u5E38\u9A7B\u8FDE\u63A5\uFF08Stream \u957F\u8FDE\u63A5\uFF09"),
|
|
4094
4237
|
groupReply: Schema.boolean().default(true).description("\u662F\u5426\u56DE\u590D\u7FA4\u804A\u6D88\u606F\uFF08false \u65F6\u53EA\u5904\u7406\u5355\u804A\uFF09"),
|
|
4095
4238
|
welcomeReply: Schema.boolean().default(false).description("\u662F\u5426\u5728\u7528\u6237\u5F53\u5929\u9996\u6B21\u8FDB\u5165\u5355\u804A\u65F6\u81EA\u52A8\u53D1\u9001\u6B22\u8FCE\u8BED\uFF08\u9ED8\u8BA4 false \u4FDD\u6301\u514D\u6253\u6270\uFF0C\u8BBE\u4E3A true \u5F00\u542F\uFF09"),
|
|
4096
4239
|
outboundWebhooks: Schema.array(Schema.object({
|
|
@@ -4147,18 +4290,23 @@ function apply(ctx, config = Config({})) {
|
|
|
4147
4290
|
manager.startFeishu(feishu);
|
|
4148
4291
|
console.log("[dsh-message-gateway] feishu bridge auto-started");
|
|
4149
4292
|
}
|
|
4293
|
+
const dingtalk = store.platforms.dingtalk;
|
|
4294
|
+
if (config.autoStartDingtalk && dingtalk !== void 0 && dingtalk.clientId !== "" && dingtalk.clientSecret !== "") {
|
|
4295
|
+
manager.startDingTalk(dingtalk);
|
|
4296
|
+
console.log("[dsh-message-gateway] dingtalk bridge auto-started");
|
|
4297
|
+
}
|
|
4150
4298
|
});
|
|
4151
4299
|
const disposeRoutes = registerGatewayRoutes(ctx, manager);
|
|
4152
4300
|
let disposeTools;
|
|
4153
4301
|
if (ctx.tools) {
|
|
4154
4302
|
const disposeSend = ctx.tools.register(defineTool({
|
|
4155
4303
|
name: "send_chat_message",
|
|
4156
|
-
description: "\u5411\u5DF2\u8FDE\u63A5\u7684\u6D88\u606F\u5E73\u53F0\uFF08Telegram / Discord / \u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u673A\u5668\u4EBA / \u98DE\u4E66 / 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",
|
|
4304
|
+
description: "\u5411\u5DF2\u8FDE\u63A5\u7684\u6D88\u606F\u5E73\u53F0\uFF08Telegram / Discord / \u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u673A\u5668\u4EBA / \u98DE\u4E66 / \u9489\u9489 / 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",
|
|
4157
4305
|
parameters: {
|
|
4158
4306
|
platform: {
|
|
4159
4307
|
type: "string",
|
|
4160
4308
|
required: true,
|
|
4161
|
-
description: "\u76EE\u6807\u5E73\u53F0 id\uFF1Atelegram / discord / wecom-aibot / feishu / email"
|
|
4309
|
+
description: "\u76EE\u6807\u5E73\u53F0 id\uFF1Atelegram / discord / wecom-aibot / feishu / dingtalk / email"
|
|
4162
4310
|
},
|
|
4163
4311
|
target: {
|
|
4164
4312
|
type: "string",
|