Skip to content

Commit

Permalink
refactor: 重构 PushDeer 推送 为新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent b636613 commit 2c85ecf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/push/dingtalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/push/i-got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] 额外选项
Expand Down
2 changes: 1 addition & 1 deletion src/push/one-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 号或群号
Expand Down
59 changes: 47 additions & 12 deletions src/push/push-deer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
Expand All @@ -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<SendResponse> {
Debugger('text: "%s", desp: "%s", type: "%s"', text, desp, type)
async send(title: string, desp: string = '', option?: PushDeerOption): Promise<SendResponse<PushDeerResponse>> {
Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option)
const { type = 'markdown' } = option || {}
return ajax({
baseURL: this.PUSH_DEER_ENDPOINT,
url: '/message/push',
Expand All @@ -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,
Expand Down

0 comments on commit 2c85ecf

Please sign in to comment.