dsh-message-gateway 0.1.15 → 0.1.17
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/README.en.md +34 -21
- package/README.es.md +39 -26
- package/README.md +24 -9
- package/lib/client.js +18 -0
- package/lib/client.js.map +2 -2
- package/lib/index.js +527 -37
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
+
}) : x)(function(x) {
|
|
6
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
9
|
+
var __esm = (fn, res) => function __init() {
|
|
10
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/host/gateway-store.ts
|
|
18
|
+
var gateway_store_exports = {};
|
|
19
|
+
__export(gateway_store_exports, {
|
|
20
|
+
loadStore: () => loadStore,
|
|
21
|
+
saveStore: () => saveStore
|
|
22
|
+
});
|
|
23
|
+
import { chmod, mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
24
|
+
import { homedir } from "node:os";
|
|
25
|
+
import { join } from "node:path";
|
|
26
|
+
async function loadStore() {
|
|
27
|
+
try {
|
|
28
|
+
const text = await readFile(FILE, "utf8");
|
|
29
|
+
const parsed = JSON.parse(text);
|
|
30
|
+
return {
|
|
31
|
+
platforms: parsed.platforms ?? {},
|
|
32
|
+
statuses: parsed.statuses ?? {}
|
|
33
|
+
};
|
|
34
|
+
} catch {
|
|
35
|
+
return { platforms: {}, statuses: {} };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function saveStore(data) {
|
|
39
|
+
await mkdir(join(homedir(), ".dsh"), { recursive: true });
|
|
40
|
+
const tmp = `${FILE}.tmp`;
|
|
41
|
+
await writeFile(tmp, JSON.stringify(data, null, 2), { mode: 384 });
|
|
42
|
+
await chmod(tmp, 384);
|
|
43
|
+
await rename(tmp, FILE);
|
|
44
|
+
}
|
|
45
|
+
var FILE;
|
|
46
|
+
var init_gateway_store = __esm({
|
|
47
|
+
"src/host/gateway-store.ts"() {
|
|
48
|
+
"use strict";
|
|
49
|
+
FILE = join(homedir(), ".dsh", "gateway.json");
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
1
53
|
// node_modules/@deepseek-ai/dsh-tools/lib/index.js
|
|
2
54
|
import { Service } from "@deepseek-ai/cordis";
|
|
3
55
|
import z from "@deepseek-ai/schemastery";
|
|
@@ -3224,6 +3276,27 @@ var WecomBridge = class {
|
|
|
3224
3276
|
return false;
|
|
3225
3277
|
}
|
|
3226
3278
|
}
|
|
3279
|
+
/**
|
|
3280
|
+
* 主动向会话发送图片消息(chatid:单聊=userid,群聊=群 ID)。
|
|
3281
|
+
* 内部自动上传临时素材(WeCom uploadMedia)后通过 aibot_send_msg 发送。
|
|
3282
|
+
* @param chatid 接收会话 ID
|
|
3283
|
+
* @param image 图片数据(Buffer)
|
|
3284
|
+
* @param filename 可选文件名(默认 image.png)
|
|
3285
|
+
*/
|
|
3286
|
+
async sendImage(chatid, image, filename = "image.png") {
|
|
3287
|
+
try {
|
|
3288
|
+
const media = await this.client?.uploadMedia(image, { type: "image", filename });
|
|
3289
|
+
if (!media?.media_id) {
|
|
3290
|
+
console.error("[dsh-message-gateway] sendImage upload failed: no media_id returned");
|
|
3291
|
+
return false;
|
|
3292
|
+
}
|
|
3293
|
+
await this.client?.sendMediaMessage(chatid, "image", media.media_id);
|
|
3294
|
+
return true;
|
|
3295
|
+
} catch (error) {
|
|
3296
|
+
console.error("[dsh-message-gateway] sendImage failed", error);
|
|
3297
|
+
return false;
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3227
3300
|
/** 回复欢迎语(enter_chat 事件)。 */
|
|
3228
3301
|
async welcome(frame, content) {
|
|
3229
3302
|
try {
|
|
@@ -3373,6 +3446,38 @@ var TelegramBridge = class {
|
|
|
3373
3446
|
const result = await this.sendText(chatId, content);
|
|
3374
3447
|
return result !== null;
|
|
3375
3448
|
}
|
|
3449
|
+
/**
|
|
3450
|
+
* 主动推送图片:向指定 chat 发送照片(支持 Buffer 或图片 URL)。
|
|
3451
|
+
* @param chatId 目标聊天 ID
|
|
3452
|
+
* @param photo 图片数据 Buffer 或公网图片 URL
|
|
3453
|
+
* @param caption 可选图片附带文字说明
|
|
3454
|
+
* @param filename 可选文件名
|
|
3455
|
+
*/
|
|
3456
|
+
async sendPhoto(chatId, photo, caption, filename = "photo.png") {
|
|
3457
|
+
try {
|
|
3458
|
+
if (typeof photo === "string") {
|
|
3459
|
+
const body = { chat_id: chatId, photo };
|
|
3460
|
+
if (caption) body.caption = caption.slice(0, 1024);
|
|
3461
|
+
const result = await this.api("sendPhoto", body);
|
|
3462
|
+
return result?.message_id !== void 0;
|
|
3463
|
+
}
|
|
3464
|
+
const form = new FormData();
|
|
3465
|
+
form.append("chat_id", String(chatId));
|
|
3466
|
+
form.append("photo", new Blob([new Uint8Array(photo)]), filename);
|
|
3467
|
+
if (caption) form.append("caption", caption.slice(0, 1024));
|
|
3468
|
+
const response = await fetch(`https://api.telegram.org/bot${this.token}/sendPhoto`, {
|
|
3469
|
+
method: "POST",
|
|
3470
|
+
body: form,
|
|
3471
|
+
signal: AbortSignal.timeout(3e4)
|
|
3472
|
+
});
|
|
3473
|
+
if (!response.ok) return false;
|
|
3474
|
+
const data = await response.json();
|
|
3475
|
+
return data.ok === true;
|
|
3476
|
+
} catch (error) {
|
|
3477
|
+
console.error("[dsh-message-gateway] telegram sendPhoto failed", error);
|
|
3478
|
+
return false;
|
|
3479
|
+
}
|
|
3480
|
+
}
|
|
3376
3481
|
/** 发送文本(Markdown 失败自动降级纯文本)。 */
|
|
3377
3482
|
async sendText(chatId, text) {
|
|
3378
3483
|
const body = { chat_id: chatId, text: text.slice(0, MSG_LIMIT), parse_mode: "Markdown", disable_web_page_preview: true };
|
|
@@ -3596,6 +3701,34 @@ var DiscordBridge = class {
|
|
|
3596
3701
|
return false;
|
|
3597
3702
|
}
|
|
3598
3703
|
}
|
|
3704
|
+
/**
|
|
3705
|
+
* 主动推送图片:向指定频道发送图片附件。
|
|
3706
|
+
* @param channelId 目标频道 ID
|
|
3707
|
+
* @param image 图片数据 Buffer
|
|
3708
|
+
* @param content 可选文字说明
|
|
3709
|
+
* @param filename 可选文件名
|
|
3710
|
+
*/
|
|
3711
|
+
async sendImage(channelId, image, content, filename = "image.png") {
|
|
3712
|
+
try {
|
|
3713
|
+
const form = new FormData();
|
|
3714
|
+
form.append("files[0]", new Blob([new Uint8Array(image)]), filename);
|
|
3715
|
+
if (content) {
|
|
3716
|
+
form.append("payload_json", JSON.stringify({ content: content.slice(0, MSG_LIMIT2) }));
|
|
3717
|
+
}
|
|
3718
|
+
const response = await fetch(`${REST}/channels/${encodeURIComponent(channelId)}/messages`, {
|
|
3719
|
+
method: "POST",
|
|
3720
|
+
headers: { authorization: `Bot ${this.token}` },
|
|
3721
|
+
body: form,
|
|
3722
|
+
signal: AbortSignal.timeout(3e4)
|
|
3723
|
+
});
|
|
3724
|
+
if (!response.ok) return false;
|
|
3725
|
+
const data = await response.json();
|
|
3726
|
+
return data.id !== void 0;
|
|
3727
|
+
} catch (error) {
|
|
3728
|
+
console.error("[dsh-message-gateway] discord sendImage failed", error);
|
|
3729
|
+
return false;
|
|
3730
|
+
}
|
|
3731
|
+
}
|
|
3599
3732
|
/** 停止网关连接。 */
|
|
3600
3733
|
stop() {
|
|
3601
3734
|
this.stopped = true;
|
|
@@ -4985,6 +5118,16 @@ var BridgeManager = class {
|
|
|
4985
5118
|
const userid = body.from?.userid ?? "";
|
|
4986
5119
|
const chatid = body.chatid ?? "";
|
|
4987
5120
|
const key = chatid !== "" ? `wecom:group:${chatid}` : `wecom:user:${userid}`;
|
|
5121
|
+
try {
|
|
5122
|
+
const fs = __require("node:fs");
|
|
5123
|
+
const path = __require("node:path");
|
|
5124
|
+
const os = __require("node:os");
|
|
5125
|
+
const target = chatid !== "" ? chatid : userid;
|
|
5126
|
+
if (target) {
|
|
5127
|
+
fs.writeFileSync(path.join(os.homedir(), ".dsh", "latest_wecom_target.txt"), target, "utf8");
|
|
5128
|
+
}
|
|
5129
|
+
} catch {
|
|
5130
|
+
}
|
|
4988
5131
|
const sink = {
|
|
4989
5132
|
stream: (f, sid, content, finish) => void this.wecom?.streamReply(f, sid, content, finish)
|
|
4990
5133
|
// 企业微信走单一流式消息(ack → 内容 → 定稿同一条消息就地更新);
|
|
@@ -5057,12 +5200,201 @@ ${content}` : content;
|
|
|
5057
5200
|
const sent = await this.email.send(target, subject, content);
|
|
5058
5201
|
return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "email send failed" };
|
|
5059
5202
|
}
|
|
5203
|
+
case "dingtalk": {
|
|
5204
|
+
const rawUrl = target.startsWith("http") ? target : `https://oapi.dingtalk.com/robot/send?access_token=${encodeURIComponent(target)}`;
|
|
5205
|
+
let targetUrl = rawUrl;
|
|
5206
|
+
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
5207
|
+
const store = await loadStore2();
|
|
5208
|
+
const secret = store.platforms.dingtalk?.secret;
|
|
5209
|
+
if (secret) {
|
|
5210
|
+
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
5211
|
+
const timestamp = Date.now();
|
|
5212
|
+
const stringToSign = `${timestamp}
|
|
5213
|
+
${secret}`;
|
|
5214
|
+
const sign2 = encodeURIComponent(createHmac2("sha256", secret).update(stringToSign, "utf8").digest("base64"));
|
|
5215
|
+
targetUrl += `${rawUrl.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${sign2}`;
|
|
5216
|
+
}
|
|
5217
|
+
const resp = await fetch(targetUrl, {
|
|
5218
|
+
method: "POST",
|
|
5219
|
+
headers: { "content-type": "application/json" },
|
|
5220
|
+
body: JSON.stringify({
|
|
5221
|
+
msgtype: "markdown",
|
|
5222
|
+
markdown: { title: opts.title ?? "DSH \u901A\u77E5", text }
|
|
5223
|
+
})
|
|
5224
|
+
});
|
|
5225
|
+
const r = await resp.json().catch(() => ({}));
|
|
5226
|
+
return r.errcode === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.errmsg ?? "dingtalk send failed" };
|
|
5227
|
+
}
|
|
5228
|
+
case "feishu": {
|
|
5229
|
+
const rawUrl = target.startsWith("http") ? target : `https://open.feishu.cn/open-apis/bot/v2/hook/${encodeURIComponent(target)}`;
|
|
5230
|
+
const payload = {
|
|
5231
|
+
msg_type: "text",
|
|
5232
|
+
content: { text }
|
|
5233
|
+
};
|
|
5234
|
+
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
5235
|
+
const store = await loadStore2();
|
|
5236
|
+
const secret = store.platforms.feishu?.secret;
|
|
5237
|
+
if (secret) {
|
|
5238
|
+
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
5239
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
5240
|
+
const stringToSign = `${timestamp}
|
|
5241
|
+
${secret}`;
|
|
5242
|
+
const sign2 = createHmac2("sha256", stringToSign).digest("base64");
|
|
5243
|
+
payload.timestamp = String(timestamp);
|
|
5244
|
+
payload.sign = sign2;
|
|
5245
|
+
}
|
|
5246
|
+
const resp = await fetch(rawUrl, {
|
|
5247
|
+
method: "POST",
|
|
5248
|
+
headers: { "content-type": "application/json" },
|
|
5249
|
+
body: JSON.stringify(payload)
|
|
5250
|
+
});
|
|
5251
|
+
const r = await resp.json().catch(() => ({}));
|
|
5252
|
+
return r.code === 0 || r.StatusCode === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.msg ?? "feishu send failed" };
|
|
5253
|
+
}
|
|
5254
|
+
case "bark": {
|
|
5255
|
+
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
5256
|
+
const store = await loadStore2();
|
|
5257
|
+
const server = (store.platforms.bark?.serverUrl || "https://api.day.app").replace(/\/+$/, "");
|
|
5258
|
+
const deviceKey = target || store.platforms.bark?.deviceKey;
|
|
5259
|
+
if (!deviceKey) return { ok: false, detail: "missing bark device key" };
|
|
5260
|
+
const url = `${server}/${encodeURIComponent(deviceKey)}/`;
|
|
5261
|
+
const resp = await fetch(url, {
|
|
5262
|
+
method: "POST",
|
|
5263
|
+
headers: { "content-type": "application/json" },
|
|
5264
|
+
body: JSON.stringify({ title: opts.title ?? "DSH \u901A\u77E5", body: content, group: "DSH" })
|
|
5265
|
+
});
|
|
5266
|
+
const r = await resp.json().catch(() => ({}));
|
|
5267
|
+
return r.code === 200 || resp.status === 200 ? { ok: true, detail: "sent" } : { ok: false, detail: r.message ?? "bark send failed" };
|
|
5268
|
+
}
|
|
5269
|
+
case "serverchan": {
|
|
5270
|
+
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
5271
|
+
const store = await loadStore2();
|
|
5272
|
+
const sendKey = target || store.platforms.serverchan?.sendKey;
|
|
5273
|
+
if (!sendKey) return { ok: false, detail: "missing serverchan sendkey" };
|
|
5274
|
+
const url = `https://sctapi.ftqq.com/${encodeURIComponent(sendKey)}.send`;
|
|
5275
|
+
const resp = await fetch(url, {
|
|
5276
|
+
method: "POST",
|
|
5277
|
+
headers: { "content-type": "application/json" },
|
|
5278
|
+
body: JSON.stringify({ title: (opts.title ?? "DSH \u901A\u77E5").slice(0, 32), desp: content })
|
|
5279
|
+
});
|
|
5280
|
+
const r = await resp.json().catch(() => ({}));
|
|
5281
|
+
return r.code === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.message ?? "serverchan send failed" };
|
|
5282
|
+
}
|
|
5060
5283
|
case "qq":
|
|
5061
5284
|
return { ok: false, detail: "QQ \u5E73\u53F0\u81EA 2025-04-21 \u8D77\u4E0D\u652F\u6301\u4E3B\u52A8\u63A8\u9001\uFF08\u4EC5\u88AB\u52A8\u56DE\u590D\uFF09\uFF0C\u65E0\u6CD5\u53D1\u9001\u4E3B\u52A8\u6D88\u606F" };
|
|
5062
5285
|
default:
|
|
5063
5286
|
return { ok: false, detail: `platform "${platform}" does not support push` };
|
|
5064
5287
|
}
|
|
5065
5288
|
}
|
|
5289
|
+
/**
|
|
5290
|
+
* 主动推送图片:向指定平台的目标会话发送图片。
|
|
5291
|
+
* - wecom-aibot: 自动上传临时素材并发送 image 消息
|
|
5292
|
+
* - telegram: 调用 sendPhoto(支持 Buffer 上传或图片 URL)
|
|
5293
|
+
* - discord: multipart 上传 files[0] 附件
|
|
5294
|
+
* - bark: 支持图片 URL 传入(通过 image 字段富文本横幅展示)
|
|
5295
|
+
* - dingtalk: 支持图片 URL(在 Markdown 中嵌入 ![]())
|
|
5296
|
+
* - serverchan: 支持图片 URL(在 Markdown desp 中嵌入 ![]())
|
|
5297
|
+
* - feishu / qq / email / 其它: 对不支持的二进制上传返回明确的官方协议限制原因
|
|
5298
|
+
*
|
|
5299
|
+
* @param platform 平台 id
|
|
5300
|
+
* @param target 目标(wecom-aibot=userid/群ID;telegram=chatId;discord=channelId;bark=deviceKey 等)
|
|
5301
|
+
* @param image 图片数据 Buffer 或可公网访问的图片 URL 字符串
|
|
5302
|
+
* @param opts.caption 可选文字说明
|
|
5303
|
+
* @param opts.filename 可选文件名(默认 image.png)
|
|
5304
|
+
*/
|
|
5305
|
+
async pushImage(platform, target, image, opts = {}) {
|
|
5306
|
+
const filename = opts.filename ?? "image.png";
|
|
5307
|
+
const isUrl = typeof image === "string" && /^https?:\/\//i.test(image.trim());
|
|
5308
|
+
switch (platform) {
|
|
5309
|
+
case "wecom-aibot": {
|
|
5310
|
+
if (this.wecom === null) return { ok: false, detail: "wecom-aibot bridge not connected" };
|
|
5311
|
+
if (typeof image === "string") {
|
|
5312
|
+
try {
|
|
5313
|
+
const resp = await fetch(image, { signal: AbortSignal.timeout(2e4) });
|
|
5314
|
+
if (!resp.ok) return { ok: false, detail: `failed to fetch image from url: HTTP ${resp.status}` };
|
|
5315
|
+
image = Buffer.from(await resp.arrayBuffer());
|
|
5316
|
+
} catch (err) {
|
|
5317
|
+
return { ok: false, detail: `fetch image url error: ${err instanceof Error ? err.message : String(err)}` };
|
|
5318
|
+
}
|
|
5319
|
+
}
|
|
5320
|
+
const sent = await this.wecom.sendImage(target, image, filename);
|
|
5321
|
+
return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "wecom image send failed" };
|
|
5322
|
+
}
|
|
5323
|
+
case "telegram": {
|
|
5324
|
+
if (this.telegram === null) return { ok: false, detail: "telegram bridge not connected" };
|
|
5325
|
+
const chatId = Number(target);
|
|
5326
|
+
if (!Number.isInteger(chatId) || chatId <= 0) return { ok: false, detail: "telegram target must be a numeric chatId" };
|
|
5327
|
+
const sent = await this.telegram.sendPhoto(chatId, image, opts.caption, filename);
|
|
5328
|
+
return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "telegram photo send failed" };
|
|
5329
|
+
}
|
|
5330
|
+
case "discord": {
|
|
5331
|
+
if (this.discord === null) return { ok: false, detail: "discord bridge not connected" };
|
|
5332
|
+
let buf;
|
|
5333
|
+
if (typeof image === "string") {
|
|
5334
|
+
try {
|
|
5335
|
+
const resp = await fetch(image, { signal: AbortSignal.timeout(2e4) });
|
|
5336
|
+
if (!resp.ok) return { ok: false, detail: `failed to fetch image from url: HTTP ${resp.status}` };
|
|
5337
|
+
buf = Buffer.from(await resp.arrayBuffer());
|
|
5338
|
+
} catch (err) {
|
|
5339
|
+
return { ok: false, detail: `fetch image url error: ${err instanceof Error ? err.message : String(err)}` };
|
|
5340
|
+
}
|
|
5341
|
+
} else {
|
|
5342
|
+
buf = image;
|
|
5343
|
+
}
|
|
5344
|
+
const sent = await this.discord.sendImage(target, buf, opts.caption, filename);
|
|
5345
|
+
return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "discord image send failed" };
|
|
5346
|
+
}
|
|
5347
|
+
case "bark": {
|
|
5348
|
+
if (!isUrl) {
|
|
5349
|
+
return { ok: false, detail: "Bark only supports public image URLs (pass image as http/https URL string)" };
|
|
5350
|
+
}
|
|
5351
|
+
const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
|
|
5352
|
+
const store = await loadStore2();
|
|
5353
|
+
const server = (store.platforms.bark?.serverUrl || "https://api.day.app").replace(/\/+$/, "");
|
|
5354
|
+
const deviceKey = target || store.platforms.bark?.deviceKey;
|
|
5355
|
+
if (!deviceKey) return { ok: false, detail: "missing bark device key" };
|
|
5356
|
+
const url = `${server}/${encodeURIComponent(deviceKey)}/`;
|
|
5357
|
+
const resp = await fetch(url, {
|
|
5358
|
+
method: "POST",
|
|
5359
|
+
headers: { "content-type": "application/json" },
|
|
5360
|
+
body: JSON.stringify({
|
|
5361
|
+
title: opts.caption ?? "DSH \u901A\u77E5",
|
|
5362
|
+
body: opts.caption ? "" : "\u6536\u5230\u4E00\u5F20\u56FE\u7247",
|
|
5363
|
+
image,
|
|
5364
|
+
group: "DSH"
|
|
5365
|
+
})
|
|
5366
|
+
});
|
|
5367
|
+
const r = await resp.json().catch(() => ({}));
|
|
5368
|
+
return r.code === 200 || resp.status === 200 ? { ok: true, detail: "sent" } : { ok: false, detail: r.message ?? "bark send failed" };
|
|
5369
|
+
}
|
|
5370
|
+
case "dingtalk": {
|
|
5371
|
+
if (!isUrl) {
|
|
5372
|
+
return { ok: false, detail: "DingTalk custom robot only supports public image URLs via Markdown image tag" };
|
|
5373
|
+
}
|
|
5374
|
+
const caption = opts.caption ?? "\u56FE\u7247";
|
|
5375
|
+
const mdText = `${caption ? `${caption}
|
|
5376
|
+
|
|
5377
|
+
` : ""}`;
|
|
5378
|
+
return this.pushMessage("dingtalk", target, mdText, { title: opts.caption ?? "\u56FE\u7247" });
|
|
5379
|
+
}
|
|
5380
|
+
case "serverchan": {
|
|
5381
|
+
if (!isUrl) {
|
|
5382
|
+
return { ok: false, detail: "ServerChan only supports public image URLs via Markdown desp" };
|
|
5383
|
+
}
|
|
5384
|
+
const caption = opts.caption ?? "\u56FE\u7247";
|
|
5385
|
+
const mdText = `${caption ? `${caption}
|
|
5386
|
+
|
|
5387
|
+
` : ""}`;
|
|
5388
|
+
return this.pushMessage("serverchan", target, mdText, { title: opts.caption ?? "\u56FE\u7247" });
|
|
5389
|
+
}
|
|
5390
|
+
case "feishu":
|
|
5391
|
+
return { ok: false, detail: "Feishu custom robot webhook does not support binary image upload without tenant_access_token (im/v1/images)" };
|
|
5392
|
+
case "qq":
|
|
5393
|
+
return { ok: false, detail: "QQ platform has discontinued active push API since 2025-04-21" };
|
|
5394
|
+
default:
|
|
5395
|
+
return { ok: false, detail: `platform "${platform}" does not support image push` };
|
|
5396
|
+
}
|
|
5397
|
+
}
|
|
5066
5398
|
/**
|
|
5067
5399
|
* 向已配置的 Outbound Webhooks 广播事件(异步投递,失败不阻塞)。
|
|
5068
5400
|
*/
|
|
@@ -5279,32 +5611,11 @@ function nowInShanghai() {
|
|
|
5279
5611
|
}).format(/* @__PURE__ */ new Date());
|
|
5280
5612
|
}
|
|
5281
5613
|
|
|
5282
|
-
// src/
|
|
5283
|
-
|
|
5284
|
-
import { homedir } from "node:os";
|
|
5285
|
-
import { join } from "node:path";
|
|
5286
|
-
var FILE = join(homedir(), ".dsh", "gateway.json");
|
|
5287
|
-
async function loadStore() {
|
|
5288
|
-
try {
|
|
5289
|
-
const text = await readFile(FILE, "utf8");
|
|
5290
|
-
const parsed = JSON.parse(text);
|
|
5291
|
-
return {
|
|
5292
|
-
platforms: parsed.platforms ?? {},
|
|
5293
|
-
statuses: parsed.statuses ?? {}
|
|
5294
|
-
};
|
|
5295
|
-
} catch {
|
|
5296
|
-
return { platforms: {}, statuses: {} };
|
|
5297
|
-
}
|
|
5298
|
-
}
|
|
5299
|
-
async function saveStore(data) {
|
|
5300
|
-
await mkdir(join(homedir(), ".dsh"), { recursive: true });
|
|
5301
|
-
const tmp = `${FILE}.tmp`;
|
|
5302
|
-
await writeFile(tmp, JSON.stringify(data, null, 2), { mode: 384 });
|
|
5303
|
-
await chmod(tmp, 384);
|
|
5304
|
-
await rename(tmp, FILE);
|
|
5305
|
-
}
|
|
5614
|
+
// src/index.ts
|
|
5615
|
+
init_gateway_store();
|
|
5306
5616
|
|
|
5307
5617
|
// src/host/routes.ts
|
|
5618
|
+
init_gateway_store();
|
|
5308
5619
|
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
5309
5620
|
|
|
5310
5621
|
// src/host/platforms.ts
|
|
@@ -5413,6 +5724,49 @@ var PLATFORMS = [
|
|
|
5413
5724
|
{ key: "smtpPass", labelKey: "field.smtpPass", placeholderKey: "field.secret.ph", kind: "secret" }
|
|
5414
5725
|
]
|
|
5415
5726
|
},
|
|
5727
|
+
{
|
|
5728
|
+
id: "dingtalk",
|
|
5729
|
+
nameKey: "platform.dingtalk",
|
|
5730
|
+
icon: "\u{1F4CC}",
|
|
5731
|
+
testable: true,
|
|
5732
|
+
hintKey: "platform.dingtalk.hint",
|
|
5733
|
+
fields: [
|
|
5734
|
+
{ key: "webhookUrl", labelKey: "field.webhookUrl", placeholderKey: "field.dingtalkWebhook.ph", kind: "text" },
|
|
5735
|
+
{ key: "secret", labelKey: "field.dingtalkSecret", placeholderKey: "field.secret.ph", kind: "secret" }
|
|
5736
|
+
]
|
|
5737
|
+
},
|
|
5738
|
+
{
|
|
5739
|
+
id: "feishu",
|
|
5740
|
+
nameKey: "platform.feishu",
|
|
5741
|
+
icon: "\u{1F54A}\uFE0F",
|
|
5742
|
+
testable: true,
|
|
5743
|
+
hintKey: "platform.feishu.hint",
|
|
5744
|
+
fields: [
|
|
5745
|
+
{ key: "webhookUrl", labelKey: "field.webhookUrl", placeholderKey: "field.feishuWebhook.ph", kind: "text" },
|
|
5746
|
+
{ key: "secret", labelKey: "field.feishuSecret", placeholderKey: "field.secret.ph", kind: "secret" }
|
|
5747
|
+
]
|
|
5748
|
+
},
|
|
5749
|
+
{
|
|
5750
|
+
id: "bark",
|
|
5751
|
+
nameKey: "platform.bark",
|
|
5752
|
+
icon: "\u{1F514}",
|
|
5753
|
+
testable: true,
|
|
5754
|
+
hintKey: "platform.bark.hint",
|
|
5755
|
+
fields: [
|
|
5756
|
+
{ key: "serverUrl", labelKey: "field.barkServer", placeholderKey: "field.barkServer.ph", kind: "text" },
|
|
5757
|
+
{ key: "deviceKey", labelKey: "field.barkKey", placeholderKey: "field.barkKey.ph", kind: "secret" }
|
|
5758
|
+
]
|
|
5759
|
+
},
|
|
5760
|
+
{
|
|
5761
|
+
id: "serverchan",
|
|
5762
|
+
nameKey: "platform.serverchan",
|
|
5763
|
+
icon: "\u{1F680}",
|
|
5764
|
+
testable: true,
|
|
5765
|
+
hintKey: "platform.serverchan.hint",
|
|
5766
|
+
fields: [
|
|
5767
|
+
{ key: "sendKey", labelKey: "field.serverchanKey", placeholderKey: "field.serverchanKey.ph", kind: "secret" }
|
|
5768
|
+
]
|
|
5769
|
+
},
|
|
5416
5770
|
{
|
|
5417
5771
|
id: "webhooks",
|
|
5418
5772
|
nameKey: "platform.webhooks",
|
|
@@ -5538,6 +5892,82 @@ async function testPlatform(id, cred) {
|
|
|
5538
5892
|
}
|
|
5539
5893
|
return { ok: false, detail: banner === "" ? `\u65E0\u6CD5\u8FDE\u63A5 ${cred.imapHost}:${port}` : banner };
|
|
5540
5894
|
}
|
|
5895
|
+
case "dingtalk": {
|
|
5896
|
+
const rawUrl = cred.webhookUrl ?? "";
|
|
5897
|
+
if (!rawUrl.startsWith("http")) return { ok: false, detail: "\u65E0\u6548\u7684 Webhook URL" };
|
|
5898
|
+
let targetUrl = rawUrl;
|
|
5899
|
+
if (cred.secret) {
|
|
5900
|
+
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
5901
|
+
const timestamp = Date.now();
|
|
5902
|
+
const stringToSign = `${timestamp}
|
|
5903
|
+
${cred.secret}`;
|
|
5904
|
+
const sign2 = encodeURIComponent(createHmac2("sha256", cred.secret).update(stringToSign, "utf8").digest("base64"));
|
|
5905
|
+
targetUrl += `${rawUrl.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${sign2}`;
|
|
5906
|
+
}
|
|
5907
|
+
const { status, body } = await httpJson(targetUrl, {
|
|
5908
|
+
method: "POST",
|
|
5909
|
+
headers: { "content-type": "application/json" },
|
|
5910
|
+
body: JSON.stringify({
|
|
5911
|
+
msgtype: "markdown",
|
|
5912
|
+
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" }
|
|
5913
|
+
})
|
|
5914
|
+
});
|
|
5915
|
+
const r = body;
|
|
5916
|
+
if (r.errcode === 0) return { ok: true, detail: "\u9489\u9489\u673A\u5668\u4EBA\u8FDE\u63A5\u6210\u529F" };
|
|
5917
|
+
return { ok: false, detail: r.errmsg ?? `HTTP ${status}` };
|
|
5918
|
+
}
|
|
5919
|
+
case "feishu": {
|
|
5920
|
+
const rawUrl = cred.webhookUrl ?? "";
|
|
5921
|
+
if (!rawUrl.startsWith("http")) return { ok: false, detail: "\u65E0\u6548\u7684 Webhook URL" };
|
|
5922
|
+
const payload = {
|
|
5923
|
+
msg_type: "text",
|
|
5924
|
+
content: { text: "\u{1F50C} [DSH Message Gateway] \u98DE\u4E66\u81EA\u5B9A\u4E49\u673A\u5668\u4EBA\u8FDE\u63A5\u6D4B\u8BD5\u6210\u529F\uFF01" }
|
|
5925
|
+
};
|
|
5926
|
+
if (cred.secret) {
|
|
5927
|
+
const { createHmac: createHmac2 } = await import("node:crypto");
|
|
5928
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
5929
|
+
const stringToSign = `${timestamp}
|
|
5930
|
+
${cred.secret}`;
|
|
5931
|
+
const sign2 = createHmac2("sha256", stringToSign).digest("base64");
|
|
5932
|
+
payload.timestamp = String(timestamp);
|
|
5933
|
+
payload.sign = sign2;
|
|
5934
|
+
}
|
|
5935
|
+
const { status, body } = await httpJson(rawUrl, {
|
|
5936
|
+
method: "POST",
|
|
5937
|
+
headers: { "content-type": "application/json" },
|
|
5938
|
+
body: JSON.stringify(payload)
|
|
5939
|
+
});
|
|
5940
|
+
const r = body;
|
|
5941
|
+
if (r.code === 0 || r.StatusCode === 0) return { ok: true, detail: "\u98DE\u4E66\u673A\u5668\u4EBA\u8FDE\u63A5\u6210\u529F" };
|
|
5942
|
+
return { ok: false, detail: r.msg ?? `HTTP ${status}` };
|
|
5943
|
+
}
|
|
5944
|
+
case "bark": {
|
|
5945
|
+
const server = (cred.serverUrl || "https://api.day.app").replace(/\/+$/, "");
|
|
5946
|
+
const key = cred.deviceKey ?? "";
|
|
5947
|
+
if (!key) return { ok: false, detail: "\u8BF7\u586B\u5199 Device Key" };
|
|
5948
|
+
const url = `${server}/${encodeURIComponent(key)}/`;
|
|
5949
|
+
const { status, body } = await httpJson(url, {
|
|
5950
|
+
method: "POST",
|
|
5951
|
+
headers: { "content-type": "application/json" },
|
|
5952
|
+
body: JSON.stringify({ title: "DSH \u8FDE\u63A5\u6D4B\u8BD5", body: "Bark \u624B\u673A\u63A8\u9001\u901A\u9053\u5DF2\u8FDE\u901A", group: "DSH" })
|
|
5953
|
+
});
|
|
5954
|
+
const r = body;
|
|
5955
|
+
if (r.code === 200 || status === 200) return { ok: true, detail: "Bark \u63A8\u9001\u6D4B\u8BD5\u6210\u529F" };
|
|
5956
|
+
return { ok: false, detail: r.message ?? `HTTP ${status}` };
|
|
5957
|
+
}
|
|
5958
|
+
case "serverchan": {
|
|
5959
|
+
const key = cred.sendKey ?? "";
|
|
5960
|
+
if (!key) return { ok: false, detail: "\u8BF7\u586B\u5199 SendKey" };
|
|
5961
|
+
const url = `https://sctapi.ftqq.com/${encodeURIComponent(key)}.send`;
|
|
5962
|
+
const { status, body } = await httpJson(url, {
|
|
5963
|
+
method: "POST",
|
|
5964
|
+
headers: { "content-type": "application/json" },
|
|
5965
|
+
body: JSON.stringify({ title: "DSH\u8FDE\u63A5\u6D4B\u8BD5", desp: "Server\u9171\u6D88\u606F\u901A\u9053\u6D4B\u8BD5\u6210\u529F" })
|
|
5966
|
+
});
|
|
5967
|
+
const r = body;
|
|
5968
|
+
if (r.code === 0) return { ok: true, detail: "Server\u9171\u63A8\u9001\u6D4B\u8BD5\u6210\u529F" };
|
|
5969
|
+
return { ok: false, detail: r.message ?? `HTTP ${status}` };
|
|
5970
|
+
}
|
|
5541
5971
|
default:
|
|
5542
5972
|
return { ok: true, detail: "configured" };
|
|
5543
5973
|
}
|
|
@@ -5812,7 +6242,7 @@ function callbackIdentity(key, send) {
|
|
|
5812
6242
|
}
|
|
5813
6243
|
|
|
5814
6244
|
// src/host/routes.ts
|
|
5815
|
-
var BODY_CAP_BYTES =
|
|
6245
|
+
var BODY_CAP_BYTES = 8 << 20;
|
|
5816
6246
|
var CALLBACK_PATHS = {
|
|
5817
6247
|
wecom: "/gateway/wecom/callback",
|
|
5818
6248
|
"wechat-mp": "/gateway/wechat-mp/callback",
|
|
@@ -6137,18 +6567,57 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
6137
6567
|
const target = typeof body?.target === "string" ? body.target.trim() : "";
|
|
6138
6568
|
const content = typeof body?.content === "string" ? body.content.trim() : "";
|
|
6139
6569
|
const title = typeof body?.title === "string" ? body.title.trim() : "";
|
|
6140
|
-
|
|
6141
|
-
|
|
6570
|
+
const imageB64 = typeof body?.image === "string" ? body.image.trim() : "";
|
|
6571
|
+
const filename = typeof body?.filename === "string" && body.filename.trim() !== "" ? body.filename.trim() : void 0;
|
|
6572
|
+
if (platform === "" || target === "" || content === "" && imageB64 === "") {
|
|
6573
|
+
json(res, { ok: false, error: { code: "internal", message: "missing platform/target, or both content and image are empty" } }, 400);
|
|
6142
6574
|
return;
|
|
6143
6575
|
}
|
|
6144
|
-
|
|
6145
|
-
|
|
6146
|
-
|
|
6147
|
-
|
|
6576
|
+
if (content !== "") {
|
|
6577
|
+
const textResult = await manager.pushMessage(platform, target, content, { title: title === "" ? void 0 : title });
|
|
6578
|
+
if (!textResult.ok) {
|
|
6579
|
+
json(res, { ok: false, error: { code: "internal", message: textResult.detail } }, 409);
|
|
6580
|
+
return;
|
|
6581
|
+
}
|
|
6582
|
+
}
|
|
6583
|
+
if (imageB64 !== "") {
|
|
6584
|
+
let imagePayload;
|
|
6585
|
+
if (/^https?:\/\//i.test(imageB64)) {
|
|
6586
|
+
imagePayload = imageB64;
|
|
6587
|
+
} else {
|
|
6588
|
+
const cleanB64 = imageB64.replace(/^data:image\/[a-zA-Z0-9+.-]+;base64,/, "");
|
|
6589
|
+
try {
|
|
6590
|
+
imagePayload = Buffer.from(cleanB64, "base64");
|
|
6591
|
+
} catch {
|
|
6592
|
+
json(res, { ok: false, error: { code: "internal", message: "invalid base64 image data" } }, 400);
|
|
6593
|
+
return;
|
|
6594
|
+
}
|
|
6595
|
+
}
|
|
6596
|
+
const imgResult = await manager.pushImage(platform, target, imagePayload, {
|
|
6597
|
+
caption: content !== "" ? void 0 : title !== "" ? title : void 0,
|
|
6598
|
+
filename
|
|
6599
|
+
});
|
|
6600
|
+
if (!imgResult.ok) {
|
|
6601
|
+
json(res, { ok: false, error: { code: "internal", message: imgResult.detail } }, 409);
|
|
6602
|
+
return;
|
|
6603
|
+
}
|
|
6148
6604
|
}
|
|
6149
6605
|
json(res, { ok: true, value: { sent: true } });
|
|
6150
6606
|
return;
|
|
6151
6607
|
}
|
|
6608
|
+
if (path === "/gateway/recent-target") {
|
|
6609
|
+
const fs = await import("node:fs");
|
|
6610
|
+
const pathModule = await import("node:path");
|
|
6611
|
+
const os = await import("node:os");
|
|
6612
|
+
const targetFile = pathModule.join(os.homedir(), ".dsh", "latest_wecom_target.txt");
|
|
6613
|
+
let target = "";
|
|
6614
|
+
try {
|
|
6615
|
+
target = fs.readFileSync(targetFile, "utf8").trim();
|
|
6616
|
+
} catch {
|
|
6617
|
+
}
|
|
6618
|
+
json(res, { ok: true, value: { target } });
|
|
6619
|
+
return;
|
|
6620
|
+
}
|
|
6152
6621
|
if (path === "/gateway/send") {
|
|
6153
6622
|
const body = payload;
|
|
6154
6623
|
const chatid = typeof body?.chatid === "string" ? body.chatid.trim() : "";
|
|
@@ -6343,6 +6812,14 @@ function apply(ctx, config = Config({})) {
|
|
|
6343
6812
|
title: {
|
|
6344
6813
|
type: "string",
|
|
6345
6814
|
description: "\u53EF\u9009\u6807\u9898\uFF08email \u4F5C\u4E3A\u4E3B\u9898\uFF0C\u5176\u4ED6\u5E73\u53F0\u4F5C\u4E3A\u9996\u884C\u52A0\u7C97\u524D\u7F00\uFF09"
|
|
6815
|
+
},
|
|
6816
|
+
image: {
|
|
6817
|
+
type: "string",
|
|
6818
|
+
description: "\u53EF\u9009\u56FE\u7247\u6570\u636E\uFF1ABase64 \u5B57\u7B26\u4E32\u6216\u516C\u7F51\u53EF\u8BBF\u95EE\u7684 http/https \u56FE\u7247 URL"
|
|
6819
|
+
},
|
|
6820
|
+
filename: {
|
|
6821
|
+
type: "string",
|
|
6822
|
+
description: "\u53EF\u9009\u56FE\u7247\u6587\u4EF6\u540D\uFF08\u5982 screenshot.png\uFF0C\u9ED8\u8BA4 image.png\uFF09"
|
|
6346
6823
|
}
|
|
6347
6824
|
},
|
|
6348
6825
|
output: {
|
|
@@ -6354,14 +6831,27 @@ function apply(ctx, config = Config({})) {
|
|
|
6354
6831
|
const target = String(args.target ?? "").trim();
|
|
6355
6832
|
const message = String(args.message ?? "").trim();
|
|
6356
6833
|
const title = args.title !== void 0 ? String(args.title).trim() : void 0;
|
|
6357
|
-
|
|
6358
|
-
|
|
6834
|
+
const image = args.image !== void 0 ? String(args.image).trim() : void 0;
|
|
6835
|
+
const filename = args.filename !== void 0 ? String(args.filename).trim() : void 0;
|
|
6836
|
+
if (!platform || !target || !message && !image) {
|
|
6837
|
+
return "\u9519\u8BEF\uFF1Aplatform\u3001target \u4E3A\u5FC5\u586B\uFF0C\u4E14 message \u4E0E image \u81F3\u5C11\u63D0\u4F9B\u4E00\u4E2A";
|
|
6359
6838
|
}
|
|
6360
|
-
|
|
6361
|
-
|
|
6362
|
-
return `\
|
|
6839
|
+
if (message) {
|
|
6840
|
+
const res = await manager.pushMessage(platform, target, message, { title });
|
|
6841
|
+
if (!res.ok) return `\u6587\u672C\u63A8\u9001\u5931\u8D25\uFF1A${res.detail}`;
|
|
6842
|
+
}
|
|
6843
|
+
if (image) {
|
|
6844
|
+
let imgPayload;
|
|
6845
|
+
if (/^https?:\/\//i.test(image)) {
|
|
6846
|
+
imgPayload = image;
|
|
6847
|
+
} else {
|
|
6848
|
+
const clean = image.replace(/^data:image\/[a-zA-Z0-9+.-]+;base64,/, "");
|
|
6849
|
+
imgPayload = Buffer.from(clean, "base64");
|
|
6850
|
+
}
|
|
6851
|
+
const imgRes = await manager.pushImage(platform, target, imgPayload, { caption: message ? void 0 : title, filename });
|
|
6852
|
+
if (!imgRes.ok) return `\u56FE\u7247\u63A8\u9001\u5931\u8D25\uFF1A${imgRes.detail}`;
|
|
6363
6853
|
}
|
|
6364
|
-
return `\u63A8\u9001\
|
|
6854
|
+
return `\u5DF2\u6210\u529F\u63A8\u9001\u81F3 [${platform}] \u76EE\u6807 ${target}`;
|
|
6365
6855
|
}
|
|
6366
6856
|
}));
|
|
6367
6857
|
}
|