dsh-message-gateway 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1,3 +1,49 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
+ };
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+
11
+ // src/host/gateway-store.ts
12
+ var gateway_store_exports = {};
13
+ __export(gateway_store_exports, {
14
+ loadStore: () => loadStore,
15
+ saveStore: () => saveStore
16
+ });
17
+ import { chmod, mkdir, readFile, rename, writeFile } from "node:fs/promises";
18
+ import { homedir } from "node:os";
19
+ import { join } from "node:path";
20
+ async function loadStore() {
21
+ try {
22
+ const text = await readFile(FILE, "utf8");
23
+ const parsed = JSON.parse(text);
24
+ return {
25
+ platforms: parsed.platforms ?? {},
26
+ statuses: parsed.statuses ?? {}
27
+ };
28
+ } catch {
29
+ return { platforms: {}, statuses: {} };
30
+ }
31
+ }
32
+ async function saveStore(data) {
33
+ await mkdir(join(homedir(), ".dsh"), { recursive: true });
34
+ const tmp = `${FILE}.tmp`;
35
+ await writeFile(tmp, JSON.stringify(data, null, 2), { mode: 384 });
36
+ await chmod(tmp, 384);
37
+ await rename(tmp, FILE);
38
+ }
39
+ var FILE;
40
+ var init_gateway_store = __esm({
41
+ "src/host/gateway-store.ts"() {
42
+ "use strict";
43
+ FILE = join(homedir(), ".dsh", "gateway.json");
44
+ }
45
+ });
46
+
1
47
  // node_modules/@deepseek-ai/dsh-tools/lib/index.js
2
48
  import { Service } from "@deepseek-ai/cordis";
3
49
  import z from "@deepseek-ai/schemastery";
