From 284a56da46f6212a55f38e333a94ed6e5ed793c8 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Fri, 8 Nov 2024 22:29:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20Qmsg=20?= =?UTF-8?q?=E9=85=B1=20=E4=B8=BA=E6=96=B0=E7=89=88=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/i-got.ts | 4 +-- src/push/one-bot.ts | 15 ++++++----- src/push/push-plus.ts | 15 +++++------ src/push/qmsg.ts | 62 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/src/push/i-got.ts b/src/push/i-got.ts index 51927e1..cd9bc0e 100644 --- a/src/push/i-got.ts +++ b/src/push/i-got.ts @@ -90,8 +90,8 @@ export class IGot implements Send { * * @author CaoMeiYouRen * @date 2024-11-08 - * @param title 请求标题 - * @param [desp] 请求正文 + * @param title 消息标题 + * @param [desp] 消息正文 * @param [option] 额外选项 * @returns */ diff --git a/src/push/one-bot.ts b/src/push/one-bot.ts index 391716d..dc011fa 100644 --- a/src/push/one-bot.ts +++ b/src/push/one-bot.ts @@ -20,7 +20,7 @@ export interface OneBotConfig { ONE_BOT_ACCESS_TOKEN?: string } -export interface PrivateMsgOption { +export interface OneBotPrivateMsgOption { /** * 消息类型 */ @@ -31,7 +31,7 @@ export interface PrivateMsgOption { user_id: number } -export interface GroupMsgOption { +export interface OneBotGroupMsgOption { /** * 消息类型 */ @@ -43,7 +43,7 @@ export interface GroupMsgOption { } -export type OneBotOption = (PrivateMsgOption | GroupMsgOption) & { +export type OneBotOption = (OneBotPrivateMsgOption | OneBotGroupMsgOption) & { /** * 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效 */ @@ -120,14 +120,15 @@ export class OneBot implements Send { } /** + * * * @author CaoMeiYouRen * @date 2024-11-08 - * @param message 要发送的消息 - * @param msgType 消息类型 - * @param recieverId 用户/群组 ID,即 QQ 号或群号 + * @param title 消息标题 + * @param desp 消息正文 + * @param option 额外推送选项 */ - async send(title: string, desp?: string, option?: OneBotOption): Promise> { + async send(title: string, desp: string, option: OneBotOption): Promise> { Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) const { message_type, ...args } = option || {} const message = `${title}${desp ? `\n${desp}` : ''}` diff --git a/src/push/push-plus.ts b/src/push/push-plus.ts index 63d2516..c77c4eb 100644 --- a/src/push/push-plus.ts +++ b/src/push/push-plus.ts @@ -81,20 +81,19 @@ export class PushPlus implements Send { throw new Error('PUSH_PLUS_TOKEN 是必须的!') } } + /** - * + * 发送消息 * * @author CaoMeiYouRen - * @date 2021-06-06 - * @param title - * @param [content] 消息标题 - * @param [template='html'] 具体消息内容,根据不同template支持不同格式 - * @param [channel='wechat'] 发送渠道 - * @returns + * @date 2024-11-08 + * @param title 消息标题 + * @param [desp=''] 消息内容 + * @param [option] 额外推送选项 */ send(title: string, desp: string = '', option?: PushPlusOption): Promise> { Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) - const { template, channel, ...args } = option || {} + const { template = 'html', channel = 'wechat', ...args } = option || {} const content = desp || title return ajax({ url: 'http://www.pushplus.plus/send', diff --git a/src/push/qmsg.ts b/src/push/qmsg.ts index 2dbfc36..1ca3cd9 100644 --- a/src/push/qmsg.ts +++ b/src/push/qmsg.ts @@ -10,6 +10,43 @@ const Debugger = debug('push:qmsg') */ export type QmsgPushType = 'send' | 'group' +export interface QmsgConfig { + /** + * 推送的 key。在 [Qmsg 酱管理台](https://qmsg.zendee.cn/user) 查看 + */ + QMSG_KEY: string +} + +export interface QmsgPrivateMsgOption { + /** + * send 表示发送消息给指定的QQ号,group 表示发送消息给指定的QQ群。默认为 send + */ + type: 'send' + /** + * 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346 + */ + qq: string +} + +export interface QmsgGroupMsgOption { + /** + * send 表示发送消息给指定的QQ号,group 表示发送消息给指定的QQ群。默认为 send + */ + type: 'group' + /** + * 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346 + */ + qq: string + +} + +export type QmsgOption = (QmsgPrivateMsgOption | QmsgGroupMsgOption) & { + /** + * 机器人的QQ号。指定使用哪个机器人来发送消息,不指定则会自动随机选择一个在线的机器人发送消息。该参数仅私有云有效 + */ + bot?: string +} + /** * Qmsg酱。使用说明见 [Qmsg酱](https://qmsg.zendee.cn/docs) * @@ -21,12 +58,11 @@ export type QmsgPushType = 'send' | 'group' export class Qmsg implements Send { private QMSG_KEY: string - private QMSG_BOT?: string - constructor(QMSG_KEY: string, QMSG_BOT?: string) { + constructor(config: QmsgConfig) { + const { QMSG_KEY } = config this.QMSG_KEY = QMSG_KEY - this.QMSG_BOT = QMSG_BOT - Debugger('set QMSG_KEY: "%s", QMSG_BOT: "%s"', QMSG_KEY, QMSG_BOT) + Debugger('set QMSG_KEY: "%s"', QMSG_KEY) if (!this.QMSG_KEY) { throw new Error('QMSG_KEY 是必须的!') } @@ -34,22 +70,24 @@ export class Qmsg implements Send { /** * - * + * 发送消息 * @author CaoMeiYouRen - * @date 2024-10-30 - * @param msg 要推送的消息内容 - * @param [qq] 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346 - * @param [type='send'] send 表示发送消息给指定的QQ号,group 表示发送消息给指定的QQ群。默认为 send + * @date 2024-11-08 + * @param title 消息标题 + * @param [desp] 消息描述 + * @param [option] QmsgOption 选项 */ - async send(msg: string, qq?: string, type: QmsgPushType = 'send'): Promise { - Debugger('msg: "%s", qq: "%s", type: "%s"', msg, qq, type) + async send(title: string, desp: string, option: QmsgOption): Promise { + Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) + const { qq, type = 'send', bot } = option || {} + const msg = `${title}${desp ? `\n${desp}` : ''}` return ajax({ url: `https://qmsg.zendee.cn/${type}/${this.QMSG_KEY}`, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, method: 'POST', - data: { msg, qq, bot: this.QMSG_BOT }, + data: { msg, qq, bot }, }) }