From d087a64de854882e1b55c88536a7bf106929d869 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Fri, 8 Nov 2024 17:47:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20Discord=20?= =?UTF-8?q?=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 --- jest.config.ts | 1 + src/push/custom-email.ts | 4 +-- src/push/dingtalk.ts | 9 ++++-- src/push/discord.ts | 64 +++++++++++++++++++++++++++++++--------- src/utils/ajax.ts | 2 +- 5 files changed, 60 insertions(+), 20 deletions(-) diff --git a/jest.config.ts b/jest.config.ts index 3b6fe94..d56b55c 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -2,6 +2,7 @@ import path from 'path' import type { Config } from 'jest' const config: Config = { + testTimeout: 20000, moduleNameMapper: { '^@/(.*)$': '/src/$1', }, diff --git a/src/push/custom-email.ts b/src/push/custom-email.ts index ba28943..b5085ab 100644 --- a/src/push/custom-email.ts +++ b/src/push/custom-email.ts @@ -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> { Debugger('title: "%s", desp: "%s", option: %o', title, desp, option) diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index fc96c23..3ea8454 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -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' @@ -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 @@ -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 diff --git a/src/push/discord.ts b/src/push/discord.ts index 032814e..cd34cf7 100644 --- a/src/push/discord.ts +++ b/src/push/discord.ts @@ -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 + +export interface DiscordResponse { } + /** * Discord Webhook 推送 * @@ -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 { + async send(title: string, desp: string = '', option?: DiscordOption): Promise> { + 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, }, }) } diff --git a/src/utils/ajax.ts b/src/utils/ajax.ts index b731716..249e26c 100644 --- a/src/utils/ajax.ts +++ b/src/utils/ajax.ts @@ -60,7 +60,7 @@ export async function ajax(config: AjaxConfig): Promise