@@ -5057,12 +5103,116 @@ ${content}` : content;
5057
5103
  const sent = await this.email.send(target, subject, content);
5058
5104
  return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "email send failed" };
5059
5105
  }
5106
+ case "dingtalk": {
5107
+ const rawUrl = target.startsWith("http") ? target : `https://oapi.dingtalk.com/robot/send?access_token=${encodeURIComponent(target)}`;
5108
+ let targetUrl = rawUrl;
5109
+ const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
5110
+ const store = await loadStore2();
5111
+ const secret = store.platforms.dingtalk?.secret;
5112
+ if (secret) {
5113
+ const { createHmac: createHmac2 } = await import("node:crypto");
5114
+ const timestamp = Date.now();
5115
+ const stringToSign = `${timestamp}
5116
+ ${secret}`;
5117
+ const sign2 = encodeURIComponent(createHmac2("sha256", secret).update(stringToSign, "utf8").digest("base64"));
5118
+ targetUrl += `${rawUrl.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${sign2}`;
5119
+ }
5120
+ const resp = await fetch(targetUrl, {
5121
+ method: "POST",
5122
+ headers: { "content-type": "application/json" },
5123
+ body: JSON.stringify({
5124
+ msgtype: "markdown",
5125
+ markdown: { title: opts.title ?? "DSH \u901A\u77E5", text }
5126
+ })
5127
+ });
5128
+ const r = await resp.json().catch(() => ({}));
5129
+ return r.errcode === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.errmsg ?? "dingtalk send failed" };
5130
+ }
5131
+ case "feishu": {
5132
+ const rawUrl = target.startsWith("http") ? target : `https://open.feishu.cn/open-apis/bot/v2/hook/${encodeURIComponent(target)}`;
5133
+ const payload = {
5134
+ msg_type: "text",
5135
+ content: { text }
5136
+ };
5137
+ const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
5138
+ const store = await loadStore2();
5139
+ const secret = store.platforms.feishu?.secret;
5140
+ if (secret) {
5141
+ const { createHmac: createHmac2 } = await import("node:crypto");
5142
+ const timestamp = Math.floor(Date.now() / 1e3);
5143
+ const stringToSign = `${timestamp}
5144
+ ${secret}`;
5145
+ const sign2 = createHmac2("sha256", stringToSign).digest("base64");
5146
+ payload.timestamp = String(timestamp);
5147
+ payload.sign = sign2;
5148
+ }
5149
+ const resp = await fetch(rawUrl, {
5150
+ method: "POST",
5151
+ headers: { "content-type": "application/json" },
5152
+ body: JSON.stringify(payload)
5153
+ });
5154
+ const r = await resp.json().catch(() => ({}));
5155
+ return r.code === 0 || r.StatusCode === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.msg ?? "feishu send failed" };
5156
+ }
5157
+ case "bark": {
5158
+ const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
5159
+ const store = await loadStore2();
5160
+ const server = (store.platforms.bark?.serverUrl || "https://api.day.app").replace(/\/+$/, "");
5161
+ const deviceKey = target || store.platforms.bark?.deviceKey;
5162
+ if (!deviceKey) return { ok: false, detail: "missing bark device key" };
5163
+ const url = `${server}/${encodeURIComponent(deviceKey)}/`;
5164
+ const resp = await fetch(url, {
5165
+ method: "POST",
5166
+ headers: { "content-type": "application/json" },
5167
+ body: JSON.stringify({ title: opts.title ?? "DSH \u901A\u77E5", body: content, group: "DSH" })
5168
+ });
5169
+ const r = await resp.json().catch(() => ({}));
5170
+ return r.code === 200 || resp.status === 200 ? { ok: true, detail: "sent" } : { ok: false, detail: r.message ?? "bark send failed" };
5171
+ }
5172
+ case "serverchan": {
5173
+ const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
5174
+ const store = await loadStore2();
5175
+ const sendKey = target || store.platforms.serverchan?.sendKey;
5176
+ if (!sendKey) return { ok: false, detail: "missing serverchan sendkey" };
5177
+ const url = `https://sctapi.ftqq.com/${encodeURIComponent(sendKey)}.send`;
5178
+ const resp = await fetch(url, {
5179
+ method: "POST",
5180
+ headers: { "content-type": "application/json" },
5181
+ body: JSON.stringify({ title: (opts.title ?? "DSH \u901A\u77E5").slice(0, 32), desp: content })
5182
+ });
5183
+ const r = await resp.json().catch(() => ({}));
5184
+ return r.code === 0 ? { ok: true, detail: "sent" } : { ok: false, detail: r.message ?? "serverchan send failed" };
5185
+ }
5060
5186
  case "qq":
5061
5187
  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
5188
  default:
5063
5189
  return { ok: false, detail: `platform "${platform}" does not support push` };
5064
5190
  }
5065
5191
  }
5192
+ /**
5193
+ * 向已配置的 Outbound Webhooks 广播事件(异步投递,失败不阻塞)。
5194
+ */
5195
+ async broadcastEvent(event, payload) {
5196
+ const webhooks = this.config.outboundWebhooks ?? [];
5197
+ if (webhooks.length === 0) return;
5198
+ const { createHmac: createHmac2 } = await import("node:crypto");
5199
+ const bodyStr = JSON.stringify({ event, timestamp: Date.now(), ...payload });
5200
+ for (const hook of webhooks) {
5201
+ const allowed = hook.events ?? ["*"];
5202
+ if (!allowed.includes("*") && !allowed.includes(event)) continue;
5203
+ void (async () => {
5204
+ try {
5205
+ const headers = { "content-type": "application/json" };
5206
+ if (hook.secret) {
5207
+ headers["x-gateway-signature"] = createHmac2("sha256", hook.secret).update(bodyStr, "utf8").digest("hex");
5208
+ }
5209
+ await fetch(hook.url, { method: "POST", headers, body: bodyStr, signal: AbortSignal.timeout(1e4) });
5210
+ } catch (error) {
5211
+ console.warn("[dsh-message-gateway] outbound webhook post failed", hook.url, String(error));
5212
+ }
5213
+ })();
5214
+ }
5215
+ }
5066
5216
  // ==================== Telegram / Discord / QQ / Email ====================
