dsh-message-gateway 0.1.16 → 0.1.18

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
@@ -3270,6 +3270,27 @@ var WecomBridge = class {
3270
3270
  return false;
3271
3271
  }
3272
3272
  }
3273
+ /**
3274
+ * 主动向会话发送图片消息(chatid:单聊=userid,群聊=群 ID)。
3275
+ * 内部自动上传临时素材(WeCom uploadMedia)后通过 aibot_send_msg 发送。
3276
+ * @param chatid 接收会话 ID
3277
+ * @param image 图片数据(Buffer)
3278
+ * @param filename 可选文件名(默认 image.png)
3279
+ */
3280
+ async sendImage(chatid, image, filename = "image.png") {
3281
+ try {
3282
+ const media = await this.client?.uploadMedia(image, { type: "image", filename });
3283
+ if (!media?.media_id) {
3284
+ console.error("[dsh-message-gateway] sendImage upload failed: no media_id returned");
3285
+ return false;
3286
+ }
3287
+ await this.client?.sendMediaMessage(chatid, "image", media.media_id);
3288
+ return true;
3289
+ } catch (error) {
3290
+ console.error("[dsh-message-gateway] sendImage failed", error);
3291
+ return false;
3292
+ }
3293
+ }
3273
3294
  /** 回复欢迎语(enter_chat 事件)。 */
