From 664ca210a5eada05eb65ca02b862dee17b02f69f Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sat, 9 Nov 2024 17:42:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20runPushAllInOne=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/index.ts | 1 + src/one.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/push/dingtalk.ts | 5 ++++- 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/one.ts diff --git a/package.json b/package.json index d5ba217..a4cb61c 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "server-chan", "serverchan", "server-chan-turbo", + "server-chan-v3", "email", "custom-email", "nodemailer", diff --git a/src/index.ts b/src/index.ts index 01fae54..9cc59ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,3 +13,4 @@ export * from './push/wechat-app' export * from './push/wechat-robot' export * from './push/xi-zhi' +export * from './one' diff --git a/src/one.ts b/src/one.ts new file mode 100644 index 0000000..667be29 --- /dev/null +++ b/src/one.ts @@ -0,0 +1,49 @@ +import { CustomEmail, Dingtalk, Discord, IGot, OneBot, PushDeer, PushPlus, Qmsg, ServerChanTurbo, ServerChanV3, Telegram, WechatApp, WechatRobot, XiZhi } from '../src' +import { SendResponse } from '../src/interfaces/response' + +const PushAllInOne = { + CustomEmail, + Dingtalk, + Discord, + IGot, + OneBot, + PushDeer, + PushPlus, + Qmsg, + ServerChanTurbo, + ServerChanV3, + Telegram, + WechatApp, + WechatRobot, + XiZhi, +} as const + +export type IPushAllInOne = typeof PushAllInOne + +export type PushType = keyof IPushAllInOne + +export type MetaPushConfig = { + type: T + config: ConstructorParameters[0] + option: Parameters[2] +} + +/** + * 从传入变量中读取配置,并选择一个渠道推送 + * + * @author CaoMeiYouRen + * @date 2024-11-09 + * @export + * @template T + * @param title 推送标题 + * @param desp 推送内容 + * @param pushConfig 推送配置 + */ +export async function runPushAllInOne(title: string, desp: string, pushConfig: MetaPushConfig): Promise> { + const { type, config, option } = pushConfig + if (PushAllInOne[type]) { + const push = new PushAllInOne[type](config as any) + return push.send(title, desp, option as any) + } + throw new Error('未匹配到任何推送方式!') +} diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index 2cdd409..9e1665a 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -11,6 +11,8 @@ import { SendResponse } from '@/interfaces/response' const Debugger = debug('push:dingtalk') +export type DINGTALK_MSG_TYPE = 'text' | 'markdown' | 'link' | 'actionCard' | 'feedCard' + export interface DingtalkConfig { /** * 钉钉机器人 access_token。官方文档:https://developers.dingtalk.com/document/app/custom-robot-access @@ -23,6 +25,7 @@ export interface DingtalkConfig { } export interface DingtalkOption { + [key: string]: any } export interface DingtalkResponse { @@ -110,7 +113,7 @@ export class Dingtalk implements Send { * @returns */ async send(title: string, desp?: string, option?: DingtalkOption): Promise> { - Debugger('title: "%s", desp: "%s"', title, desp) + Debugger('title: "%s", desp: "%s", option: %O', title, desp, option) if (!desp) { return this.push(new Text(title)) }