Skip to content

Commit

Permalink
refactor: 重构 Discord 为新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 82bfab4 commit d087a64
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import type { Config } from 'jest'

const config: Config = {
testTimeout: 20000,
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
Expand Down
4 changes: 2 additions & 2 deletions src/push/custom-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export class CustomEmail implements Send {
}

/**
*
*
* @author CaoMeiYouRen
* @date 2023-03-12
* @date 2024-11-08
* @param title 消息的标题
* @param [desp] 消息的内容,支持 html
* @param [option] 选项,可以覆盖默认的同名配置
*/
async send(title: string, desp?: string, option?: CustomEmailOption): Promise<SendResponse<SMTPTransport.SentMessageInfo>> {
Debugger('title: "%s", desp: "%s", option: %o', title, desp, option)
Expand Down
9 changes: 6 additions & 3 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AxiosResponse } from 'axios'
import debug from 'debug'

import { MessageTemplateAbs } from './dingtalk/template'
import { Text } from './dingtalk/Text'
import { Markdown } from './dingtalk/Markdown'
Expand Down Expand Up @@ -31,8 +30,6 @@ export interface DingtalkResponse {
/**
* 钉钉机器人推送
* 在 [dingtalk-robot-sdk](https://github.com/ineo6/dingtalk-robot-sdk) 的基础上重构了一下,用法几乎完全一致。
* 参考文档 [钉钉开放平台 - 自定义机器人接入](https://developers.dingtalk.com/document/app/custom-robot-access)
*
* @author CaoMeiYouRen
* @date 2021-02-27
* @export
Expand All @@ -48,6 +45,12 @@ export class Dingtalk implements Send {
private SECRET?: string
private webhook: string = 'https://oapi.dingtalk.com/robot/send'

/**
* 参考文档 [钉钉开放平台 - 自定义机器人接入](https://developers.dingtalk.com/document/app/custom-robot-access)
* @author CaoMeiYouRen
* @date 2024-11-08
* @param config
*/
constructor(config: DingtalkConfig) {
const { DINGTALK_ACCESS_TOKEN, DINGTALK_SECRET } = config
this.ACCESS_TOKEN = DINGTALK_ACCESS_TOKEN
Expand Down
64 changes: 50 additions & 14 deletions src/push/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ import { SendResponse } from '@/interfaces/response'

const Debugger = debug('push:discord')

export interface DiscordConfig {
/**
* Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取
*/
DISCORD_WEBHOOK: string
/**
* 机器人显示的名称
*/
DISCORD_USERNAME?: string

/**
* 机器人头像的 Url
*/
DISCORD_AVATAR_URL?: string

/**
* 代理地址
*/
PROXY_URL?: string
}

export type DiscordOption = Pick<DiscordConfig, 'DISCORD_USERNAME' | 'PROXY_URL' | 'DISCORD_AVATAR_URL'>

export interface DiscordResponse { }

/**
* Discord Webhook 推送
*
Expand Down Expand Up @@ -32,42 +57,53 @@ export class Discord implements Send {
*/
private DISCORD_USERNAME?: string

proxyUrl?: string
proxyUrl: string

/**
*
* 创建 Discord 实例
* @author CaoMeiYouRen
* @date 2023-09-17
* @param DISCORD_WEBHOOK Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取
* @param [DISCORD_USERNAME] 机器人显示的名称
* @date 2024-11-08
* @param config 配置
*/
constructor(DISCORD_WEBHOOK: string, DISCORD_USERNAME?: string) {
Debugger('DISCORD_WEBHOOK: %s, DISCORD_USERNAME: %s', DISCORD_WEBHOOK, DISCORD_USERNAME)
constructor(config: DiscordConfig) {
const { DISCORD_WEBHOOK, DISCORD_USERNAME, PROXY_URL } = config
Debugger('DISCORD_WEBHOOK: %s, DISCORD_USERNAME: %s, PROXY_URL: %s', DISCORD_WEBHOOK, DISCORD_USERNAME, PROXY_URL)
this.DISCORD_WEBHOOK = DISCORD_WEBHOOK
if (DISCORD_USERNAME) {
this.DISCORD_USERNAME = DISCORD_USERNAME
}
if (PROXY_URL) {
this.proxyUrl = PROXY_URL
}
if (!this.DISCORD_WEBHOOK) {
throw new Error('DISCORD_WEBHOOK 是必须的!')
}
}

/**
*
* 发送消息
*
* @author CaoMeiYouRen
* @date 2023-09-17
* @param content 要发送的内容
* @return
* @date 2024-11-08
* @param title 消息的标题
* @param [desp] 消息的描述。最多 2000 个字符
* @param [option] 选项,可以覆盖默认的同名配置
*/
async send(content: string): Promise<SendResponse> {
async send(title: string, desp: string = '', option?: DiscordOption): Promise<SendResponse<DiscordResponse>> {
Debugger('title: "%s", desp: "%s", option: %o', title, desp, option)
const { DISCORD_USERNAME, PROXY_URL, DISCORD_AVATAR_URL } = option || {}
const username = DISCORD_USERNAME || this.DISCORD_USERNAME
const avatar_url = DISCORD_AVATAR_URL
const proxyUrl = PROXY_URL || this.proxyUrl
const content = `${title}\n${desp}`.trim()
return ajax({
url: this.DISCORD_WEBHOOK,
method: 'POST',
proxyUrl: this.proxyUrl,
proxyUrl,
data: {
username: this.DISCORD_USERNAME,
username,
content,
avatar_url,
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function ajax<T = any>(config: AjaxConfig): Promise<AxiosResponse<T
headers,
params: query,
data,
timeout: 10000,
timeout: 60000,
httpAgent,
httpsAgent: httpAgent,
proxy: false,
Expand Down

0 comments on commit d087a64

Please sign in to comment.