3274
3295
  async welcome(frame, content) {
3275
3296
  try {
@@ -3419,6 +3440,38 @@ var TelegramBridge = class {
3419
3440
  const result = await this.sendText(chatId, content);
3420
3441
  return result !== null;
3421
3442
  }
3443
+ /**
3444
+ * 主动推送图片:向指定 chat 发送照片(支持 Buffer 或图片 URL)。
3445
+ * @param chatId 目标聊天 ID
3446
+ * @param photo 图片数据 Buffer 或公网图片 URL
3447
+ * @param caption 可选图片附带文字说明
3448
+ * @param filename 可选文件名
3449
+ */
3450
+ async sendPhoto(chatId, photo, caption, filename = "photo.png") {
3451
+ try {
3452
+ if (typeof photo === "string") {
3453
+ const body = { chat_id: chatId, photo };
3454
+ if (caption) body.caption = caption.slice(0, 1024);
3455
+ const result = await this.api("sendPhoto", body);
3456
+ return result?.message_id !== void 0;
3457
+ }
3458
+ const form = new FormData();
3459
+ form.append("chat_id", String(chatId));
3460
+ form.append("photo", new Blob([new Uint8Array(photo)]), filename);
3461
+ if (caption) form.append("caption", caption.slice(0, 1024));
3462
+ const response = await fetch(`https://api.telegram.org/bot${this.token}/sendPhoto`, {
3463
+ method: "POST",
3464
+ body: form,
3465
+ signal: AbortSignal.timeout(3e4)
3466
+ });
3467
+ if (!response.ok) return false;
3468
+ const data = await response.json();
3469
+ return data.ok === true;
3470
+ } catch (error) {
3471
+ console.error("[dsh-message-gateway] telegram sendPhoto failed", error);
3472
+ return false;
3473
+ }
3474
+ }
3422
3475
  /** 发送文本(Markdown 失败自动降级纯文本)。 */
3423
3476
  async sendText(chatId, text) {
3424
3477
  const body = { chat_id: chatId, text: text.slice(0, MSG_LIMIT), parse_mode: "Markdown", disable_web_page_preview: true };
@@ -3642,6 +3695,34 @@ var DiscordBridge = class {
3642
3695
  return false;
3643
3696
  }
3644
3697
  }
3698
+ /**
3699
+ * 主动推送图片:向指定频道发送图片附件。
3700
+ * @param channelId 目标频道 ID
3701
+ * @param image 图片数据 Buffer
3702
+ * @param content 可选文字说明
3703
+ * @param filename 可选文件名
3704
+ */
3705
+ async sendImage(channelId, image, content, filename = "image.png") {
3706
+ try {
3707
+ const form = new FormData();
3708
+ form.append("files[0]", new Blob([new Uint8Array(image)]), filename);
3709
+ if (content) {
3710
+ form.append("payload_json", JSON.stringify({ content: content.slice(0, MSG_LIMIT2) }));
3711
+ }
3712
+ const response = await fetch(`${REST}/channels/${encodeURIComponent(channelId)}/messages`, {
3713
+ method: "POST",
3714
+ headers: { authorization: `Bot ${this.token}` },
3715
+ body: form,
3716
+ signal: AbortSignal.timeout(3e4)
3717
+ });
3718
+ if (!response.ok) return false;
3719
+ const data = await response.json();
3720
+ return data.id !== void 0;
3721
+ } catch (error) {
3722
+ console.error("[dsh-message-gateway] discord sendImage failed", error);
3723
+ return false;
3724
+ }
3725
+ }
3645
3726
  /** 停止网关连接。 */
3646
3727
  stop() {
3647
3728
  this.stopped = true;
@@ -4292,18 +4373,18 @@ var ImapClient = class {
4292
4373
  return match[1].trim().split(/\s+/).filter((s) => s !== "").map(Number);
4293
4374
  }
4294
4375
  /** FETCH 头部与正文(BODY.PEEK 不置已读)。 */
4295
- async fetchMessage(id) {
4376
+ async fetchMessage(id2) {
4296
4377
  const res = await this.command(
4297
- `FETCH ${id} (UID BODY.PEEK[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)] BODY.PEEK[TEXT])`,
4378
+ `FETCH ${id2} (UID BODY.PEEK[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)] BODY.PEEK[TEXT])`,
4298
4379
  3e4
4299
4380
  );
4300
- const uid = res.match(/UID (\d+)/)?.[1] ?? String(id);
4381
+ const uid = res.match(/UID (\d+)/)?.[1] ?? String(id2);
4301
4382
  const header = literalAt(res, "BODY[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)]")?.value ?? "";
4302
4383
  const text = literalAt(res, "BODY[TEXT]")?.value ?? "";
4303
4384
  return { uid, header, text };
4304
4385
  }
4305
- async markSeen(id) {
4306
- await this.command(`STORE ${id} +FLAGS (\\Seen)`);
4386
+ async markSeen(id2) {
4387
+ await this.command(`STORE ${id2} +FLAGS (\\Seen)`);
4307
4388
  }
4308
4389
  close() {
4309
4390
  try {
@@ -4566,9 +4647,9 @@ var EmailBridge = class {
4566
4647
  this.setStatus("connected", "email");
4567
4648
  }
4568
4649
  const ids = await this.imap.searchUnseen();
4569
- for (const id of ids) {
4650
+ for (const id2 of ids) {
4570
4651
  if (this.stopped) break;
4571
- await this.processMessage(id);
4652
+ await this.processMessage(id2);
4572
4653
  }
4573
4654
  await sleep(POLL_MS);
4574
4655
  } catch (error) {
@@ -4580,16 +4661,16 @@ var EmailBridge = class {
4580
4661
  }
4581
4662
  }
4582
4663
  }
4583
- async processMessage(id) {
4664
+ async processMessage(id2) {
4584
4665
  if (this.imap === null) return;
4585
4666
  let fetched;
4586
4667
  try {
4587
- fetched = await this.imap.fetchMessage(id);
4668
+ fetched = await this.imap.fetchMessage(id2);
4588
4669
  } catch {
4589
4670
  return;
4590
4671
  }
4591
4672
  if (this.seen.has(fetched.uid)) {
4592
- void this.imap.markSeen(id).catch(() => {
4673
+ void this.imap.markSeen(id2).catch(() => {
4593
4674
  });
4594
4675
  return;
4595
4676
  }
@@ -4601,7 +4682,7 @@ var EmailBridge = class {
4601
4682
  const from = parseAddress(headerField(fetched.header, "From"));
4602
4683
  const subject = headerField(fetched.header, "Subject");
4603
4684
  const text = cleanBody(fetched.text);
4604
- void this.imap.markSeen(id).catch(() => {
4685
+ void this.imap.markSeen(id2).catch(() => {
4605
4686
  });
4606
4687
  if (from === "" || text.trim() === "") return;
4607
4688
  const frame = { from, subject, threadKey };
@@ -4712,10 +4793,14 @@ var BridgeManager = class {
4712
4793
  return;
4713
4794
  }
4714
4795
  if (event.type === "turn/end") {
4715
- if (p.buffer !== "" && p.pushed) {
4796
+ if (p.buffer !== "") {
4716
4797
  void this.pushStream(p, true);
4717
4798
  this.finishPending(key);
4718
4799
  return;
4800
+ } else {
4801
+ id.sink.stream(id.frame, p.streamId, "\u2705 \u5904\u7406\u5B8C\u6210", true);
4802
+ this.finishPending(key);
4803
+ return;
4719
4804
  }
4720
4805
  }
4721
4806
  }
@@ -4893,26 +4978,26 @@ var BridgeManager = class {
4893
4978
  * 外部平台消息统一入口:命令优先,否则注入该聊天独立 agent 会话,
4894
4979
  * 回复经 sink 流式回发。所有已打通的平台共用此管线。
4895
4980
  */
4896
- async handleExternalMessage(id, rawText) {
4981
+ async handleExternalMessage(id2, rawText) {
4897
4982
  if (this.disposed) return;
4898
4983
  const text = stripMention(rawText);
4899
4984
  const reply = (content) => {
4900
- id.sink.stream(id.frame, `cmd-${Date.now().toString(36)}`, content, true);
4985
+ id2.sink.stream(id2.frame, `cmd-${Date.now().toString(36)}`, content, true);
4901
4986
  };
4902
4987
  try {
4903
- if (id.chatType === "group" && !this.config.groupReply) return;
4904
- if (await this.handleCommand(text, id.key, reply)) return;
4988
+ if (id2.chatType === "group" && !this.config.groupReply) return;
4989
+ if (await this.handleCommand(text, id2.key, reply)) return;
4905
4990
  if (this.awaiting !== null) {
4906
4991
  console.warn("[dsh-message-gateway] busy: webhook in flight, drop message");
4907
4992
  return;
4908
4993
  }
4909
- if (this.pendingMap.has(id.key)) {
4910
- id.sink.stream(id.frame, `busy-${Date.now().toString(36)}`, this.t("busy"), true);
4994
+ if (this.pendingMap.has(id2.key)) {
4995
+ id2.sink.stream(id2.frame, `busy-${Date.now().toString(36)}`, this.t("busy"), true);
4911
4996
  return;
4912
4997
  }
4913
- const route = this.matchRoute(id.key, text);
4998
+ const route = this.matchRoute(id2.key, text);
4914
4999
  const routedText = route !== null && route.prefix !== "" ? text.slice(route.prefix.length).trim() : text;
4915
- const agent = await this.ensureAgentForKey(id.key, route?.rule);
5000
+ const agent = await this.ensureAgentForKey(id2.key, route?.rule);
4916
5001
  if (agent === null) {
4917
5002
  console.warn("[dsh-message-gateway] no dedicated agent available");
4918
5003
  return;
@@ -4925,8 +5010,8 @@ var BridgeManager = class {
4925
5010
  };
4926
5011
  const session = agent.session;
4927
5012
  const p = {
4928
- frame: id.frame,
4929
- sink: id.sink,
5013
+ frame: id2.frame,
5014
+ sink: id2.sink,
4930
5015
  streamId: `gw-${Date.now().toString(36)}`,
4931
5016
  buffer: "",
4932
5017
  cursor: session.seq,
@@ -4939,26 +5024,26 @@ var BridgeManager = class {
4939
5024
  startedAt: Date.now(),
4940
5025
  heartbeat: null
4941
5026
  };
4942
- this.pendingMap.set(id.key, p);
4943
- if (id.sink.ack !== false) {
5027
+ this.pendingMap.set(id2.key, p);
5028
+ if (id2.sink.ack !== false) {
4944
5029
  p.ackSent = true;
4945
- id.sink.stream(id.frame, p.streamId, this.t("ack"), false);
5030
+ id2.sink.stream(id2.frame, p.streamId, this.t("ack"), false);
4946
5031
  p.heartbeat = setInterval(() => {
4947
- if (this.pendingMap.get(id.key) !== p || p.pushed) return;
5032
+ if (this.pendingMap.get(id2.key) !== p || p.pushed) return;
4948
5033
  const elapsed = Math.round((Date.now() - p.startedAt) / 1e3);
4949
- id.sink.stream(id.frame, p.streamId, `${this.t("ack")}\uFF08${elapsed}s\uFF09`, false);
5034
+ id2.sink.stream(id2.frame, p.streamId, `${this.t("ack")}\uFF08${elapsed}s\uFF09`, false);
4950
5035
  }, ACK_HEARTBEAT_INTERVAL);
4951
5036
  p.heartbeat.unref?.();
4952
5037
  }
4953
5038
  agent.send(message, "next-turn", true);
4954
- console.log("[dsh-message-gateway] sent to agent", { key: id.key, text: sanitizeSecrets(text.slice(0, 60)), baseSeq: p.cursor });
5039
+ console.log("[dsh-message-gateway] sent to agent", { key: id2.key, text: sanitizeSecrets(text.slice(0, 60)), baseSeq: p.cursor });
4955
5040
  p.timer = setInterval(() => {
4956
- if (this.pendingMap.get(id.key) !== p) return;
4957
- this.pollPending(id.key, p, session);
5041
+ if (this.pendingMap.get(id2.key) !== p) return;
5042
+ this.pollPending(id2.key, p, session);
4958
5043
  }, 400);
4959
5044
  p.fallback = setTimeout(() => {
4960
- if (this.pendingMap.get(id.key) === p) this.finishPending(id.key);
4961
- }, 10 * 60 * 1e3);
5045
+ if (this.pendingMap.get(id2.key) === p) this.finishPending(id2.key);
5046
+ }, 2 * 60 * 1e3);
4962
5047
  p.fallback.unref?.();
4963
5048
  } catch (error) {
4964
5049
  console.error("[dsh-message-gateway] handleExternalMessage failed", error);
@@ -5189,6 +5274,115 @@ ${secret}`;
5189
5274
  return { ok: false, detail: `platform "${platform}" does not support push` };
5190
5275
  }
5191
5276
  }
5277
+ /**
5278
+ * 主动推送图片:向指定平台的目标会话发送图片。
5279
+ * - wecom-aibot: 自动上传临时素材并发送 image 消息
5280
+ * - telegram: 调用 sendPhoto(支持 Buffer 上传或图片 URL)
5281
+ * - discord: multipart 上传 files[0] 附件
5282
+ * - bark: 支持图片 URL 传入(通过 image 字段富文本横幅展示)
5283
+ * - dingtalk: 支持图片 URL(在 Markdown 中嵌入 ![]())
5284
+ * - serverchan: 支持图片 URL(在 Markdown desp 中嵌入 ![]())
5285
+ * - feishu / qq / email / 其它: 对不支持的二进制上传返回明确的官方协议限制原因
5286
+ *
5287
+ * @param platform 平台 id
5288
+ * @param target 目标(wecom-aibot=userid/群ID;telegram=chatId;discord=channelId;bark=deviceKey 等)
5289
+ * @param image 图片数据 Buffer 或可公网访问的图片 URL 字符串
5290
+ * @param opts.caption 可选文字说明
5291
+ * @param opts.filename 可选文件名(默认 image.png)
5292
+ */
5293
+ async pushImage(platform, target, image, opts = {}) {
5294
+ const filename = opts.filename ?? "image.png";
5295
+ const isUrl = typeof image === "string" && /^https?:\/\//i.test(image.trim());
5296
+ switch (platform) {
5297
+ case "wecom-aibot": {
5298
+ if (this.wecom === null) return { ok: false, detail: "wecom-aibot bridge not connected" };
5299
+ if (typeof image === "string") {
5300
+ try {
5301
+ const resp = await fetch(image, { signal: AbortSignal.timeout(2e4) });
5302
+ if (!resp.ok) return { ok: false, detail: `failed to fetch image from url: HTTP ${resp.status}` };
5303
+ image = Buffer.from(await resp.arrayBuffer());
5304
+ } catch (err) {
5305
+ return { ok: false, detail: `fetch image url error: ${err instanceof Error ? err.message : String(err)}` };
5306
+ }
5307
+ }
5308
+ const sent = await this.wecom.sendImage(target, image, filename);
5309
+ return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "wecom image send failed" };
5310
+ }
5311
+ case "telegram": {
5312
+ if (this.telegram === null) return { ok: false, detail: "telegram bridge not connected" };
5313
+ const chatId = Number(target);
5314
+ if (!Number.isInteger(chatId) || chatId <= 0) return { ok: false, detail: "telegram target must be a numeric chatId" };
5315
+ const sent = await this.telegram.sendPhoto(chatId, image, opts.caption, filename);
5316
+ return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "telegram photo send failed" };
5317
+ }
5318
+ case "discord": {
5319
+ if (this.discord === null) return { ok: false, detail: "discord bridge not connected" };
5320
+ let buf;
5321
+ if (typeof image === "string") {
5322
+ try {
5323
+ const resp = await fetch(image, { signal: AbortSignal.timeout(2e4) });
5324
+ if (!resp.ok) return { ok: false, detail: `failed to fetch image from url: HTTP ${resp.status}` };
5325
+ buf = Buffer.from(await resp.arrayBuffer());
5326
+ } catch (err) {
5327
+ return { ok: false, detail: `fetch image url error: ${err instanceof Error ? err.message : String(err)}` };
5328
+ }
5329
+ } else {
5330
+ buf = image;
5331
+ }
5332
+ const sent = await this.discord.sendImage(target, buf, opts.caption, filename);
5333
+ return sent ? { ok: true, detail: "sent" } : { ok: false, detail: "discord image send failed" };
5334
+ }
5335
+ case "bark": {
5336
+ if (!isUrl) {
5337
+ return { ok: false, detail: "Bark only supports public image URLs (pass image as http/https URL string)" };
5338
+ }
5339
+ const { loadStore: loadStore2 } = await Promise.resolve().then(() => (init_gateway_store(), gateway_store_exports));
5340
+ const store = await loadStore2();
5341
+ const server = (store.platforms.bark?.serverUrl || "https://api.day.app").replace(/\/+$/, "");
5342
+ const deviceKey = target || store.platforms.bark?.deviceKey;
5343
+ if (!deviceKey) return { ok: false, detail: "missing bark device key" };
5344
+ const url = `${server}/${encodeURIComponent(deviceKey)}/`;
5345
+ const resp = await fetch(url, {
5346
+ method: "POST",
5347
+ headers: { "content-type": "application/json" },
5348
+ body: JSON.stringify({
5349
+ title: opts.caption ?? "DSH \u901A\u77E5",
5350
+ body: opts.caption ? "" : "\u6536\u5230\u4E00\u5F20\u56FE\u7247",
5351
+ image,
5352
+ group: "DSH"
5353
+ })
5354
+ });
5355
+ const r = await resp.json().catch(() => ({}));
5356
+ return r.code === 200 || resp.status === 200 ? { ok: true, detail: "sent" } : { ok: false, detail: r.message ?? "bark send failed" };
5357
+ }
5358
+ case "dingtalk": {
5359
+ if (!isUrl) {
5360
+ return { ok: false, detail: "DingTalk custom robot only supports public image URLs via Markdown image tag" };
5361
+ }
5362
+ const caption = opts.caption ?? "\u56FE\u7247";
5363
+ const mdText = `${caption ? `${caption}
5364
+
5365
+ ` : ""}![${filename}](${image})`;
5366
+ return this.pushMessage("dingtalk", target, mdText, { title: opts.caption ?? "\u56FE\u7247" });
5367
+ }
5368
+ case "serverchan": {
5369
+ if (!isUrl) {
5370
+ return { ok: false, detail: "ServerChan only supports public image URLs via Markdown desp" };
5371
+ }
5372
+ const caption = opts.caption ?? "\u56FE\u7247";
5373
+ const mdText = `${caption ? `${caption}
5374
+
5375
+ ` : ""}![${filename}](${image})`;
5376
+ return this.pushMessage("serverchan", target, mdText, { title: opts.caption ?? "\u56FE\u7247" });
5377
+ }
5378
+ case "feishu":
5379
+ return { ok: false, detail: "Feishu custom robot webhook does not support binary image upload without tenant_access_token (im/v1/images)" };
5380
+ case "qq":
5381
+ return { ok: false, detail: "QQ platform has discontinued active push API since 2025-04-21" };
5382
+ default:
5383
+ return { ok: false, detail: `platform "${platform}" does not support image push` };
5384
+ }
5385
+ }
5192
5386
  /**
5193
5387
  * 向已配置的 Outbound Webhooks 广播事件(异步投递,失败不阻塞)。
5194
5388
  */
@@ -5289,16 +5483,16 @@ ${secret}`;
5289
5483
  this.email = null;
5290
5484
  }
5291
5485
  /** 任意桥接平台的状态(telegram/discord/qq/email)。 */
5292
- bridgeStatus(id) {
5293
- if (id === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
5294
- if (id === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
5295
- if (id === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
5296
- if (id === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
5486
+ bridgeStatus(id2) {
5487
+ if (id2 === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
5488
+ if (id2 === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
5489
+ if (id2 === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
5490
+ if (id2 === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
5297
5491
  return { state: "idle", detail: "", connectedAt: null };
5298
5492
  }
5299
5493
  /** 将桥状态并入存储状态。 */
5300
- mergeStatus(stored, id = "wecom-aibot") {
5301
- const live = id === "wecom-aibot" ? this.wecomStatus() : this.bridgeStatus(id);
5494
+ mergeStatus(stored, id2 = "wecom-aibot") {
5495
+ const live = id2 === "wecom-aibot" ? this.wecomStatus() : this.bridgeStatus(id2);
5302
5496
  if (live.state === "connected") {
5303
5497
  return { state: "connected", detail: live.detail, testedAt: live.connectedAt ?? stored?.testedAt ?? null };
5304
5498
  }
@@ -5570,8 +5764,8 @@ var PLATFORMS = [
5570
5764
  fields: [{ key: "secret", labelKey: "field.webhookSecret", placeholderKey: "field.secret.ph", kind: "secret" }]
5571
5765
  }
5572
5766
  ];
5573
- function platformDef(id) {
5574
- return PLATFORMS.find((p) => p.id === id);
5767
+ function platformDef(id2) {
5768
+ return PLATFORMS.find((p) => p.id === id2);
5575
5769
  }
5576
5770
  async function httpJson(url, init) {
5577
5771
  const controller = new AbortController();
@@ -5609,8 +5803,8 @@ function tcpBanner(host, port, timeoutMs = 8e3) {
5609
5803
  });
5610
5804
  });
5611
5805
  }
5612
- async function testPlatform(id, cred) {
5613
- switch (id) {
5806
+ async function testPlatform(id2, cred) {
5807
+ switch (id2) {
5614
5808
  case "telegram": {
5615
5809
  const { status, body } = await httpJson(`https://api.telegram.org/bot${cred.token ?? ""}/getMe`);
5616
5810
  const r = body;
@@ -6036,7 +6230,7 @@ function callbackIdentity(key, send) {
6036
6230
  }
6037
6231
 
6038
6232
  // src/host/routes.ts
6039
- var BODY_CAP_BYTES = 1 << 20;
6233
+ var BODY_CAP_BYTES = 8 << 20;
6040
6234
  var CALLBACK_PATHS = {
6041
6235
  wecom: "/gateway/wecom/callback",
6042
6236
  "wechat-mp": "/gateway/wechat-mp/callback",
@@ -6361,14 +6555,40 @@ function registerGatewayRoutes(ctx, manager) {
6361
6555
  const target = typeof body?.target === "string" ? body.target.trim() : "";
6362
6556
  const content = typeof body?.content === "string" ? body.content.trim() : "";
6363
6557
  const title = typeof body?.title === "string" ? body.title.trim() : "";
6364
- if (platform === "" || target === "" || content === "") {
6365
- json(res, { ok: false, error: { code: "internal", message: "missing platform/target/content" } }, 400);
6558
+ const imageB64 = typeof body?.image === "string" ? body.image.trim() : "";
6559
+ const filename = typeof body?.filename === "string" && body.filename.trim() !== "" ? body.filename.trim() : void 0;
6560
+ if (platform === "" || target === "" || content === "" && imageB64 === "") {
6561
+ json(res, { ok: false, error: { code: "internal", message: "missing platform/target, or both content and image are empty" } }, 400);
6366
6562
  return;
6367
6563
  }
6368
- const result = await manager.pushMessage(platform, target, content, { title: title === "" ? void 0 : title });
6369
- if (!result.ok) {
6370
- json(res, { ok: false, error: { code: "internal", message: result.detail } }, 409);
6371
- return;
6564
+ if (content !== "") {
6565
+ const textResult = await manager.pushMessage(platform, target, content, { title: title === "" ? void 0 : title });
6566
+ if (!textResult.ok) {
6567
+ json(res, { ok: false, error: { code: "internal", message: textResult.detail } }, 409);
6568
+ return;
6569
+ }
6570
+ }
6571
+ if (imageB64 !== "") {
6572
+ let imagePayload;
6573
+ if (/^https?:\/\//i.test(imageB64)) {
6574
+ imagePayload = imageB64;
6575
+ } else {
6576
+ const cleanB64 = imageB64.replace(/^data:image\/[a-zA-Z0-9+.-]+;base64,/, "");
6577
+ try {
6578
+ imagePayload = Buffer.from(cleanB64, "base64");
6579
+ } catch {
6580
+ json(res, { ok: false, error: { code: "internal", message: "invalid base64 image data" } }, 400);
6581
+ return;
6582
+ }
6583
+ }
6584
+ const imgResult = await manager.pushImage(platform, target, imagePayload, {
6585
+ caption: content !== "" ? void 0 : title !== "" ? title : void 0,
6586
+ filename
6587
+ });
6588
+ if (!imgResult.ok) {
6589
+ json(res, { ok: false, error: { code: "internal", message: imgResult.detail } }, 409);
6590
+ return;
6591
+ }
6372
6592
  }
6373
6593
  json(res, { ok: true, value: { sent: true } });
6374
6594
  return;
@@ -6567,6 +6787,14 @@ function apply(ctx, config = Config({})) {
6567
6787
  title: {
6568
6788
  type: "string",
6569
6789
  description: "\u53EF\u9009\u6807\u9898\uFF08email \u4F5C\u4E3A\u4E3B\u9898\uFF0C\u5176\u4ED6\u5E73\u53F0\u4F5C\u4E3A\u9996\u884C\u52A0\u7C97\u524D\u7F00\uFF09"
6790
+ },
6791
+ image: {
6792
+ type: "string",
6793
+ description: "\u53EF\u9009\u56FE\u7247\u6570\u636E\uFF1ABase64 \u5B57\u7B26\u4E32\u6216\u516C\u7F51\u53EF\u8BBF\u95EE\u7684 http/https \u56FE\u7247 URL"
6794
+ },
6795
+ filename: {
6796
+ type: "string",
6797
+ description: "\u53EF\u9009\u56FE\u7247\u6587\u4EF6\u540D\uFF08\u5982 screenshot.png\uFF0C\u9ED8\u8BA4 image.png\uFF09"
6570
6798
  }
6571
6799
  },
6572
6800
  output: {
@@ -6578,14 +6806,27 @@ function apply(ctx, config = Config({})) {
6578
6806
  const target = String(args.target ?? "").trim();
6579
6807
  const message = String(args.message ?? "").trim();
6580
6808
  const title = args.title !== void 0 ? String(args.title).trim() : void 0;
6581
- if (!platform || !target || !message) {
6582
- return "\u9519\u8BEF\uFF1Aplatform, target \u4E0E message \u5747\u4E3A\u5FC5\u586B\u53C2\u6570";
6809
+ const image = args.image !== void 0 ? String(args.image).trim() : void 0;
6810
+ const filename = args.filename !== void 0 ? String(args.filename).trim() : void 0;
6811
+ if (!platform || !target || !message && !image) {
6812
+ return "\u9519\u8BEF\uFF1Aplatform\u3001target \u4E3A\u5FC5\u586B\uFF0C\u4E14 message \u4E0E image \u81F3\u5C11\u63D0\u4F9B\u4E00\u4E2A";
6583
6813
  }
6584
- const res = await manager.pushMessage(platform, target, message, { title });
6585
- if (res.ok) {
6586
- return `\u6D88\u606F\u5DF2\u6210\u529F\u63A8\u9001\u81F3 [${platform}] \u76EE\u6807 ${target}`;
6814
+ if (message) {
6815
+ const res = await manager.pushMessage(platform, target, message, { title });
6816
+ if (!res.ok) return `\u6587\u672C\u63A8\u9001\u5931\u8D25\uFF1A${res.detail}`;
6817
+ }
6818
+ if (image) {
6819
+ let imgPayload;
6820
+ if (/^https?:\/\//i.test(image)) {
6821
+ imgPayload = image;
6822
+ } else {
6823
+ const clean = image.replace(/^data:image\/[a-zA-Z0-9+.-]+;base64,/, "");
6824
+ imgPayload = Buffer.from(clean, "base64");
6825
+ }
6826
+ const imgRes = await manager.pushImage(platform, target, imgPayload, { caption: message ? void 0 : title, filename });
6827
+ if (!imgRes.ok) return `\u56FE\u7247\u63A8\u9001\u5931\u8D25\uFF1A${imgRes.detail}`;
6587
6828
  }
6588
- return `\u63A8\u9001\u5931\u8D25\uFF1A${res.detail}`;
6829
+ return `\u5DF2\u6210\u529F\u63A8\u9001\u81F3 [${platform}] \u76EE\u6807 ${target}`;
6589
6830
  }
6590
6831
  }));
6591
6832
  }