5067
5217
  telegram = null;
5068
5218
  discord = null;
@@ -5255,32 +5405,11 @@ function nowInShanghai() {
5255
5405
  }).format(/* @__PURE__ */ new Date());
5256
5406
  }
5257
5407
 
5258
- // src/host/gateway-store.ts
5259
- import { chmod, mkdir, readFile, rename, writeFile } from "node:fs/promises";
5260
- import { homedir } from "node:os";
5261
- import { join } from "node:path";
5262
- var FILE = join(homedir(), ".dsh", "gateway.json");
5263
- async function loadStore() {
5264
- try {
5265
- const text = await readFile(FILE, "utf8");
5266
- const parsed = JSON.parse(text);
5267
- return {
5268
- platforms: parsed.platforms ?? {},
5269
- statuses: parsed.statuses ?? {}
5270
- };
5271
- } catch {
5272
- return { platforms: {}, statuses: {} };
5273
- }
5274
- }
5275
- async function saveStore(data) {
5276
- await mkdir(join(homedir(), ".dsh"), { recursive: true });
5277
- const tmp = `${FILE}.tmp`;
5278
- await writeFile(tmp, JSON.stringify(data, null, 2), { mode: 384 });
5279
- await chmod(tmp, 384);
5280
- await rename(tmp, FILE);
5281
- }
5408
+ // src/index.ts
5409
+ init_gateway_store();
5282
5410
 
5283
5411
  // src/host/routes.ts
5412
+ init_gateway_store();
5284
5413
  import { createHmac, timingSafeEqual } from "node:crypto";
5285
5414
 
5286
5415
  // src/host/platforms.ts
@@ -5389,6 +5518,49 @@ var PLATFORMS = [
5389
5518
  { key: "smtpPass", labelKey: "field.smtpPass", placeholderKey: "field.secret.ph", kind: "secret" }
5390
5519
  ]
5391
5520
  },
5521
+ {
5522
+ id: "dingtalk",
5523
+ nameKey: "platform.dingtalk",
5524
+ icon: "\u{1F4CC}",
5525
+ testable: true,
5526
+ hintKey: "platform.dingtalk.hint",
5527
+ fields: [
5528
+ { key: "webhookUrl", labelKey: "field.webhookUrl", placeholderKey: "field.dingtalkWebhook.ph", kind: "text" },
5529
+ { key: "secret", labelKey: "field.dingtalkSecret", placeholderKey: "field.secret.ph", kind: "secret" }
5530
+ ]
5531
+ },
5532
+ {
5533
+ id: "feishu",
5534
+ nameKey: "platform.feishu",
5535
+ icon: "\u{1F54A}\uFE0F",
5536
+ testable: true,
5537
+ hintKey: "platform.feishu.hint",
5538
+ fields: [
5539
+ { key: "webhookUrl", labelKey: "field.webhookUrl", placeholderKey: "field.feishuWebhook.ph", kind: "text" },
5540
+ { key: "secret", labelKey: "field.feishuSecret", placeholderKey: "field.secret.ph", kind: "secret" }
5541
+ ]
5542
+ },
5543
+ {
5544
+ id: "bark",
5545
+ nameKey: "platform.bark",
5546
+ icon: "\u{1F514}",
5547
+ testable: true,
5548
+ hintKey: "platform.bark.hint",
5549
+ fields: [
5550
+ { key: "serverUrl", labelKey: "field.barkServer", placeholderKey: "field.barkServer.ph", kind: "text" },
5551
+ { key: "deviceKey", labelKey: "field.barkKey", placeholderKey: "field.barkKey.ph", kind: "secret" }
5552
+ ]
5553
+ },
5554
+ {
5555
+ id: "serverchan",
5556
+ nameKey: "platform.serverchan",
5557
+ icon: "\u{1F680}",
5558
+ testable: true,
5559
+ hintKey: "platform.serverchan.hint",
5560
+ fields: [
5561
+ { key: "sendKey", labelKey: "field.serverchanKey", placeholderKey: "field.serverchanKey.ph", kind: "secret" }
5562
+ ]
5563
+ },
5392
5564
  {
5393
5565
  id: "webhooks",
5394
5566
  nameKey: "platform.webhooks",
@@ -5514,6 +5686,82 @@ async function testPlatform(id, cred) {
5514
5686
  }
5515
5687
  return { ok: false, detail: banner === "" ? `\u65E0\u6CD5\u8FDE\u63A5 ${cred.imapHost}:${port}` : banner };
5516
5688
  }
