dsh-message-gateway 0.1.30 → 0.1.32
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 +53 -7
- package/lib/client.js.map +2 -2
- package/lib/index.js +286 -65
- package/lib/index.js.map +4 -4
- package/package.json +3 -2
package/lib/index.js
CHANGED
|
@@ -458,6 +458,67 @@ var WecomBridge = class {
|
|
|
458
458
|
}
|
|
459
459
|
};
|
|
460
460
|
|
|
461
|
+
// src/host/proxy.ts
|
|
462
|
+
import { ProxyAgent, fetch as undiciFetch } from "undici";
|
|
463
|
+
var cachedProxyUrl = void 0;
|
|
464
|
+
async function getProxyUrl() {
|
|
465
|
+
if (cachedProxyUrl !== void 0) return cachedProxyUrl;
|
|
466
|
+
const envProxy = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY || process.env.all_proxy || process.env.ALL_PROXY;
|
|
467
|
+
if (envProxy) {
|
|
468
|
+
cachedProxyUrl = envProxy;
|
|
469
|
+
return cachedProxyUrl;
|
|
470
|
+
}
|
|
471
|
+
const commonPorts = [7890, 7897, 10808, 10809, 1087];
|
|
472
|
+
const net = await import("node:net");
|
|
473
|
+
for (const port of commonPorts) {
|
|
474
|
+
const isUp = await new Promise((resolve) => {
|
|
475
|
+
const socket = net.connect({ host: "127.0.0.1", port, timeout: 500 });
|
|
476
|
+
socket.once("connect", () => {
|
|
477
|
+
socket.destroy();
|
|
478
|
+
resolve(true);
|
|
479
|
+
});
|
|
480
|
+
socket.once("error", () => resolve(false));
|
|
481
|
+
socket.once("timeout", () => {
|
|
482
|
+
socket.destroy();
|
|
483
|
+
resolve(false);
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
if (isUp) {
|
|
487
|
+
cachedProxyUrl = `http://127.0.0.1:${port}`;
|
|
488
|
+
console.log(`[dsh-message-gateway] auto-detected local proxy on port ${port}`);
|
|
489
|
+
return cachedProxyUrl;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
cachedProxyUrl = null;
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
async function getProxyDispatcher() {
|
|
496
|
+
const proxyUrl = await getProxyUrl();
|
|
497
|
+
if (proxyUrl) {
|
|
498
|
+
return new ProxyAgent(proxyUrl);
|
|
499
|
+
}
|
|
500
|
+
return void 0;
|
|
501
|
+
}
|
|
502
|
+
async function smartFetch(url, init) {
|
|
503
|
+
const urlStr = String(url);
|
|
504
|
+
const isOverseas = /discord\.com|telegram\.org|api\.openai\.com/i.test(urlStr);
|
|
505
|
+
if (isOverseas) {
|
|
506
|
+
const dispatcher = await getProxyDispatcher();
|
|
507
|
+
if (dispatcher) {
|
|
508
|
+
const res2 = await undiciFetch(urlStr, { ...init, dispatcher });
|
|
509
|
+
return {
|
|
510
|
+
status: res2.status,
|
|
511
|
+
json: () => res2.json()
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
const res = await fetch(url, init);
|
|
516
|
+
return {
|
|
517
|
+
status: res.status,
|
|
518
|
+
json: () => res.json()
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
|
|
461
522
|
// src/host/telegram-bridge.ts
|
|
462
523
|
var EDIT_INTERVAL_MS = 1200;
|
|
463
524
|
var MSG_LIMIT = 4096;
|
|
@@ -491,11 +552,13 @@ var TelegramBridge = class {
|
|
|
491
552
|
const controller = new AbortController();
|
|
492
553
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
493
554
|
try {
|
|
555
|
+
const dispatcher = await getProxyDispatcher();
|
|
494
556
|
const response = await fetch(`https://api.telegram.org/bot${this.token}/${method}`, {
|
|
495
557
|
method: "POST",
|
|
496
558
|
headers: { "content-type": "application/json" },
|
|
497
559
|
body: JSON.stringify(params),
|
|
498
|
-
signal: controller.signal
|
|
560
|
+
signal: controller.signal,
|
|
561
|
+
...dispatcher ? { dispatcher } : {}
|
|
499
562
|
});
|
|
500
563
|
const body = await response.json();
|
|
501
564
|
if (body.ok === true) return body.result;
|
|
@@ -648,6 +711,7 @@ var TelegramBridge = class {
|
|
|
648
711
|
};
|
|
649
712
|
|
|
650
713
|
// src/host/discord-bridge.ts
|
|
714
|
+
import { WebSocket as UndiciWebSocket } from "undici";
|
|
651
715
|
var GATEWAY_URL = "wss://gateway.discord.gg/?v=10&encoding=json";
|
|
652
716
|
var REST = "https://discord.com/api/v10";
|
|
653
717
|
var INTENTS = 1 << 0 | 1 << 9 | 1 << 12 | 1 << 15;
|
|
@@ -681,26 +745,33 @@ var DiscordBridge = class {
|
|
|
681
745
|
}
|
|
682
746
|
connect() {
|
|
683
747
|
if (this.stopped) return;
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
this.clearHeartbeat();
|
|
698
|
-
this.ws = null;
|
|
699
|
-
if (!this.stopped) {
|
|
700
|
-
console.warn("[dsh-message-gateway] discord gateway closed, reconnect in 3s");
|
|
701
|
-
setTimeout(() => this.connect(), 3e3);
|
|
748
|
+
void (async () => {
|
|
749
|
+
let ws;
|
|
750
|
+
try {
|
|
751
|
+
const dispatcher = await getProxyDispatcher();
|
|
752
|
+
if (dispatcher) {
|
|
753
|
+
ws = new UndiciWebSocket(GATEWAY_URL, { dispatcher });
|
|
754
|
+
} else {
|
|
755
|
+
ws = new WebSocket(GATEWAY_URL);
|
|
756
|
+
}
|
|
757
|
+
} catch (error) {
|
|
758
|
+
console.error("[dsh-message-gateway] discord ws create failed", error);
|
|
759
|
+
this.setStatus("error", "ws create failed");
|
|
760
|
+
return;
|
|
702
761
|
}
|
|
703
|
-
|
|
762
|
+
this.ws = ws;
|
|
763
|
+
ws.onopen = () => console.log("[dsh-message-gateway] discord gateway open");
|
|
764
|
+
ws.onmessage = (event) => this.onMessage(String(event.data));
|
|
765
|
+
ws.onerror = () => console.warn("[dsh-message-gateway] discord gateway ws error");
|
|
766
|
+
ws.onclose = () => {
|
|
767
|
+
this.clearHeartbeat();
|
|
768
|
+
this.ws = null;
|
|
769
|
+
if (!this.stopped) {
|
|
770
|
+
console.warn("[dsh-message-gateway] discord gateway closed, reconnect in 3s");
|
|
771
|
+
setTimeout(() => this.connect(), 3e3);
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
})();
|
|
704
775
|
}
|
|
705
776
|
onMessage(raw) {
|
|
706
777
|
let payload;
|
|
@@ -723,8 +794,10 @@ var DiscordBridge = class {
|
|
|
723
794
|
}
|
|
724
795
|
case 0: {
|
|
725
796
|
if (payload.t === "READY") {
|
|
726
|
-
|
|
727
|
-
|
|
797
|
+
const user = payload.d?.user;
|
|
798
|
+
const name = user?.username ? `@${user.username}` : "\u5DF2\u8FDE\u63A5";
|
|
799
|
+
this.setStatus("connected", name);
|
|
800
|
+
console.log("[dsh-message-gateway] discord READY", name);
|
|
728
801
|
} else if (payload.t === "MESSAGE_CREATE") {
|
|
729
802
|
this.handleMessage(payload.d);
|
|
730
803
|
}
|
|
@@ -737,7 +810,7 @@ var DiscordBridge = class {
|
|
|
737
810
|
}
|
|
738
811
|
}
|
|
739
812
|
sendOp(op, d) {
|
|
740
|
-
if (this.ws === null || this.ws.readyState !==
|
|
813
|
+
if (this.ws === null || this.ws.readyState !== 1) return;
|
|
741
814
|
this.ws.send(JSON.stringify({ op, d }));
|
|
742
815
|
}
|
|
743
816
|
identify() {
|
|
@@ -762,17 +835,14 @@ var DiscordBridge = class {
|
|
|
762
835
|
const controller = new AbortController();
|
|
763
836
|
const timer = setTimeout(() => controller.abort(), 15e3);
|
|
764
837
|
try {
|
|
765
|
-
const response = await
|
|
838
|
+
const response = await smartFetch(`${REST}${path}`, {
|
|
766
839
|
method,
|
|
767
840
|
headers: { authorization: `Bot ${this.token}`, "content-type": "application/json" },
|
|
768
841
|
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
769
842
|
signal: controller.signal
|
|
770
843
|
});
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
return null;
|
|
774
|
-
}
|
|
775
|
-
return await response.json();
|
|
844
|
+
const data = await response.json();
|
|
845
|
+
return data;
|
|
776
846
|
} catch (error) {
|
|
777
847
|
console.warn("[dsh-message-gateway] discord rest failed", method, path, String(error));
|
|
778
848
|
return null;
|
|
@@ -2019,6 +2089,110 @@ var FeishuBridge = class {
|
|
|
2019
2089
|
}
|
|
2020
2090
|
};
|
|
2021
2091
|
|
|
2092
|
+
// src/host/dingtalk-bridge.ts
|
|
2093
|
+
import { DWClient, TOPIC_ROBOT } from "dingtalk-stream";
|
|
2094
|
+
var DingTalkBridge = class {
|
|
2095
|
+
constructor(cred, callbacks) {
|
|
2096
|
+
this.cred = cred;
|
|
2097
|
+
this.callbacks = callbacks;
|
|
2098
|
+
}
|
|
2099
|
+
client = null;
|
|
2100
|
+
started = false;
|
|
2101
|
+
status = { state: "idle", detail: "", connectedAt: null };
|
|
2102
|
+
setStatus(state, detail = "") {
|
|
2103
|
+
this.status = {
|
|
2104
|
+
state,
|
|
2105
|
+
detail,
|
|
2106
|
+
connectedAt: state === "connected" ? this.status.connectedAt ?? Date.now() : null
|
|
2107
|
+
};
|
|
2108
|
+
this.callbacks.onStatus(this.status);
|
|
2109
|
+
}
|
|
2110
|
+
/** 建立长连接。 */
|
|
2111
|
+
start() {
|
|
2112
|
+
if (this.started) return;
|
|
2113
|
+
this.started = true;
|
|
2114
|
+
this.setStatus("connecting", "\u6B63\u5728\u8FDE\u63A5\u9489\u9489 Stream \u957F\u8FDE\u63A5\u2026");
|
|
2115
|
+
try {
|
|
2116
|
+
this.client = new DWClient({
|
|
2117
|
+
clientId: this.cred.clientId,
|
|
2118
|
+
clientSecret: this.cred.clientSecret,
|
|
2119
|
+
debug: false
|
|
2120
|
+
});
|
|
2121
|
+
this.client.registerCallbackListener(TOPIC_ROBOT, async (downstream) => {
|
|
2122
|
+
try {
|
|
2123
|
+
const raw = JSON.parse(downstream.data || "{}");
|
|
2124
|
+
this.client?.socketCallBackResponse(downstream.headers.messageId, {
|
|
2125
|
+
response: {
|
|
2126
|
+
statusLine: { code: 200, reasonPhrase: "OK" },
|
|
2127
|
+
headers: {},
|
|
2128
|
+
body: JSON.stringify({ status: "SUCCESS" })
|
|
2129
|
+
}
|
|
2130
|
+
});
|
|
2131
|
+
const text = raw.text?.content?.trim() || "";
|
|
2132
|
+
if (!text) return;
|
|
2133
|
+
const isGroup = raw.conversationType === "2";
|
|
2134
|
+
const sessionWebhook = raw.sessionWebhook || "";
|
|
2135
|
+
this.callbacks.onText(text, {
|
|
2136
|
+
conversationId: raw.conversationId || "",
|
|
2137
|
+
msgId: raw.msgId || downstream.headers.messageId,
|
|
2138
|
+
senderNick: raw.senderNick || "",
|
|
2139
|
+
senderStaffId: raw.senderStaffId || "",
|
|
2140
|
+
sessionWebhook,
|
|
2141
|
+
chatType: isGroup ? "group" : "single"
|
|
2142
|
+
});
|
|
2143
|
+
} catch (err) {
|
|
2144
|
+
console.error("[dsh-message-gateway] dingtalk message parsing error", err);
|
|
2145
|
+
}
|
|
2146
|
+
});
|
|
2147
|
+
this.client.connect().then(() => {
|
|
2148
|
+
this.setStatus("connected", `Client ID: ${this.cred.clientId}`);
|
|
2149
|
+
console.log("[dsh-message-gateway] dingtalk stream connected for", this.cred.clientId);
|
|
2150
|
+
}).catch((error) => {
|
|
2151
|
+
console.error("[dsh-message-gateway] dingtalk connect failed", error);
|
|
2152
|
+
this.setStatus("error", error instanceof Error ? error.message : String(error));
|
|
2153
|
+
});
|
|
2154
|
+
} catch (error) {
|
|
2155
|
+
console.error("[dsh-message-gateway] dingtalk start failed", error);
|
|
2156
|
+
this.setStatus("error", error instanceof Error ? error.message : String(error));
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* 回复单次消息(通过 sessionWebhook 回复)。
|
|
2161
|
+
*/
|
|
2162
|
+
async replySession(sessionWebhook, content) {
|
|
2163
|
+
if (!sessionWebhook) return false;
|
|
2164
|
+
try {
|
|
2165
|
+
const resp = await fetch(sessionWebhook, {
|
|
2166
|
+
method: "POST",
|
|
2167
|
+
headers: { "content-type": "application/json" },
|
|
2168
|
+
body: JSON.stringify({
|
|
2169
|
+
msgtype: "markdown",
|
|
2170
|
+
markdown: {
|
|
2171
|
+
title: "DSH \u52A9\u624B\u56DE\u590D",
|
|
2172
|
+
text: content
|
|
2173
|
+
}
|
|
2174
|
+
})
|
|
2175
|
+
});
|
|
2176
|
+
const data = await resp.json().catch(() => ({}));
|
|
2177
|
+
return data.errcode === 0;
|
|
2178
|
+
} catch (err) {
|
|
2179
|
+
console.error("[dsh-message-gateway] dingtalk replySession error", err);
|
|
2180
|
+
return false;
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
/** 优雅停止长连接。 */
|
|
2184
|
+
stop() {
|
|
2185
|
+
this.started = false;
|
|
2186
|
+
try {
|
|
2187
|
+
this.client?.disconnect();
|
|
2188
|
+
} catch (e) {
|
|
2189
|
+
console.warn("[dsh-message-gateway] dingtalk disconnect warning", e);
|
|
2190
|
+
}
|
|
2191
|
+
this.client = null;
|
|
2192
|
+
this.setStatus("idle");
|
|
2193
|
+
}
|
|
2194
|
+
};
|
|
2195
|
+
|
|
2022
2196
|
// src/host/bridge-manager.ts
|
|
2023
2197
|
var DEFAULT_MAX_CHAT_AGENTS = 40;
|
|
2024
2198
|
var STREAM_PUSH_INTERVAL = 600;
|
|
@@ -2269,6 +2443,8 @@ var BridgeManager = class {
|
|
|
2269
2443
|
this.email = null;
|
|
2270
2444
|
this.feishu?.stop();
|
|
2271
2445
|
this.feishu = null;
|
|
2446
|
+
this.dingtalk?.stop();
|
|
2447
|
+
this.dingtalk = null;
|
|
2272
2448
|
this.finishAllPending();
|
|
2273
2449
|
const entries = [...this.agents.values()];
|
|
2274
2450
|
this.agents.clear();
|
|
@@ -2735,6 +2911,40 @@ ${secret}`;
|
|
|
2735
2911
|
qq = null;
|
|
2736
2912
|
email = null;
|
|
2737
2913
|
feishu = null;
|
|
2914
|
+
dingtalk = null;
|
|
2915
|
+
/** 启动 DingTalk 桥(企业自建应用 Stream 模式 WebSocket 长连接)。 */
|
|
2916
|
+
startDingTalk(cred) {
|
|
2917
|
+
this.dingtalk?.stop();
|
|
2918
|
+
const bridge = new DingTalkBridge(
|
|
2919
|
+
{ clientId: cred.clientId ?? "", clientSecret: cred.clientSecret ?? "", robotCode: cred.robotCode },
|
|
2920
|
+
{
|
|
2921
|
+
onStatus: (status) => this.onStatusCallback?.(status),
|
|
2922
|
+
onText: (text, frame) => {
|
|
2923
|
+
const identity = {
|
|
2924
|
+
key: `dingtalk:${frame.conversationId}`,
|
|
2925
|
+
frame,
|
|
2926
|
+
sink: {
|
|
2927
|
+
stream: (_f, _streamId, content, finish) => {
|
|
2928
|
+
if (finish && content.trim() && frame.sessionWebhook) {
|
|
2929
|
+
void bridge.replySession(frame.sessionWebhook, content);
|
|
2930
|
+
}
|
|
2931
|
+
},
|
|
2932
|
+
ack: false
|
|
2933
|
+
},
|
|
2934
|
+
chatType: frame.chatType
|
|
2935
|
+
};
|
|
2936
|
+
void this.handleExternalMessage(identity, text);
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
);
|
|
2940
|
+
this.dingtalk = bridge;
|
|
2941
|
+
bridge.start();
|
|
2942
|
+
}
|
|
2943
|
+
/** 停止 DingTalk 桥。 */
|
|
2944
|
+
stopDingTalk() {
|
|
2945
|
+
this.dingtalk?.stop();
|
|
2946
|
+
this.dingtalk = null;
|
|
2947
|
+
}
|
|
2738
2948
|
/** 启动 Feishu 桥(企业自建应用 WebSocket 长连接)。 */
|
|
2739
2949
|
startFeishu(cred) {
|
|
2740
2950
|
this.feishu?.stop();
|
|
@@ -2838,13 +3048,14 @@ ${secret}`;
|
|
|
2838
3048
|
this.email?.stop();
|
|
2839
3049
|
this.email = null;
|
|
2840
3050
|
}
|
|
2841
|
-
/** 任意桥接平台的状态(telegram/discord/qq/email/feishu)。 */
|
|
3051
|
+
/** 任意桥接平台的状态(telegram/discord/qq/email/feishu/dingtalk)。 */
|
|
2842
3052
|
bridgeStatus(id) {
|
|
2843
3053
|
if (id === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2844
3054
|
if (id === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2845
3055
|
if (id === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2846
3056
|
if (id === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2847
3057
|
if (id === "feishu") return this.feishu?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
3058
|
+
if (id === "dingtalk") return this.dingtalk?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2848
3059
|
return { state: "idle", detail: "", connectedAt: null };
|
|
2849
3060
|
}
|
|
2850
3061
|
/** 将桥状态并入存储状态。 */
|
|
@@ -2919,7 +3130,8 @@ ${secret}`;
|
|
|
2919
3130
|
bridgeLine("Discord", this.bridgeStatus("discord")),
|
|
2920
3131
|
bridgeLine("QQ", this.bridgeStatus("qq")),
|
|
2921
3132
|
bridgeLine("Email", this.bridgeStatus("email")),
|
|
2922
|
-
bridgeLine("Feishu", this.bridgeStatus("feishu"))
|
|
3133
|
+
bridgeLine("Feishu", this.bridgeStatus("feishu")),
|
|
3134
|
+
bridgeLine("DingTalk", this.bridgeStatus("dingtalk"))
|
|
2923
3135
|
];
|
|
2924
3136
|
reply(lines.join("\n"));
|
|
2925
3137
|
return true;
|
|
@@ -3077,8 +3289,9 @@ var PLATFORMS = [
|
|
|
3077
3289
|
testable: true,
|
|
3078
3290
|
hintKey: "platform.dingtalk.hint",
|
|
3079
3291
|
fields: [
|
|
3080
|
-
{ key: "
|
|
3081
|
-
{ key: "
|
|
3292
|
+
{ key: "clientId", labelKey: "field.dingtalkClientId", placeholderKey: "field.dingtalkClientId.ph", kind: "text" },
|
|
3293
|
+
{ key: "clientSecret", labelKey: "field.dingtalkClientSecret", placeholderKey: "field.dingtalkClientSecret.ph", kind: "secret" },
|
|
3294
|
+
{ key: "robotCode", labelKey: "field.dingtalkRobotCode", placeholderKey: "field.dingtalkRobotCode.ph", kind: "text" }
|
|
3082
3295
|
]
|
|
3083
3296
|
},
|
|
3084
3297
|
{
|
|
@@ -3129,7 +3342,7 @@ async function httpJson(url, init) {
|
|
|
3129
3342
|
const controller = new AbortController();
|
|
3130
3343
|
const timer = setTimeout(() => controller.abort(), 12e3);
|
|
3131
3344
|
try {
|
|
3132
|
-
const response = await
|
|
3345
|
+
const response = await smartFetch(url, { ...init, signal: controller.signal });
|
|
3133
3346
|
let body = null;
|
|
3134
3347
|
try {
|
|
3135
3348
|
body = await response.json();
|
|
@@ -3239,28 +3452,19 @@ async function testPlatform(id, cred) {
|
|
|
3239
3452
|
return { ok: false, detail: banner === "" ? `\u65E0\u6CD5\u8FDE\u63A5 ${cred.imapHost}:${port}` : banner };
|
|
3240
3453
|
}
|
|
3241
3454
|
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, {
|
|
3455
|
+
const clientId = cred.clientId ?? "";
|
|
3456
|
+
const clientSecret = cred.clientSecret ?? "";
|
|
3457
|
+
if (!clientId || !clientSecret) return { ok: false, detail: "\u8BF7\u586B\u5199 Client ID (AppKey) \u4E0E Client Secret (AppSecret)" };
|
|
3458
|
+
const { status, body } = await httpJson("https://api.dingtalk.com/v1.0/oauth2/accessToken", {
|
|
3254
3459
|
method: "POST",
|
|
3255
3460
|
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
|
-
})
|
|
3461
|
+
body: JSON.stringify({ appKey: clientId, appSecret: clientSecret })
|
|
3260
3462
|
});
|
|
3261
3463
|
const r = body;
|
|
3262
|
-
if (r.
|
|
3263
|
-
|
|
3464
|
+
if (r.accessToken !== void 0) {
|
|
3465
|
+
return { ok: true, detail: `\u8BA4\u8BC1\u6210\u529F \xB7 Client ID: ${clientId}` };
|
|
3466
|
+
}
|
|
3467
|
+
return { ok: false, detail: r.message ?? `HTTP ${status}` };
|
|
3264
3468
|
}
|
|
3265
3469
|
case "feishu": {
|
|
3266
3470
|
const appId = cred.appId ?? "";
|
|
@@ -3649,10 +3853,10 @@ async function buildView(manager) {
|
|
|
3649
3853
|
for (const def of PLATFORMS) {
|
|
3650
3854
|
const stored = store.statuses[def.id];
|
|
3651
3855
|
const configured = store.platforms[def.id] !== void 0;
|
|
3652
|
-
let state = stored?.state ??
|
|
3653
|
-
let detail = stored?.detail ?? "";
|
|
3654
|
-
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") {
|
|
3856
|
+
let state = configured ? stored?.state ?? "none" : def.id === "wechat" || def.id === "webhooks" ? "manual" : "none";
|
|
3857
|
+
let detail = configured ? stored?.detail ?? "" : "";
|
|
3858
|
+
let testedAt = configured ? stored?.testedAt ?? null : null;
|
|
3859
|
+
if (configured && (def.id === "wecom-aibot" || def.id === "telegram" || def.id === "discord" || def.id === "qq" || def.id === "email" || def.id === "feishu" || def.id === "dingtalk")) {
|
|
3656
3860
|
const merged = manager.mergeStatus(stored, def.id);
|
|
3657
3861
|
state = merged.state;
|
|
3658
3862
|
detail = merged.detail;
|
|
@@ -4002,6 +4206,7 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
4002
4206
|
if (platform === "qq") manager.stopQQ();
|
|
4003
4207
|
if (platform === "email") manager.stopEmail();
|
|
4004
4208
|
if (platform === "feishu") manager.stopFeishu();
|
|
4209
|
+
if (platform === "dingtalk") manager.stopDingTalk();
|
|
4005
4210
|
json(res, { ok: true, value: await buildView(manager) });
|
|
4006
4211
|
return;
|
|
4007
4212
|
}
|
|
@@ -4051,20 +4256,30 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
4051
4256
|
manager.stopFeishu();
|
|
4052
4257
|
}
|
|
4053
4258
|
}
|
|
4259
|
+
if (platform === "dingtalk") {
|
|
4260
|
+
if (credentials2.clientId !== void 0 && credentials2.clientId !== "" && credentials2.clientSecret !== void 0 && credentials2.clientSecret !== "") {
|
|
4261
|
+
manager.startDingTalk(credentials2);
|
|
4262
|
+
} else {
|
|
4263
|
+
manager.stopDingTalk();
|
|
4264
|
+
}
|
|
4265
|
+
}
|
|
4054
4266
|
json(res, { ok: true, value: await buildView(manager) });
|
|
4055
4267
|
return;
|
|
4056
4268
|
}
|
|
4057
4269
|
const submitted = extractCredentials(platform, payload);
|
|
4058
4270
|
const hasSubmitted = Object.keys(submitted).length > 0;
|
|
4271
|
+
const isConfigured = store.platforms[platform] !== void 0;
|
|
4059
4272
|
const credentials = hasSubmitted ? submitted : store.platforms[platform] ?? {};
|
|
4060
4273
|
const result = await testPlatform(platform, credentials);
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4274
|
+
if (isConfigured && !hasSubmitted) {
|
|
4275
|
+
const stored = {
|
|
4276
|
+
state: result.ok ? "connected" : "error",
|
|
4277
|
+
detail: result.detail,
|
|
4278
|
+
testedAt: Date.now()
|
|
4279
|
+
};
|
|
4280
|
+
store.statuses[platform] = stored;
|
|
4281
|
+
await saveStore(store);
|
|
4282
|
+
}
|
|
4068
4283
|
json(res, { ok: true, value: { ok: result.ok, detail: result.detail } });
|
|
4069
4284
|
return;
|
|
4070
4285
|
}
|
|
@@ -4091,6 +4306,7 @@ var Config = Schema.object({
|
|
|
4091
4306
|
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
4307
|
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
4308
|
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"),
|
|
4309
|
+
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
4310
|
groupReply: Schema.boolean().default(true).description("\u662F\u5426\u56DE\u590D\u7FA4\u804A\u6D88\u606F\uFF08false \u65F6\u53EA\u5904\u7406\u5355\u804A\uFF09"),
|
|
4095
4311
|
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
4312
|
outboundWebhooks: Schema.array(Schema.object({
|
|
@@ -4147,18 +4363,23 @@ function apply(ctx, config = Config({})) {
|
|
|
4147
4363
|
manager.startFeishu(feishu);
|
|
4148
4364
|
console.log("[dsh-message-gateway] feishu bridge auto-started");
|
|
4149
4365
|
}
|
|
4366
|
+
const dingtalk = store.platforms.dingtalk;
|
|
4367
|
+
if (config.autoStartDingtalk && dingtalk !== void 0 && dingtalk.clientId !== "" && dingtalk.clientSecret !== "") {
|
|
4368
|
+
manager.startDingTalk(dingtalk);
|
|
4369
|
+
console.log("[dsh-message-gateway] dingtalk bridge auto-started");
|
|
4370
|
+
}
|
|
4150
4371
|
});
|
|
4151
4372
|
const disposeRoutes = registerGatewayRoutes(ctx, manager);
|
|
4152
4373
|
let disposeTools;
|
|
4153
4374
|
if (ctx.tools) {
|
|
4154
4375
|
const disposeSend = ctx.tools.register(defineTool({
|
|
4155
4376
|
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",
|
|
4377
|
+
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
4378
|
parameters: {
|
|
4158
4379
|
platform: {
|
|
4159
4380
|
type: "string",
|
|
4160
4381
|
required: true,
|
|
4161
|
-
description: "\u76EE\u6807\u5E73\u53F0 id\uFF1Atelegram / discord / wecom-aibot / feishu / email"
|
|
4382
|
+
description: "\u76EE\u6807\u5E73\u53F0 id\uFF1Atelegram / discord / wecom-aibot / feishu / dingtalk / email"
|
|
4162
4383
|
},
|
|
4163
4384
|
target: {
|
|
4164
4385
|
type: "string",
|