dsh-message-gateway 0.1.29 → 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 +56 -10
- package/lib/client.js.map +2 -2
- package/lib/index.js +390 -50
- package/lib/index.js.map +4 -4
- package/package.json +4 -2
package/lib/index.js
CHANGED
|
@@ -1895,6 +1895,234 @@ function sleep(ms) {
|
|
|
1895
1895
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1896
1896
|
}
|
|
1897
1897
|
|
|
1898
|
+
// src/host/feishu-bridge.ts
|
|
1899
|
+
import * as lark from "@larksuiteoapi/node-sdk";
|
|
1900
|
+
var FeishuBridge = class {
|
|
1901
|
+
constructor(cred, callbacks) {
|
|
1902
|
+
this.cred = cred;
|
|
1903
|
+
this.callbacks = callbacks;
|
|
1904
|
+
this.client = new lark.Client({
|
|
1905
|
+
appId: cred.appId,
|
|
1906
|
+
appSecret: cred.appSecret
|
|
1907
|
+
});
|
|
1908
|
+
}
|
|
1909
|
+
client = null;
|
|
1910
|
+
wsClient = null;
|
|
1911
|
+
started = false;
|
|
1912
|
+
status = { state: "idle", detail: "", connectedAt: null };
|
|
1913
|
+
setStatus(state, detail = "") {
|
|
1914
|
+
this.status = {
|
|
1915
|
+
state,
|
|
1916
|
+
detail,
|
|
1917
|
+
connectedAt: state === "connected" ? this.status.connectedAt ?? Date.now() : null
|
|
1918
|
+
};
|
|
1919
|
+
this.callbacks.onStatus(this.status);
|
|
1920
|
+
}
|
|
1921
|
+
/** 获取飞书官方 REST API Client 实例。 */
|
|
1922
|
+
getClient() {
|
|
1923
|
+
return this.client;
|
|
1924
|
+
}
|
|
1925
|
+
/** 建立长连接。 */
|
|
1926
|
+
start() {
|
|
1927
|
+
if (this.started) return;
|
|
1928
|
+
this.started = true;
|
|
1929
|
+
this.setStatus("connecting", "\u6B63\u5728\u8FDE\u63A5\u98DE\u4E66\u957F\u8FDE\u63A5\u2026");
|
|
1930
|
+
try {
|
|
1931
|
+
const eventDispatcher = new lark.EventDispatcher({}).register({
|
|
1932
|
+
"im.message.receive_v1": async (data) => {
|
|
1933
|
+
try {
|
|
1934
|
+
const msg = data.message;
|
|
1935
|
+
if (!msg || msg.message_type !== "text") return;
|
|
1936
|
+
const parsedContent = JSON.parse(msg.content || "{}");
|
|
1937
|
+
let text = parsedContent.text || "";
|
|
1938
|
+
if (msg.chat_type === "group" && Array.isArray(msg.mentions)) {
|
|
1939
|
+
for (const mention of msg.mentions) {
|
|
1940
|
+
if (mention.key) {
|
|
1941
|
+
text = text.replace(new RegExp(mention.key, "g"), "").trim();
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
if (text.trim() === "") return;
|
|
1946
|
+
this.callbacks.onText(text.trim(), {
|
|
1947
|
+
chatId: msg.chat_id,
|
|
1948
|
+
messageId: msg.message_id,
|
|
1949
|
+
senderId: data.sender?.sender_id?.user_id || data.sender?.sender_id?.open_id || "",
|
|
1950
|
+
chatType: msg.chat_type === "group" ? "group" : "single"
|
|
1951
|
+
});
|
|
1952
|
+
} catch (err) {
|
|
1953
|
+
console.error("[dsh-message-gateway] feishu message parsing error", err);
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
});
|
|
1957
|
+
this.wsClient = new lark.WSClient({
|
|
1958
|
+
appId: this.cred.appId,
|
|
1959
|
+
appSecret: this.cred.appSecret,
|
|
1960
|
+
loggerLevel: lark.LoggerLevel.info
|
|
1961
|
+
});
|
|
1962
|
+
this.wsClient.start({ eventDispatcher });
|
|
1963
|
+
this.setStatus("connected", `App ID: ${this.cred.appId}`);
|
|
1964
|
+
console.log("[dsh-message-gateway] feishu wsClient connection initiated for", this.cred.appId);
|
|
1965
|
+
} catch (error) {
|
|
1966
|
+
console.error("[dsh-message-gateway] feishu start failed", error);
|
|
1967
|
+
this.setStatus("error", error instanceof Error ? error.message : String(error));
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* 回复某条消息(通过 message_id 引用回复)。
|
|
1972
|
+
*/
|
|
1973
|
+
async replyMessage(messageId, content) {
|
|
1974
|
+
if (!this.client) return false;
|
|
1975
|
+
try {
|
|
1976
|
+
await this.client.im.message.reply({
|
|
1977
|
+
path: { message_id: messageId },
|
|
1978
|
+
data: {
|
|
1979
|
+
content: JSON.stringify({ text: content }),
|
|
1980
|
+
msg_type: "text"
|
|
1981
|
+
}
|
|
1982
|
+
});
|
|
1983
|
+
return true;
|
|
1984
|
+
} catch (err) {
|
|
1985
|
+
console.error("[dsh-message-gateway] feishu replyMessage error", err);
|
|
1986
|
+
return false;
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
/**
|
|
1990
|
+
* 主动发送消息至指定会话(支持单聊 open_id/user_id 或群聊 chat_id)。
|
|
1991
|
+
*/
|
|
1992
|
+
async sendMessage(receiveId, content, receiveIdType = "chat_id") {
|
|
1993
|
+
if (!this.client) return false;
|
|
1994
|
+
try {
|
|
1995
|
+
await this.client.im.message.create({
|
|
1996
|
+
params: { receive_id_type: receiveIdType },
|
|
1997
|
+
data: {
|
|
1998
|
+
receive_id: receiveId,
|
|
1999
|
+
msg_type: "text",
|
|
2000
|
+
content: JSON.stringify({ text: content })
|
|
2001
|
+
}
|
|
2002
|
+
});
|
|
2003
|
+
return true;
|
|
2004
|
+
} catch (err) {
|
|
2005
|
+
console.error("[dsh-message-gateway] feishu sendMessage error", err);
|
|
2006
|
+
return false;
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
/** 优雅停止长连接。 */
|
|
2010
|
+
stop() {
|
|
2011
|
+
this.started = false;
|
|
2012
|
+
try {
|
|
2013
|
+
this.wsClient?.close({ force: true });
|
|
2014
|
+
} catch (e) {
|
|
2015
|
+
console.warn("[dsh-message-gateway] feishu wsClient close warning", e);
|
|
2016
|
+
}
|
|
2017
|
+
this.wsClient = null;
|
|
2018
|
+
this.setStatus("idle");
|
|
2019
|
+
}
|
|
2020
|
+
};
|
|
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
|
+
|
|
1898
2126
|
// src/host/bridge-manager.ts
|
|
1899
2127
|
var DEFAULT_MAX_CHAT_AGENTS = 40;
|
|
1900
2128
|
var STREAM_PUSH_INTERVAL = 600;
|
|
@@ -2143,6 +2371,10 @@ var BridgeManager = class {
|
|
|
2143
2371
|
this.qq = null;
|
|
2144
2372
|
this.email?.stop();
|
|
2145
2373
|
this.email = null;
|
|
2374
|
+
this.feishu?.stop();
|
|
2375
|
+
this.feishu = null;
|
|
2376
|
+
this.dingtalk?.stop();
|
|
2377
|
+
this.dingtalk = null;
|
|
2146
2378
|
this.finishAllPending();
|
|
2147
2379
|
const entries = [...this.agents.values()];
|
|
2148
2380
|
this.agents.clear();
|
|
@@ -2408,30 +2640,32 @@ ${secret}`;
|
|
|
2408
2640
|
return r.errcode === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.errmsg ?? "dingtalk send failed" };
|
|
2409
2641
|
}
|
|
2410
2642
|
case "feishu": {
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
};
|
|
2643
|
+
if (this.feishu) {
|
|
2644
|
+
const sent = await this.feishu.sendMessage(target, text, target.startsWith("oc_") ? "chat_id" : target.startsWith("ou_") ? "open_id" : "chat_id");
|
|
2645
|
+
return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "feishu sendMessage failed" };
|
|
2646
|
+
}
|
|
2416
2647
|
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
2417
2648
|
const store = await loadStore2();
|
|
2418
|
-
const
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2649
|
+
const appId = store.platforms.feishu?.appId;
|
|
2650
|
+
const appSecret = store.platforms.feishu?.appSecret;
|
|
2651
|
+
if (appId && appSecret) {
|
|
2652
|
+
try {
|
|
2653
|
+
const lark2 = await import("@larksuiteoapi/node-sdk");
|
|
2654
|
+
const client = new lark2.Client({ appId, appSecret });
|
|
2655
|
+
await client.im.message.create({
|
|
2656
|
+
params: { receive_id_type: target.startsWith("oc_") ? "chat_id" : target.startsWith("ou_") ? "open_id" : "chat_id" },
|
|
2657
|
+
data: {
|
|
2658
|
+
receive_id: target,
|
|
2659
|
+
msg_type: "text",
|
|
2660
|
+
content: JSON.stringify({ text })
|
|
2661
|
+
}
|
|
2662
|
+
});
|
|
2663
|
+
return { ok: true, detail: "sent" };
|
|
2664
|
+
} catch (err) {
|
|
2665
|
+
return { ok: false, detail: `feishu client send failed: ${err instanceof Error ? err.message : String(err)}` };
|
|
2666
|
+
}
|
|
2427
2667
|
}
|
|
2428
|
-
|
|
2429
|
-
method: "POST",
|
|
2430
|
-
headers: { "content-type": "application/json" },
|
|
2431
|
-
body: JSON.stringify(payload)
|
|
2432
|
-
});
|
|
2433
|
-
const r = await resp.json().catch(() => ({}));
|
|
2434
|
-
return r.code === 0 || r.StatusCode === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.msg ?? "feishu send failed" };
|
|
2668
|
+
return { ok: false, detail: "feishu not configured or bridge not started" };
|
|
2435
2669
|
}
|
|
2436
2670
|
case "bark": {
|
|
2437
2671
|
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
@@ -2606,6 +2840,74 @@ ${secret}`;
|
|
|
2606
2840
|
discord = null;
|
|
2607
2841
|
qq = null;
|
|
2608
2842
|
email = null;
|
|
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
|
+
}
|
|
2878
|
+
/** 启动 Feishu 桥(企业自建应用 WebSocket 长连接)。 */
|
|
2879
|
+
startFeishu(cred) {
|
|
2880
|
+
this.feishu?.stop();
|
|
2881
|
+
const bridge = new FeishuBridge(
|
|
2882
|
+
{ appId: cred.appId ?? "", appSecret: cred.appSecret ?? "" },
|
|
2883
|
+
{
|
|
2884
|
+
onStatus: (status) => this.onStatusCallback?.(status),
|
|
2885
|
+
onText: (text, frame) => {
|
|
2886
|
+
const identity = {
|
|
2887
|
+
key: `feishu:${frame.chatId}`,
|
|
2888
|
+
frame,
|
|
2889
|
+
sink: {
|
|
2890
|
+
stream: (_f, _streamId, content, finish) => {
|
|
2891
|
+
if (finish && content.trim()) {
|
|
2892
|
+
void bridge.replyMessage(frame.messageId, content);
|
|
2893
|
+
}
|
|
2894
|
+
},
|
|
2895
|
+
ack: false
|
|
2896
|
+
},
|
|
2897
|
+
chatType: frame.chatType
|
|
2898
|
+
};
|
|
2899
|
+
void this.handleExternalMessage(identity, text);
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
);
|
|
2903
|
+
this.feishu = bridge;
|
|
2904
|
+
bridge.start();
|
|
2905
|
+
}
|
|
2906
|
+
/** 停止 Feishu 桥。 */
|
|
2907
|
+
stopFeishu() {
|
|
2908
|
+
this.feishu?.stop();
|
|
2909
|
+
this.feishu = null;
|
|
2910
|
+
}
|
|
2609
2911
|
/** 启动 Telegram 桥(长轮询)。 */
|
|
2610
2912
|
startTelegram(cred) {
|
|
2611
2913
|
this.telegram?.stop();
|
|
@@ -2676,12 +2978,14 @@ ${secret}`;
|
|
|
2676
2978
|
this.email?.stop();
|
|
2677
2979
|
this.email = null;
|
|
2678
2980
|
}
|
|
2679
|
-
/** 任意桥接平台的状态(telegram/discord/qq/email)。 */
|
|
2981
|
+
/** 任意桥接平台的状态(telegram/discord/qq/email/feishu/dingtalk)。 */
|
|
2680
2982
|
bridgeStatus(id) {
|
|
2681
2983
|
if (id === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2682
2984
|
if (id === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2683
2985
|
if (id === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
2684
2986
|
if (id === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
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 };
|
|
2685
2989
|
return { state: "idle", detail: "", connectedAt: null };
|
|
2686
2990
|
}
|
|
2687
2991
|
/** 将桥状态并入存储状态。 */
|
|
@@ -2755,7 +3059,9 @@ ${secret}`;
|
|
|
2755
3059
|
bridgeLine("Telegram", this.bridgeStatus("telegram")),
|
|
2756
3060
|
bridgeLine("Discord", this.bridgeStatus("discord")),
|
|
2757
3061
|
bridgeLine("QQ", this.bridgeStatus("qq")),
|
|
2758
|
-
bridgeLine("Email", this.bridgeStatus("email"))
|
|
3062
|
+
bridgeLine("Email", this.bridgeStatus("email")),
|
|
3063
|
+
bridgeLine("Feishu", this.bridgeStatus("feishu")),
|
|
3064
|
+
bridgeLine("DingTalk", this.bridgeStatus("dingtalk"))
|
|
2759
3065
|
];
|
|
2760
3066
|
reply(lines.join("\n"));
|
|
2761
3067
|
return true;
|
|
@@ -2913,8 +3219,9 @@ var PLATFORMS = [
|
|
|
2913
3219
|
testable: true,
|
|
2914
3220
|
hintKey: "platform.dingtalk.hint",
|
|
2915
3221
|
fields: [
|
|
2916
|
-
{ key: "
|
|
2917
|
-
{ 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" }
|
|
2918
3225
|
]
|
|
2919
3226
|
},
|
|
2920
3227
|
{
|
|
@@ -2924,8 +3231,8 @@ var PLATFORMS = [
|
|
|
2924
3231
|
testable: true,
|
|
2925
3232
|
hintKey: "platform.feishu.hint",
|
|
2926
3233
|
fields: [
|
|
2927
|
-
{ key: "
|
|
2928
|
-
{ key: "
|
|
3234
|
+
{ key: "appId", labelKey: "field.feishuAppId", placeholderKey: "field.feishuAppId.ph", kind: "text" },
|
|
3235
|
+
{ key: "appSecret", labelKey: "field.feishuAppSecret", placeholderKey: "field.feishuAppSecret.ph", kind: "secret" }
|
|
2929
3236
|
]
|
|
2930
3237
|
},
|
|
2931
3238
|
{
|
|
@@ -3075,35 +3382,40 @@ async function testPlatform(id, cred) {
|
|
|
3075
3382
|
return { ok: false, detail: banner === "" ? `\u65E0\u6CD5\u8FDE\u63A5 ${cred.imapHost}:${port}` : banner };
|
|
3076
3383
|
}
|
|
3077
3384
|
case "dingtalk": {
|
|
3078
|
-
const
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
3083
|
-
const timestamp = Date.now();
|
|
3084
|
-
const stringToSign = `${timestamp}
|
|
3085
|
-
${cred.secret}`;
|
|
3086
|
-
const sign2 = encodeURIComponent(createHmac2("sha256", cred.secret).update(stringToSign, "utf8").digest("base64"));
|
|
3087
|
-
targetUrl += `${rawUrl.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${sign2}`;
|
|
3088
|
-
}
|
|
3089
|
-
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", {
|
|
3090
3389
|
method: "POST",
|
|
3091
3390
|
headers: { "content-type": "application/json" },
|
|
3092
|
-
body: JSON.stringify({
|
|
3093
|
-
msgtype: "markdown",
|
|
3094
|
-
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" }
|
|
3095
|
-
})
|
|
3391
|
+
body: JSON.stringify({ appKey: clientId, appSecret: clientSecret })
|
|
3096
3392
|
});
|
|
3097
3393
|
const r = body;
|
|
3098
|
-
if (r.
|
|
3099
|
-
|
|
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}` };
|
|
3100
3398
|
}
|
|
3101
3399
|
case "feishu": {
|
|
3400
|
+
const appId = cred.appId ?? "";
|
|
3401
|
+
const appSecret = cred.appSecret ?? "";
|
|
3402
|
+
if (appId && appSecret) {
|
|
3403
|
+
const { status: status2, body: body2 } = await httpJson("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", {
|
|
3404
|
+
method: "POST",
|
|
3405
|
+
headers: { "content-type": "application/json" },
|
|
3406
|
+
body: JSON.stringify({ app_id: appId, app_secret: appSecret })
|
|
3407
|
+
});
|
|
3408
|
+
const r2 = body2;
|
|
3409
|
+
if (r2.code === 0 && r2.tenant_access_token) {
|
|
3410
|
+
return { ok: true, detail: "\u98DE\u4E66\u5E94\u7528\u9274\u6743\u6210\u529F\uFF08\u957F\u8FDE\u63A5\u5DF2\u5C31\u7EEA\uFF09" };
|
|
3411
|
+
}
|
|
3412
|
+
return { ok: false, detail: r2.msg ?? `HTTP ${status2}` };
|
|
3413
|
+
}
|
|
3102
3414
|
const rawUrl = cred.webhookUrl ?? "";
|
|
3103
|
-
if (!rawUrl.startsWith("http")) return { ok: false, detail: "\
|
|
3415
|
+
if (!rawUrl.startsWith("http")) return { ok: false, detail: "\u8BF7\u586B\u5199 App ID \u4E0E App Secret" };
|
|
3104
3416
|
const payload = {
|
|
3105
3417
|
msg_type: "text",
|
|
3106
|
-
content: { text: "\u{1F50C} [DSH Message Gateway] \u98DE\u4E66\
|
|
3418
|
+
content: { text: "\u{1F50C} [DSH Message Gateway] \u98DE\u4E66\u8FDE\u63A5\u6D4B\u8BD5\u6210\u529F\uFF01" }
|
|
3107
3419
|
};
|
|
3108
3420
|
if (cred.secret) {
|
|
3109
3421
|
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
@@ -3474,7 +3786,7 @@ async function buildView(manager) {
|
|
|
3474
3786
|
let state = stored?.state ?? (def.id === "wechat" || def.id === "webhooks" ? "manual" : "none");
|
|
3475
3787
|
let detail = stored?.detail ?? "";
|
|
3476
3788
|
let testedAt = stored?.testedAt ?? null;
|
|
3477
|
-
if (def.id === "wecom-aibot" || def.id === "telegram" || def.id === "discord" || def.id === "qq" || def.id === "email") {
|
|
3789
|
+
if (def.id === "wecom-aibot" || def.id === "telegram" || def.id === "discord" || def.id === "qq" || def.id === "email" || def.id === "feishu" || def.id === "dingtalk") {
|
|
3478
3790
|
const merged = manager.mergeStatus(stored, def.id);
|
|
3479
3791
|
state = merged.state;
|
|
3480
3792
|
detail = merged.detail;
|
|
@@ -3823,6 +4135,8 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
3823
4135
|
if (platform === "discord") manager.stopDiscord();
|
|
3824
4136
|
if (platform === "qq") manager.stopQQ();
|
|
3825
4137
|
if (platform === "email") manager.stopEmail();
|
|
4138
|
+
if (platform === "feishu") manager.stopFeishu();
|
|
4139
|
+
if (platform === "dingtalk") manager.stopDingTalk();
|
|
3826
4140
|
json(res, { ok: true, value: await buildView(manager) });
|
|
3827
4141
|
return;
|
|
3828
4142
|
}
|
|
@@ -3865,6 +4179,20 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
3865
4179
|
manager.stopEmail();
|
|
3866
4180
|
}
|
|
3867
4181
|
}
|
|
4182
|
+
if (platform === "feishu") {
|
|
4183
|
+
if (credentials2.appId !== void 0 && credentials2.appId !== "" && credentials2.appSecret !== void 0 && credentials2.appSecret !== "") {
|
|
4184
|
+
manager.startFeishu(credentials2);
|
|
4185
|
+
} else {
|
|
4186
|
+
manager.stopFeishu();
|
|
4187
|
+
}
|
|
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
|
+
}
|
|
3868
4196
|
json(res, { ok: true, value: await buildView(manager) });
|
|
3869
4197
|
return;
|
|
3870
4198
|
}
|
|
@@ -3904,6 +4232,8 @@ var Config = Schema.object({
|
|
|
3904
4232
|
autoStartDiscord: Schema.boolean().default(true).description("\u542F\u52A8\u65F6\u81EA\u52A8\u7528\u5DF2\u4FDD\u5B58\u7684 Discord \u51ED\u636E\u5EFA\u7ACB\u5E38\u9A7B\u8FDE\u63A5"),
|
|
3905
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"),
|
|
3906
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"),
|
|
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"),
|
|
3907
4237
|
groupReply: Schema.boolean().default(true).description("\u662F\u5426\u56DE\u590D\u7FA4\u804A\u6D88\u606F\uFF08false \u65F6\u53EA\u5904\u7406\u5355\u804A\uFF09"),
|
|
3908
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"),
|
|
3909
4239
|
outboundWebhooks: Schema.array(Schema.object({
|
|
@@ -3955,18 +4285,28 @@ function apply(ctx, config = Config({})) {
|
|
|
3955
4285
|
manager.startEmail(email);
|
|
3956
4286
|
console.log("[dsh-message-gateway] email bridge auto-started");
|
|
3957
4287
|
}
|
|
4288
|
+
const feishu = store.platforms.feishu;
|
|
4289
|
+
if (config.autoStartFeishu && feishu !== void 0 && feishu.appId !== "" && feishu.appSecret !== "") {
|
|
4290
|
+
manager.startFeishu(feishu);
|
|
4291
|
+
console.log("[dsh-message-gateway] feishu bridge auto-started");
|
|
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
|
+
}
|
|
3958
4298
|
});
|
|
3959
4299
|
const disposeRoutes = registerGatewayRoutes(ctx, manager);
|
|
3960
4300
|
let disposeTools;
|
|
3961
4301
|
if (ctx.tools) {
|
|
3962
4302
|
const disposeSend = ctx.tools.register(defineTool({
|
|
3963
4303
|
name: "send_chat_message",
|
|
3964
|
-
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",
|
|
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",
|
|
3965
4305
|
parameters: {
|
|
3966
4306
|
platform: {
|
|
3967
4307
|
type: "string",
|
|
3968
4308
|
required: true,
|
|
3969
|
-
description: "\u76EE\u6807\u5E73\u53F0 id\uFF1Atelegram / discord / wecom-aibot / email"
|
|
4309
|
+
description: "\u76EE\u6807\u5E73\u53F0 id\uFF1Atelegram / discord / wecom-aibot / feishu / dingtalk / email"
|
|
3970
4310
|
},
|
|
3971
4311
|
target: {
|
|
3972
4312
|
type: "string",
|