5689
+ case "dingtalk": {
5690
+ const rawUrl = cred.webhookUrl ?? "";
5691
+ if (!rawUrl.startsWith("http")) return { ok: false, detail: "\u65E0\u6548\u7684 Webhook URL" };
5692
+ let targetUrl = rawUrl;
5693
+ if (cred.secret) {
5694
+ const { createHmac: createHmac2 } = await import("node:crypto");
5695
+ const timestamp = Date.now();
5696
+ const stringToSign = `${timestamp}
5697
+ ${cred.secret}`;
5698
+ const sign2 = encodeURIComponent(createHmac2("sha256", cred.secret).update(stringToSign, "utf8").digest("base64"));
5699
+ targetUrl += `${rawUrl.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${sign2}`;
5700
+ }
5701
+ const { status, body } = await httpJson(targetUrl, {
5702
+ method: "POST",
5703
+ headers: { "content-type": "application/json" },
5704
+ body: JSON.stringify({
5705
+ msgtype: "markdown",
5706
+ 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" }
5707
+ })
5708
+ });
5709
+ const r = body;
5710
+ if (r.errcode === 0) return { ok: true, detail: "\u9489\u9489\u673A\u5668\u4EBA\u8FDE\u63A5\u6210\u529F" };
5711
+ return { ok: false, detail: r.errmsg ?? `HTTP ${status}` };
5712
+ }
5713
+ case "feishu": {
5714
+ const rawUrl = cred.webhookUrl ?? "";
5715
+ if (!rawUrl.startsWith("http")) return { ok: false, detail: "\u65E0\u6548\u7684 Webhook URL" };
5716
+ const payload = {
5717
+ msg_type: "text",
5718
+ content: { text: "\u{1F50C} [DSH Message Gateway] \u98DE\u4E66\u81EA\u5B9A\u4E49\u673A\u5668\u4EBA\u8FDE\u63A5\u6D4B\u8BD5\u6210\u529F\uFF01" }
5719
+ };
5720
+ if (cred.secret) {
5721
+ const { createHmac: createHmac2 } = await import("node:crypto");
5722
+ const timestamp = Math.floor(Date.now() / 1e3);
5723
+ const stringToSign = `${timestamp}
5724
+ ${cred.secret}`;
5725
+ const sign2 = createHmac2("sha256", stringToSign).digest("base64");
5726
+ payload.timestamp = String(timestamp);
5727
+ payload.sign = sign2;
5728
+ }
5729
+ const { status, body } = await httpJson(rawUrl, {
5730
+ method: "POST",
5731
+ headers: { "content-type": "application/json" },
5732
+ body: JSON.stringify(payload)
5733
+ });
5734
+ const r = body;
5735
+ if (r.code === 0 || r.StatusCode === 0) return { ok: true, detail: "\u98DE\u4E66\u673A\u5668\u4EBA\u8FDE\u63A5\u6210\u529F" };
5736
+ return { ok: false, detail: r.msg ?? `HTTP ${status}` };
5737
+ }
5738
+ case "bark": {
5739
+ const server = (cred.serverUrl || "https://api.day.app").replace(/\/+$/, "");
5740
+ const key = cred.deviceKey ?? "";
5741
+ if (!key) return { ok: false, detail: "\u8BF7\u586B\u5199 Device Key" };
5742
+ const url = `${server}/${encodeURIComponent(key)}/`;
5743
+ const { status, body } = await httpJson(url, {
5744
+ method: "POST",
5745
+ headers: { "content-type": "application/json" },
5746
+ body: JSON.stringify({ title: "DSH \u8FDE\u63A5\u6D4B\u8BD5", body: "Bark \u624B\u673A\u63A8\u9001\u901A\u9053\u5DF2\u8FDE\u901A", group: "DSH" })
5747
+ });
5748
+ const r = body;
5749
+ if (r.code === 200 || status === 200) return { ok: true, detail: "Bark \u63A8\u9001\u6D4B\u8BD5\u6210\u529F" };
5750
+ return { ok: false, detail: r.message ?? `HTTP ${status}` };
5751
+ }
5752
+ case "serverchan": {
5753
+ const key = cred.sendKey ?? "";
5754
+ if (!key) return { ok: false, detail: "\u8BF7\u586B\u5199 SendKey" };
5755
+ const url = `https://sctapi.ftqq.com/${encodeURIComponent(key)}.send`;
5756
+ const { status, body } = await httpJson(url, {
5757
+ method: "POST",
5758
+ headers: { "content-type": "application/json" },
5759
+ body: JSON.stringify({ title: "DSH\u8FDE\u63A5\u6D4B\u8BD5", desp: "Server\u9171\u6D88\u606F\u901A\u9053\u6D4B\u8BD5\u6210\u529F" })
5760
+ });
5761
+ const r = body;
5762
+ if (r.code === 0) return { ok: true, detail: "Server\u9171\u63A8\u9001\u6D4B\u8BD5\u6210\u529F" };
5763
+ return { ok: false, detail: r.message ?? `HTTP ${status}` };
5764
+ }
5517
5765
  default:
