koishi-plugin-rina-currency 0.0.5 → 0.1.0

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/config.d.ts CHANGED
@@ -29,6 +29,7 @@ export interface Config {
29
29
  collectOnStart: boolean;
30
30
  chartWidth: number;
31
31
  chartHeight: number;
32
+ qqCombineImageAndText: boolean;
32
33
  broadcasts: BroadcastTarget[];
33
34
  thresholdAlerts: ThresholdAlert[];
34
35
  }
package/lib/index.d.ts CHANGED
@@ -2,7 +2,10 @@ import { Context } from 'koishi';
2
2
  import 'koishi-plugin-cron';
3
3
  import { Config as ConfigSchema, Config as PluginConfig } from './config';
4
4
  export declare const name = "rina-currency";
5
- export declare const inject: string[];
5
+ export declare const inject: {
6
+ required: string[];
7
+ optional: string[];
8
+ };
6
9
  export type Config = PluginConfig;
7
10
  export declare const Config: import("schemastery")<ConfigSchema>;
8
11
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -52,9 +52,9 @@ var RateTypeSchema = import_koishi.Schema.union([
52
52
  ]);
53
53
  var BroadcastSchema = import_koishi.Schema.object({
54
54
  enabled: import_koishi.Schema.boolean().default(true).description("启用这条播报规则"),
55
- platform: import_koishi.Schema.string().required().description("平台名称,例如 onebot、qq、telegram"),
56
- selfId: import_koishi.Schema.string().required().description("用于发送的机器人账号 ID"),
57
- channelId: import_koishi.Schema.string().required().description("目标频道或群组 ID"),
55
+ platform: import_koishi.Schema.string().required().description("平台名称,例如 qq、onebot、telegram"),
56
+ selfId: import_koishi.Schema.string().required().description("用于发送的机器人账号 ID(QQ 官方机器人填 AppID)"),
57
+ channelId: import_koishi.Schema.string().required().description("目标群组 ID(QQ 官方支持直接填 group_openid,或在启用 rina-qq-bridge 时填已映射群号)"),
58
58
  hour: import_koishi.Schema.number().min(0).max(23).default(9).description("北京时间小时"),
59
59
  minute: import_koishi.Schema.number().min(0).max(59).default(0).description("北京时间分钟"),
60
60
  includeChart: import_koishi.Schema.boolean().default(true).description("附上趋势图"),
@@ -65,9 +65,9 @@ var ThresholdSchema = import_koishi.Schema.object({
65
65
  bank: BankSchema.default("boc"),
66
66
  rateType: RateTypeSchema.default("spotSell"),
67
67
  changePercent: import_koishi.Schema.number().min(0.01).max(100).required().description("相邻采样点的绝对涨跌幅达到此百分比时提醒,例如 0.5 表示上涨或下跌 0.5%"),
68
- platform: import_koishi.Schema.string().required().description("平台名称"),
69
- selfId: import_koishi.Schema.string().required().description("机器人账号 ID"),
70
- channelId: import_koishi.Schema.string().required().description("目标频道或群组 ID")
68
+ platform: import_koishi.Schema.string().required().description("平台名称,例如 qq、onebot"),
69
+ selfId: import_koishi.Schema.string().required().description("机器人账号 ID(QQ 官方机器人填 AppID)"),
70
+ channelId: import_koishi.Schema.string().required().description("目标群组 ID(QQ 官方支持 group_openid 或已映射群号)")
71
71
  });
72
72
  var Config = import_koishi.Schema.object({
73
73
  defaultBank: BankSchema.default("boc").description("购汇成本与单银行趋势的默认参考银行"),
@@ -79,6 +79,7 @@ var Config = import_koishi.Schema.object({
79
79
  collectOnStart: import_koishi.Schema.boolean().default(true).description("插件启动后立即采集一次"),
80
80
  chartWidth: import_koishi.Schema.number().min(600).max(2e3).default(1200).description("趋势图宽度"),
81
81
  chartHeight: import_koishi.Schema.number().min(360).max(1200).default(680).description("趋势图高度"),
82
+ qqCombineImageAndText: import_koishi.Schema.boolean().default(true).description("在 QQ 官方群(platform=qq)下合并发送图片与文字摘要,节省消息配额"),
82
83
  broadcasts: import_koishi.Schema.array(BroadcastSchema).default([]).description("每日定时播报目标"),
83
84
  thresholdAlerts: import_koishi.Schema.array(ThresholdSchema).default([]).description("跌破阈值提醒规则")
84
85
  });
@@ -359,6 +360,22 @@ function registerCommands(ctx, service, config) {
359
360
  if (!metrics) return `暂无${BANK_LABELS[bank]}的历史牌价,已尝试采集,请稍后重试。`;
360
361
  const summary = formatTrendSummary(bank, type, metrics, days);
361
362
  if (!session) return summary;
363
+ if (session.platform === "qq" && config.qqCombineImageAndText) {
364
+ try {
365
+ const image = await renderTrendChart(metrics, type, {
366
+ width: config.chartWidth,
367
+ height: config.chartHeight,
368
+ days
369
+ });
370
+ await session.send([import_koishi2.h.image(image, "image/png"), import_koishi2.h.text(summary)]);
371
+ } catch (error) {
372
+ ctx.logger("rina-currency").warn(`生成趋势图失败:${String(error)}`);
373
+ await session.send(`${summary}
374
+
375
+ 趋势图生成失败,已返回文字摘要。`);
376
+ }
377
+ return;
378
+ }
362
379
  await session.send(summary);
363
380
  try {
364
381
  const image = await renderTrendChart(metrics, type, {
@@ -587,8 +604,42 @@ var IcbcProvider = class {
587
604
  }
588
605
  };
589
606
 
590
- // src/scheduler.ts
607
+ // src/qq.ts
591
608
  var import_koishi3 = require("koishi");
609
+ function getQqBridge(ctx) {
610
+ return ctx.rinaQq;
611
+ }
612
+ __name(getQqBridge, "getQqBridge");
613
+ function isPlainNumericId(value) {
614
+ return /^\d{5,16}$/.test(value.trim());
615
+ }
616
+ __name(isPlainNumericId, "isPlainNumericId");
617
+ async function sendTargetMessage(ctx, logger, bot, channelId, content, image, combineImageAndText) {
618
+ const bridge = getQqBridge(ctx);
619
+ if (bridge) {
620
+ await bridge.sendGroupMessage(bot, channelId, {
621
+ text: content,
622
+ image,
623
+ mimeType: "image/png",
624
+ combineImageAndText
625
+ });
626
+ return;
627
+ }
628
+ if (bot.platform === "qq") {
629
+ if (isPlainNumericId(channelId)) {
630
+ logger.warn(`QQ 群目标 ${channelId} 看起来是纯数字群号,且未启用 rina-qq-bridge 映射服务,可能无法被官方接口识别为 group_openid。`);
631
+ }
632
+ if (image && content && combineImageAndText) {
633
+ await bot.sendMessage(channelId, [import_koishi3.h.image(image, "image/png"), import_koishi3.h.text(content)]);
634
+ return;
635
+ }
636
+ }
637
+ await bot.sendMessage(channelId, content);
638
+ if (image) await bot.sendMessage(channelId, import_koishi3.h.image(image, "image/png"));
639
+ }
640
+ __name(sendTargetMessage, "sendTargetMessage");
641
+
642
+ // src/scheduler.ts
592
643
  function chinaClock(now = /* @__PURE__ */ new Date()) {
593
644
  const parts = new Intl.DateTimeFormat("en-CA", {
594
645
  timeZone: "Asia/Shanghai",
@@ -672,8 +723,7 @@ var Scheduler = class {
672
723
  this.logger.warn(`未找到机器人 ${target.platform}:${target.selfId},无法向 ${target.channelId} 发送汇率消息。`);
673
724
  return;
674
725
  }
675
- await bot.sendMessage(target.channelId, content);
676
- if (image) await bot.sendMessage(target.channelId, import_koishi3.h.image(image, "image/png"));
726
+ await sendTargetMessage(this.ctx, this.logger, bot, target.channelId, content, image, this.config.qqCombineImageAndText);
677
727
  }
678
728
  async collectAndAlert(options = {}) {
679
729
  try {
@@ -829,7 +879,7 @@ var RateService = class {
829
879
 
830
880
  // src/index.ts
831
881
  var name = "rina-currency";
832
- var inject = ["database", "cron"];
882
+ var inject = { required: ["database", "cron"], optional: ["rinaQq"] };
833
883
  var Config2 = Config;
834
884
  function apply(ctx, config) {
835
885
  extendModel(ctx);
package/lib/qq.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Bot, Context, Logger } from 'koishi';
2
+ export interface OptionalQqBridge {
3
+ resolveGroupId(platform: string, botSelfId: string, configuredGroupId: string): Promise<string>;
4
+ sendGroupMessage(bot: Bot, channelId: string, options: {
5
+ text?: string;
6
+ image?: Buffer;
7
+ mimeType?: string;
8
+ combineImageAndText?: boolean;
9
+ }): Promise<void>;
10
+ }
11
+ export declare function getQqBridge(ctx: Context): OptionalQqBridge | undefined;
12
+ export declare function isPlainNumericId(value: string): boolean;
13
+ export declare function sendTargetMessage(ctx: Context, logger: Logger, bot: Bot, channelId: string, content: string, image: Buffer | undefined, combineImageAndText: boolean): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-rina-currency",
3
3
  "description": "面向购汇决策的日元汇率插件,基于中行和工行数据",
4
- "version": "0.0.5",
4
+ "version": "0.1.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -40,4 +40,4 @@
40
40
  ]
41
41
  }
42
42
  }
43
- }
43
+ }