From 2c85ecfa408cf8566c8a3ddc15c5c2acc9a0d6da Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Fri, 8 Nov 2024 19:02:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20PushDeer=20?= =?UTF-8?q?=E6=8E=A8=E9=80=81=20=E4=B8=BA=E6=96=B0=E7=89=88=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/dingtalk.ts | 2 +- src/push/i-got.ts | 2 +- src/push/one-bot.ts | 2 +- src/push/push-deer.ts | 59 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index 3ea8454..276328a 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -101,7 +101,7 @@ export class Dingtalk implements Send { * * * @author CaoMeiYouRen - * @date 2021-02-28 + * @date 2024-11-08 * @param title 消息的标题 * @param [desp] 消息的内容,支持 Markdown * @returns diff --git a/src/push/i-got.ts b/src/push/i-got.ts index 9d296c3..51927e1 100644 --- a/src/push/i-got.ts +++ b/src/push/i-got.ts @@ -89,7 +89,7 @@ export class IGot implements Send { * * * @author CaoMeiYouRen - * @date 2021-03-03 + * @date 2024-11-08 * @param title 请求标题 * @param [desp] 请求正文 * @param [option] 额外选项 diff --git a/src/push/one-bot.ts b/src/push/one-bot.ts index f1d6ce1..391716d 100644 --- a/src/push/one-bot.ts +++ b/src/push/one-bot.ts @@ -122,7 +122,7 @@ export class OneBot implements Send { /** * * @author CaoMeiYouRen - * @date 2023-10-22 + * @date 2024-11-08 * @param message 要发送的消息 * @param msgType 消息类型 * @param recieverId 用户/群组 ID,即 QQ 号或群号 diff --git a/src/push/push-deer.ts b/src/push/push-deer.ts index d2adcb8..c24be6d 100644 --- a/src/push/push-deer.ts +++ b/src/push/push-deer.ts @@ -7,6 +7,42 @@ const Debugger = debug('push:push-deer') export type PushDeerPushType = 'markdown' | 'text' | 'image' +export interface PushDeerConfig { + /** + * pushkey。请参考 https://github.com/easychen/pushdeer 获取 + */ + PUSH_DEER_PUSH_KEY: string + + /** + * 使用自架版时的服务器端地址。例如 http://127.0.0.1:8800。默认为 https://api2.pushdeer.com + */ + PUSH_DEER_ENDPOINT?: string +} + +export interface PushDeerOption { + /** + * 格式。文本=text,markdown,图片=image,默认为markdown。type 为 image 时,text 中为要发送图片的URL + */ + type?: PushDeerPushType +} + +export interface PushDeerResponse { + /** + * 正确为0,错误为非0 + */ + code: number + /** + * 错误信息。无错误时无此字段 + */ + error: string + /** + * 消息内容,错误时无此字段 + */ + content: { + result: string[] + } +} + /** * PushDeer 推送。 官方文档 https://github.com/easychen/pushdeer * @@ -36,13 +72,13 @@ export class PushDeer implements Send { private PUSH_DEER_ENDPOINT: string /** - * + * 创建 PushDeer 实例 * @author CaoMeiYouRen - * @date 2022-02-28 - * @param PUSH_DEER_PUSH_KEY pushkey - * @param [PUSH_DEER_ENDPOINT] 使用自架版时的服务器端地址 + * @date 2024-11-08 + * @param config 配置 */ - constructor(PUSH_DEER_PUSH_KEY: string, PUSH_DEER_ENDPOINT?: string) { + constructor(config: PushDeerConfig) { + const { PUSH_DEER_PUSH_KEY, PUSH_DEER_ENDPOINT } = config this.PUSH_DEER_PUSH_KEY = PUSH_DEER_PUSH_KEY this.PUSH_DEER_ENDPOINT = PUSH_DEER_ENDPOINT || 'https://api2.pushdeer.com' Debugger('set PUSH_DEER_PUSH_KEY: "%s", PUSH_DEER_ENDPOINT: "%s"', PUSH_DEER_PUSH_KEY, PUSH_DEER_ENDPOINT) @@ -52,16 +88,15 @@ export class PushDeer implements Send { } /** - * - * * @author CaoMeiYouRen - * @date 2022-02-28 + * @date 2024-11-08 * @param text 推送消息内容 * @param [desp=''] 消息内容第二部分 - * @param [type='markdown'] 格式。文本=text,markdown,图片=image,默认为markdown。type 为 image 时,text 中为要发送图片的URL + * @param [option={}] 额外推送选项 */ - async send(text: string, desp: string = '', type: PushDeerPushType = 'markdown'): Promise { - Debugger('text: "%s", desp: "%s", type: "%s"', text, desp, type) + async send(title: string, desp: string = '', option?: PushDeerOption): Promise> { + Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) + const { type = 'markdown' } = option || {} return ajax({ baseURL: this.PUSH_DEER_ENDPOINT, url: '/message/push', @@ -70,7 +105,7 @@ export class PushDeer implements Send { 'Content-Type': 'application/x-www-form-urlencoded', }, data: { - text, + text: title, desp, pushkey: this.PUSH_DEER_PUSH_KEY, type,