5518
5766
  return { ok: true, detail: "configured" };
5519
5767
  }
@@ -6244,6 +6492,11 @@ var Config = Schema.object({
6244
6492
  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"),
6245
6493
  groupReply: Schema.boolean().default(true).description("\u662F\u5426\u56DE\u590D\u7FA4\u804A\u6D88\u606F\uFF08false \u65F6\u53EA\u5904\u7406\u5355\u804A\uFF09"),
6246
6494
  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"),
6495
+ outboundWebhooks: Schema.array(Schema.object({
6496
+ url: Schema.string().required().description("\u76EE\u6807 Webhook \u63A5\u6536 URL"),
6497
+ secret: Schema.string().description("\u53EF\u9009 HMAC-SHA256 \u7B7E\u540D\u5BC6\u94A5\uFF08\u5C06\u5728 X-Gateway-Signature \u5934\u9644\u5E26 hex \u7B7E\u540D\uFF09"),
6498
+ events: Schema.array(Schema.string()).default(["*"]).description('\u8BA2\u9605\u7684\u4E8B\u4EF6\u7C7B\u578B\uFF08\u5982 ["message.in", "message.out", "agent.done"]\uFF0C\u9ED8\u8BA4 ["*"]\uFF09')
6499
+ })).default([]).description("Outbound Webhooks \u5916\u90E8\u4E8B\u4EF6\u8BA2\u9605\u5E7F\u64AD\u5217\u8868"),
6247
6500
  routes: Schema.array(Schema.object({
6248
6501
  id: Schema.string().required().description("\u89C4\u5219 id\uFF08\u552F\u4E00\u6807\u8BC6\uFF09"),
6249
6502
  matchPlatform: Schema.string().description("\u5339\u914D\u5E73\u53F0 id\uFF08telegram/discord/qq/wecom-aibot/email/webhook \u7B49\uFF09\uFF1B\u7F3A\u7701=\u5168\u90E8\u5E73\u53F0"),