Skip to content

Commit

Permalink
refactor: 重构 Qmsg 酱 为新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 72b3457 commit 284a56d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/push/i-got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
15 changes: 8 additions & 7 deletions src/push/one-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface OneBotConfig {
ONE_BOT_ACCESS_TOKEN?: string
}

export interface PrivateMsgOption {
export interface OneBotPrivateMsgOption {
/**
* 消息类型
*/
Expand All @@ -31,7 +31,7 @@ export interface PrivateMsgOption {
user_id: number
}

export interface GroupMsgOption {
export interface OneBotGroupMsgOption {
/**
* 消息类型
*/
Expand All @@ -43,7 +43,7 @@ export interface GroupMsgOption {

}

export type OneBotOption = (PrivateMsgOption | GroupMsgOption) & {
export type OneBotOption = (OneBotPrivateMsgOption | OneBotGroupMsgOption) & {
/**
* 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效
*/
Expand Down Expand Up @@ -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<SendResponse<OneBotResponse>> {
async send(title: string, desp: string, option: OneBotOption): Promise<SendResponse<OneBotResponse>> {
Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option)
const { message_type, ...args } = option || {}
const message = `${title}${desp ? `\n${desp}` : ''}`
Expand Down
15 changes: 7 additions & 8 deletions src/push/push-plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SendResponse<PushPlusResponse>> {
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',
Expand Down
62 changes: 50 additions & 12 deletions src/push/qmsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand All @@ -21,35 +58,36 @@ 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 是必须的!')
}
}

/**
*
*
* 发送消息
* @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<SendResponse> {
Debugger('msg: "%s", qq: "%s", type: "%s"', msg, qq, type)
async send(title: string, desp: string, option: QmsgOption): Promise<SendResponse> {
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 },
})
}

Expand Down

0 comments on commit 284a56d

Please sign in to comment.