From 1f481bf8f37d1cb62754f6f7058c9442bde7081e Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sat, 9 Nov 2024 17:17:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E6=A1=A3=EF=BC=9B=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=EF=BC=9B=E4=BC=98=E5=8C=96=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=9A=84=E7=B1=BB=E5=9E=8B=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 103 ++++++++++++++------- examples/01-example.ts | 149 ++++++++++++++++++++---------- examples/02-example.ts | 203 +++++++++-------------------------------- src/interfaces/send.ts | 12 +-- src/push/dingtalk.ts | 2 +- src/push/discord.ts | 4 +- src/push/push-plus.ts | 8 +- src/push/xi-zhi.ts | 2 +- 8 files changed, 227 insertions(+), 256 deletions(-) diff --git a/README.md b/README.md index 0861e2c..0e9951c 100644 --- a/README.md +++ b/README.md @@ -51,19 +51,29 @@ npm i push-all-in-one -S ## 👨‍💻 使用 +所有推送方式均实现了 `send(title: string, desp?: string, options?: any):` 方法。 + +`title` 为 `消息标题`,`desp` 为 `消息描述`,`options` 为该推送方式的`额外推送选项`。具体请参考各个推送渠道的注释。 + +调用方式举例: + ```ts import { ServerChanTurbo, ServerChanV3, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram } from 'push-all-in-one' // Server酱·Turbo。官方文档:https://sct.ftqq.com/r/13172 const SCTKEY = 'SCTxxxxxxxxxxxxxxxxxxx' -const serverChanTurbo = new ServerChanTurbo(SCTKEY) -serverChanTurbo.send('你好', '你好,我很可爱') +const serverChanTurbo = new ServerChanTurbo({ + SERVER_CHAN_TURBO_SENDKEY: SCTKEY, +}) +serverChanTurbo.send('你好', '你好,我很可爱 - Server酱·Turbo', {}) // 【推荐】Server酱³ // Server酱3。官方文档:https://sc3.ft07.com/doc -const SERVER_CHAN_V3_KEY = 'sctpXXXXXXXXXXXXXXXXXXXXXXXX' -const serverChanV3 = new ServerChanV3(SERVER_CHAN_V3_KEY) -serverChanV3.send('你好', '你好,我很可爱 - 来自 Server酱³') +const SERVER_CHAN_V3_SENDKEY = 'sctpXXXXXXXXXXXXXXXXXXXXXXXX' +const serverChanV3 = new ServerChanV3({ + SERVER_CHAN_V3_SENDKEY, +}) +serverChanV3.send('你好', '你好,我很可爱 - Server酱³', {}) // 【推荐】自定义邮件,基于 nodemailer 实现,官方文档: https://github.com/nodemailer/nodemailer const customEmail = new CustomEmail({ @@ -74,80 +84,109 @@ const customEmail = new CustomEmail({ EMAIL_HOST: 'smtp.qq.com', EMAIL_PORT: 465, }) -customEmail.send('你好', '你好,我很可爱 - 自定义邮件') +customEmail.send('你好', '你好,我很可爱 - 自定义邮件', {}) // 【推荐】钉钉机器人。官方文档:https://developers.dingtalk.com/document/app/custom-robot-access -const ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxx' -const SECRET = 'SECxxxxxxxxxxxxxxxx' -const dingtalk = new Dingtalk(ACCESS_TOKEN, SECRET) -dingtalk.send('你好', '你好,我很可爱') +const DINGTALK_ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxx' +const DINGTALK_SECRET = 'SECxxxxxxxxxxxxxxxx' +const dingtalk = new Dingtalk({ + DINGTALK_ACCESS_TOKEN, + DINGTALK_SECRET, +}) +dingtalk.send('你好', '你好,我很可爱 - 钉钉机器人') // 企业微信群机器人。官方文档:https://developer.work.weixin.qq.com/document/path/91770 // 企业微信群机器人的使用需要两人以上加入企业,如果个人使用微信推送建议使用 企业微信应用+微信插件 推送。虽然需要配置的内容更多了,但是无需下载企业微信,网页端即可完成操作。 -const WX_ROBOT_KEY = 'xxxxxxxxxxxxxxxxxxxxxxx' -const wechatRobot = new WechatRobot(WX_ROBOT_KEY) -wechatRobot.send('你好,我很可爱', 'text') +const WECHAT_ROBOT_KEY = 'xxxxxxxxxxxxxxxxxxxxxxx' +const wechatRobot = new WechatRobot({ + WECHAT_ROBOT_KEY, +}) +wechatRobot.send('你好,我很可爱- 企业微信群机器人', '', { msgtype: 'text' }) // 【推荐】企业微信应用推送,官方文档:https://developer.work.weixin.qq.com/document/path/90664 // 微信插件 https://work.weixin.qq.com/wework_admin/frame#profile/wxPlugin // 参数的介绍请参考:https://developer.work.weixin.qq.com/document/path/90665 // 支持 text 和 markdown 格式,但 markdown 格式仅可在企业微信中查看 const wechatApp = new WechatApp({ - WX_APP_CORPID: 'wwxxxxxxxxxxxxxxxxxxxx', - WX_APP_AGENTID: 10001, // 请更换为自己的 AGENTID - WX_APP_SECRET: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', - WX_APP_USERID: '@all', + WECHAT_APP_CORPID: 'wwxxxxxxxxxxxxxxxxxxxx', + WECHAT_APP_AGENTID: 10001, // 请更换为自己的 AGENTID + WECHAT_APP_SECRET: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', +}) +wechatApp.send('你好,我很可爱 - 企业微信应用推送', '', { + msgtype: 'text', + touser: '@all', }) -wechatApp.send('你好,我很可爱', 'text') // pushplus 推送,官方文档:https://www.pushplus.plus/doc/ const PUSH_PLUS_TOKEN = 'xxxxxxxxxxxxxxxxxxxxx' -const pushplus = new PushPlus(PUSH_PLUS_TOKEN) -pushplus.send('你好', '你好,我很可爱') +const pushplus = new PushPlus({ PUSH_PLUS_TOKEN }) +pushplus.send('你好', '你好,我很可爱 - PushPlus', { + template: 'html', + channel: 'wechat', +}) // iGot 推送,官方文档:http://hellyw.com/#/ const I_GOT_KEY = 'xxxxxxxxxx' -const iGot = new IGot(I_GOT_KEY) -iGot.send('你好', '你好,我很可爱', 'https://github.com/CaoMeiYouRen/push-all-in-one') +const iGot = new IGot({ I_GOT_KEY }) +iGot.send('你好', '你好,我很可爱 - iGot', { + url: 'https://github.com/CaoMeiYouRen/push-all-in-one', + topic: 'push-all-in-one', +}) // Qmsg 酱 推送,官方文档:https://qmsg.zendee.cn const QMSG_KEY = 'xxxxxxxxxxxx' -const qmsg = new Qmsg(QMSG_KEY) -qmsg.send('你好,我很可爱 - Qmsg', '12345,12346', 'send') // msg:要推送的消息内容;qq:指定要接收消息的QQ号或者QQ群,多个以英文逗号分割,例如:12345,12346 +const qmsg = new Qmsg({ QMSG_KEY }) +qmsg.send('你好,我很可爱 - Qmsg', '', { + type: 'send', + qq: '123456,654321', +}) // msg:要推送的消息内容;qq:指定要接收消息的QQ号或者QQ群,多个以英文逗号分割,例如:12345,12346 // 息知 推送,官方文档:https://xz.qqoq.net/#/index const XI_ZHI_KEY = 'xxxxxxxxxxxxx' -const xiZhi = new XiZhi(XI_ZHI_KEY) +const xiZhi = new XiZhi({ XI_ZHI_KEY }) xiZhi.send('你好', '你好,我很可爱 - XiZhi') // PushDeer 推送,官方文档:https://github.com/easychen/pushdeer const PUSH_DEER_PUSH_KEY = 'xxxxxxxxxx' -const pushDeer = new PushDeer(PUSH_DEER_PUSH_KEY) -pushDeer.send('你好', '你好,我很可爱 - PushDeer', 'markdown') +const pushDeer = new PushDeer({ PUSH_DEER_PUSH_KEY }) +pushDeer.send('你好', '你好,我很可爱 - PushDeer', { + type: 'markdown', +}) // 【推荐】Discord Webhook 推送,官方文档:https://support.discord.com/hc/zh-tw/articles/228383668-%E4%BD%BF%E7%94%A8%E7%B6%B2%E7%B5%A1%E9%89%A4%E6%89%8B-Webhooks- const DISCORD_WEBHOOK = 'https://discord.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxxxxxxxx' const DISCORD_USERNAME = 'My Bot' -const discord = new Discord(DISCORD_WEBHOOK, DISCORD_USERNAME) +const PROXY_URL = 'http://127.0.0.1:8101' +const discord = new Discord({ DISCORD_WEBHOOK, PROXY_URL }) // Discord 也支持以下方式添加代理地址 // discord.proxyUrl = 'http://127.0.0.1:8101' -discord.send('你好,我很可爱 - Discord') +discord.send('你好,我很可爱 - Discord', '', { + username: DISCORD_USERNAME, +}) // 【推荐】Telegram Bot 推送。官方文档:https://core.telegram.org/bots/api#making-requests const telegram = new Telegram({ TELEGRAM_BOT_TOKEN: '111111:xxxxxxxxxxxxxx', TELEGRAM_CHAT_ID: 100000, + // PROXY_URL: 'http://127.0.0.1:8101', }) // Telegram 也支持以下方式添加代理地址 // telegram.proxyUrl = 'http://127.0.0.1:8101' -telegram.send('你好,我很可爱 - Telegram') +telegram.send('你好,我很可爱 - Telegram', '', { + disable_notification: true, +}) // OneBot 推送。官方文档:https://github.com/botuniverse/onebot-11 // 本项目实现的版本为 OneBot 11 // 在 mirai 环境下实现的插件版本可参考:https://github.com/yyuueexxiinngg/onebot-kotlin -const oneBot = new OneBot('http://127.0.0.1:5700', 'xxxxxxxxxxx') -oneBot.send('你好,我很可爱 - OneBot 11', 'private', 10001) +const ONE_BOT_BASE_URL = 'http://127.0.0.1:5700' +const ONE_BOT_ACCESS_TOKEN = 'xxxxxxxxxxx' +const oneBot = new OneBot({ ONE_BOT_BASE_URL, ONE_BOT_ACCESS_TOKEN }) +oneBot.send('你好,我很可爱 - OneBot 11', '', { + message_type: 'private', + user_id: 123456789, +}) ``` 更多例子请参考 [examples](https://github.com/CaoMeiYouRen/push-all-in-one/tree/master/examples) diff --git a/examples/01-example.ts b/examples/01-example.ts index 28868b2..7e5bfd4 100644 --- a/examples/01-example.ts +++ b/examples/01-example.ts @@ -1,7 +1,8 @@ import { AxiosResponse } from 'axios' import colors from '@colors/colors' -import { ServerChanTurbo, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram, MsgType, TemplateType, CustomEmailType, OneBotMsgType } from '../src' +import { ServerChanTurbo, ServerChanV3, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram, PushPlusTemplateType, CustomEmailType, OneBotMsgType, WechatRobotMsgType, WechatAppMsgType, PushPlusChannelType } from '../src' import { warn } from '../src/utils/helper' +import { SendResponse } from '../src/interfaces/response' export function info(text: any): void { // eslint-disable-next-line no-console @@ -17,138 +18,190 @@ export function info(text: any): void { * @param title * @param [desp] */ -export async function batchPushAllInOne(title: string, desp?: string): Promise>[]> { +export async function batchPushAllInOne(title: string, desp?: string): Promise>[]> { + const env = process.env const pushs: Promise>[] = [] - if (process.env.SCTKEY) { + if (env.SERVER_CHAN_TURBO_SENDKEY) { // Server酱。官方文档:https://sct.ftqq.com/ - const serverChanTurbo = new ServerChanTurbo(process.env.SCTKEY) - pushs.push(serverChanTurbo.send(title, desp)) + const serverChanTurbo = new ServerChanTurbo({ + SERVER_CHAN_TURBO_SENDKEY: env.SERVER_CHAN_TURBO_SENDKEY, + }) + pushs.push(serverChanTurbo.send(title, desp, {})) info('Server酱·Turbo 已加入推送队列') } else { info('未配置 Server酱·Turbo,已跳过') } - if (process.env.EMAIL_AUTH_USER && process.env.EMAIL_AUTH_PASS && process.env.EMAIL_HOST && process.env.EMAIL_PORT && process.env.EMAIL_TO_ADDRESS) { + if (env.SERVER_CHAN_V3_SENDKEY) { + const serverChanV3 = new ServerChanV3({ + SERVER_CHAN_V3_SENDKEY: env.SERVER_CHAN_V3_SENDKEY, + }) + pushs.push(serverChanV3.send(title, desp, {})) + info('Server酱³ 已加入推送队列') + } else { + info('未配置 Server酱³,已跳过') + } + + if (env.EMAIL_AUTH_USER && env.EMAIL_AUTH_PASS && env.EMAIL_HOST && env.EMAIL_PORT && env.EMAIL_TO_ADDRESS) { // 自定义邮件,基于 nodemailer 实现,官方文档: https://github.com/nodemailer/nodemailer const customEmail = new CustomEmail({ - EMAIL_TYPE: process.env.EMAIL_TYPE as CustomEmailType, - EMAIL_TO_ADDRESS: process.env.EMAIL_TO_ADDRESS, - EMAIL_AUTH_USER: process.env.EMAIL_AUTH_USER, - EMAIL_AUTH_PASS: process.env.EMAIL_AUTH_PASS, - EMAIL_HOST: process.env.EMAIL_HOST, - EMAIL_PORT: Number(process.env.EMAIL_PORT), + EMAIL_TYPE: env.EMAIL_TYPE as CustomEmailType, + EMAIL_TO_ADDRESS: env.EMAIL_TO_ADDRESS, + EMAIL_AUTH_USER: env.EMAIL_AUTH_USER, + EMAIL_AUTH_PASS: env.EMAIL_AUTH_PASS, + EMAIL_HOST: env.EMAIL_HOST, + EMAIL_PORT: Number(env.EMAIL_PORT), }) - pushs.push(customEmail.send(title, desp).then((result) => ({ data: result.response, status: 200, statusText: 'OK', headers: {}, config: {} }))) + pushs.push(customEmail.send(title, desp)) info('自定义邮件 已加入推送队列') } else { info('未配置 自定义邮件,已跳过') } - if (process.env.DINGTALK_ACCESS_TOKEN) { + if (env.DINGTALK_ACCESS_TOKEN) { // 钉钉机器人。官方文档:https://developers.dingtalk.com/document/app/custom-robot-access - const dingtalk = new Dingtalk(process.env.DINGTALK_ACCESS_TOKEN, process.env.DINGTALK_SECRET) + const dingtalk = new Dingtalk({ + DINGTALK_ACCESS_TOKEN: env.DINGTALK_ACCESS_TOKEN, + DINGTALK_SECRET: env.DINGTALK_SECRET, + }) pushs.push(dingtalk.send(title, desp)) info('钉钉机器人 已加入推送队列') } else { info('未配置 钉钉机器人,已跳过') } - if (process.env.WX_ROBOT_KEY) { + if (env.WECHAT_ROBOT_KEY) { // 企业微信群机器人。官方文档:https://work.weixin.qq.com/help?person_id=1&doc_id=13376 // 企业微信群机器人的使用需要两人以上加入企业,如果个人使用微信推送建议使用 企业微信应用+微信插件 推送 - const wechatRobot = new WechatRobot(process.env.WX_ROBOT_KEY) - pushs.push(wechatRobot.send(`${title}\n${desp}`, process.env.WX_ROBOT_MSG_TYPE as MsgType)) + const wechatRobot = new WechatRobot({ + WECHAT_ROBOT_KEY: env.WECHAT_ROBOT_KEY, + }) + pushs.push(wechatRobot.send(title, desp, { + msgtype: env.WECHAT_ROBOT_MSG_TYPE as WechatRobotMsgType, + })) info('企业微信群机器人 已加入推送队列') } else { info('未配置 企业微信群机器人,已跳过') } - if (process.env.WX_APP_CORPID && process.env.WX_APP_AGENTID && process.env.WX_APP_SECRET) { + if (env.WECHAT_APP_CORPID && env.WECHAT_APP_AGENTID && env.WECHAT_APP_SECRET) { // 企业微信应用推送,官方文档:https://work.weixin.qq.com/api/doc/90000/90135/90664 const wechatApp = new WechatApp({ - WX_APP_CORPID: process.env.WX_APP_CORPID, - WX_APP_AGENTID: Number(process.env.WX_APP_AGENTID), - WX_APP_SECRET: process.env.WX_APP_SECRET, - WX_APP_USERID: process.env.WX_APP_USERID, + WECHAT_APP_CORPID: env.WECHAT_APP_CORPID, + WECHAT_APP_AGENTID: Number(env.WECHAT_APP_AGENTID), + WECHAT_APP_SECRET: env.WECHAT_APP_SECRET, }) - pushs.push(wechatApp.send(`${title}\n${desp}`)) + pushs.push(wechatApp.send(title, desp, { + msgtype: env.WECHAT_APP_MSG_TYPE as WechatAppMsgType, + touser: env.WECHAT_APP_USERID || '@all', + })) info('企业微信应用推送 已加入推送队列') } else { info('未配置 企业微信应用推送,已跳过') } - if (process.env.PUSH_PLUS_TOKEN) { + if (env.PUSH_PLUS_TOKEN) { // pushplus 推送,官方文档:http://pushplus.hxtrip.com/doc/ - const pushplus = new PushPlus(process.env.PUSH_PLUS_TOKEN) - pushs.push(pushplus.send(title, desp, process.env.PUSH_PLUS_TEMPLATE_TYPE as TemplateType)) + const pushplus = new PushPlus({ + PUSH_PLUS_TOKEN: env.PUSH_PLUS_TOKEN, + }) + pushs.push(pushplus.send(title, desp, { + template: env.PUSH_PLUS_TEMPLATE as PushPlusTemplateType || 'html', + channel: env.PUSH_PLUS_CHANNEL as PushPlusChannelType || 'wechat', + })) info('pushplus 推送 已加入推送队列') } else { info('未配置 pushplus 推送,已跳过') } - if (process.env.I_GOT_KEY) { + if (env.I_GOT_KEY) { // iGot 推送,官方文档:https://wahao.github.io/Bark-MP-helper - const iGot = new IGot(process.env.I_GOT_KEY) - pushs.push(iGot.send(title, desp)) + const iGot = new IGot({ + I_GOT_KEY: env.I_GOT_KEY, + }) + pushs.push(iGot.send(title, desp, {})) info('iGot 推送 已加入推送队列') } else { info('未配置 iGot 推送,已跳过') } - if (process.env.QMSG_KEY) { + if (env.QMSG_KEY) { // Qmsg 酱 推送,官方文档:https://qmsg.zendee.cn - const qmsg = new Qmsg(process.env.QMSG_KEY) - pushs.push(qmsg.send(title, desp)) + const qmsg = new Qmsg({ + QMSG_KEY: env.QMSG_KEY, + }) + pushs.push(qmsg.send(title, desp || '', { + type: 'send', + qq: env.QMSG_QQ || '', + })) info('Qmsg 推送 已加入推送队列') } else { info('未配置 Qmsg 推送,已跳过') } - if (process.env.XI_ZHI_KEY) { + if (env.XI_ZHI_KEY) { // 息知 推送,官方文档:https://xz.qqoq.net/#/index - const xiZhi = new XiZhi(process.env.XI_ZHI_KEY) + const xiZhi = new XiZhi({ + XI_ZHI_KEY: env.XI_ZHI_KEY, + }) pushs.push(xiZhi.send(title, desp)) info('XiZhi 推送 已加入推送队列') } else { info('未配置 XiZhi 推送,已跳过') } - if (process.env.PUSH_DEER_PUSH_KEY) { + if (env.PUSH_DEER_PUSH_KEY) { // 【推荐】PushDeer 推送,官方文档:https://github.com/easychen/pushdeer - const pushDeer = new PushDeer(process.env.PUSH_DEER_PUSH_KEY) - pushs.push(pushDeer.send(title, desp)) + const pushDeer = new PushDeer({ + PUSH_DEER_PUSH_KEY: env.PUSH_DEER_PUSH_KEY, + }) + pushs.push(pushDeer.send(title, desp, { + type: 'markdown', + })) info('PushDeer 推送 已加入推送队列') } else { info('未配置 PushDeer 推送,已跳过') } - if (process.env.DISCORD_WEBHOOK) { + if (env.DISCORD_WEBHOOK) { // 【推荐】Discord Webhook 推送,官方文档:https://support.discord.com/hc/zh-tw/articles/228383668-%E4%BD%BF%E7%94%A8%E7%B6%B2%E7%B5%A1%E9%89%A4%E6%89%8B-Webhooks- - const discord = new Discord(process.env.DISCORD_WEBHOOK, process.env.DISCORD_USERNAME) - pushs.push(discord.send(`${title}\n${desp}`)) + const discord = new Discord({ + DISCORD_WEBHOOK: env.DISCORD_WEBHOOK, + }) + pushs.push(discord.send(title, desp, { + username: env.DISCORD_USERNAME, + })) info('Discord 推送 已加入推送队列') } else { info('未配置 Discord 推送,已跳过') } - if (process.env.TELEGRAM_BOT_TOKEN && process.env.TELEGRAM_CHAT_ID) { + if (env.TELEGRAM_BOT_TOKEN && env.TELEGRAM_CHAT_ID) { // 【推荐】Telegram Bot 推送。官方文档:https://core.telegram.org/bots/api#making-requests const telegram = new Telegram({ - TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN, - TELEGRAM_CHAT_ID: Number(process.env.TELEGRAM_CHAT_ID), + TELEGRAM_BOT_TOKEN: env.TELEGRAM_BOT_TOKEN, + TELEGRAM_CHAT_ID: Number(env.TELEGRAM_CHAT_ID), }) - pushs.push(telegram.send(`${title}\n${desp}`)) + pushs.push(telegram.send(title, desp, { + disable_notification: false, + })) info('Telegram 推送 已加入推送队列') } else { info('未配置 Telegram 推送,已跳过') } - if (process.env.ONE_BOT_BASE_URL && process.env.ONE_BOT_ACCESS_TOKEN) { + if (env.ONE_BOT_BASE_URL && env.ONE_BOT_ACCESS_TOKEN) { // OneBot 推送。官方文档:https://github.com/botuniverse/onebot-11 // 本项目实现的版本为 OneBot 11 // 在 mirai 环境下实现的插件版本可参考:https://github.com/yyuueexxiinngg/onebot-kotlin - const oneBot = new OneBot(process.env.ONE_BOT_BASE_URL, process.env.ONE_BOT_ACCESS_TOKEN) - pushs.push(oneBot.send(`${title}\n${desp}`, process.env.ONE_BOT_MSG_TYPE as OneBotMsgType, Number(process.env.ONE_BOT_RECIEVER_ID))) + const oneBot = new OneBot({ + ONE_BOT_BASE_URL: env.ONE_BOT_BASE_URL, + ONE_BOT_ACCESS_TOKEN: env.ONE_BOT_ACCESS_TOKEN, + }) + pushs.push(oneBot.send(title, desp || '', { + message_type: 'group', + group_id: Number(env.ONE_BOT_GROUP_ID), + })) info('Discord 推送 已加入推送队列') } else { info('未配置 Discord 推送,已跳过') diff --git a/examples/02-example.ts b/examples/02-example.ts index 14f3b14..551bc67 100644 --- a/examples/02-example.ts +++ b/examples/02-example.ts @@ -1,81 +1,35 @@ -import { AxiosResponse } from 'axios' -import { ServerChanTurbo, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram, MsgType, TemplateType, CustomEmailType, OneBotMsgType, ChannelType, PushDeerPushType, QmsgPushType } from '../src' +import { ServerChanTurbo, ServerChanV3, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram } from '../src' +import { SendResponse } from '../src/interfaces/response' -export type PushConfig = Required<{ - ServerChanTurbo?: { - SCTKEY: string - } - Dingtalk?: { - ACCESS_TOKEN: string - SECRET?: string - } - CustomEmail?: { - EMAIL_TYPE: CustomEmailType // 邮件类型 - EMAIL_TO_ADDRESS: string // 收件人 - EMAIL_AUTH_USER: string // 发件邮箱 - EMAIL_AUTH_PASS: string // 密码/授权码 - EMAIL_HOST: string // 发件域名 - EMAIL_PORT: number // 发件端口 - } - WechatRobot?: { - WX_ROBOT_KEY: string - MSG_TYPE: MsgType - } - WechatApp?: { - WX_APP_CORPID: string - WX_APP_AGENTID: number - WX_APP_SECRET: string - WX_APP_USERID: string - } - PushPlus?: { - PUSH_PLUS_TOKEN: string - TEMPLATE_TYPE?: TemplateType - CHANNEL_TYPE?: ChannelType - } - IGot?: { - I_GOT_KEY: string - } - Qmsg?: { - QMSG_KEY: string - QMSG_QQ: string - QMSG_PUSH_TYPE: QmsgPushType - } - XiZhi?: { - XI_ZHI_KEY: string - } - PushDeer?: { - PUSH_DEER_PUSH_KEY: string - PUSH_DEER_ENDPOINT?: string - PUSH_DEER_PUSH_TYPE?: PushDeerPushType - } - Discord?: { - DISCORD_WEBHOOK: string - DISCORD_USERNAME: string - } - Telegram?: { - TELEGRAM_BOT_TOKEN: string - TELEGRAM_CHAT_ID: number - TELEGRAM_SEND_SILENTLY?: boolean - TELEGRAM_PROTECT_CONTENT?: boolean - TELEGRAM_MESSAGE_THREAD_ID?: string - } - OneBot?: { - ONE_BOT_BASE_URL: string - ONE_BOT_ACCESS_TOKEN?: string - ONE_BOT_MSG_TYPE: OneBotMsgType - ONE_BOT_RECIEVER_ID: number - } -}> +// 获取 每个 推送渠道的构造函数的 类型 + +export const PushAllInOne = { + ServerChanTurbo, + ServerChanV3, + CustomEmail, + Dingtalk, + WechatRobot, + WechatApp, + PushPlus, + IGot, + Qmsg, + XiZhi, + PushDeer, + Discord, + OneBot, + Telegram, +} as const -export type PushType = keyof PushConfig +export type IPushAllInOne = typeof PushAllInOne -export type MetaPushConfig = { +export type PushType = keyof IPushAllInOne + +export type MetaPushConfig = { type: T - config: PushConfig[T] + config: ConstructorParameters[0] + option: Parameters[2] } -export declare type PushAllInOneConfig = T extends any ? MetaPushConfig : never - /** * 从传入变量中读取配置,并选择一个渠道推送 * @@ -86,95 +40,20 @@ export declare type PushAllInOneConfig = T extend * @param desp * @param pushConfig */ -export async function runPushAllInOne(title: string, desp: string, pushConfig: PushAllInOneConfig): Promise> { - switch (pushConfig.type) { - case 'ServerChanTurbo': { - const { SCTKEY } = pushConfig.config - const serverChanTurbo = new ServerChanTurbo(SCTKEY) - return serverChanTurbo.send(title, desp) - } - case 'CustomEmail': { - const customEmail = new CustomEmail(pushConfig.config) - const result = await customEmail.send(title, desp) - const response = { data: result.response, status: 200, statusText: 'OK', headers: {}, config: {} } - return response - } - case 'Dingtalk': { - const { ACCESS_TOKEN, SECRET } = pushConfig.config - const dingtalk = new Dingtalk(ACCESS_TOKEN, SECRET) - const response = await dingtalk.send(title, desp) - return response - } - case 'WechatRobot': { - const { WX_ROBOT_KEY, MSG_TYPE } = pushConfig.config - const wechatRobot = new WechatRobot(WX_ROBOT_KEY) - const response = await wechatRobot.send(`${title}\n${desp}`, MSG_TYPE) - return response - } - case 'WechatApp': { - const { WX_APP_CORPID, WX_APP_AGENTID, WX_APP_SECRET, WX_APP_USERID } = pushConfig.config - const wechatApp = new WechatApp({ - WX_APP_CORPID, - WX_APP_AGENTID, - WX_APP_SECRET, - WX_APP_USERID, - }) - const response = await wechatApp.send(`${title}\n${desp}`) - return response - } - case 'PushPlus': { - const { PUSH_PLUS_TOKEN, TEMPLATE_TYPE, CHANNEL_TYPE } = pushConfig.config - const pushplus = new PushPlus(PUSH_PLUS_TOKEN) - const response = await pushplus.send(title, desp, TEMPLATE_TYPE, CHANNEL_TYPE) - return response - } - case 'IGot': { - const { I_GOT_KEY } = pushConfig.config - const iGot = new IGot(I_GOT_KEY) - const response = await iGot.send(title, desp) - return response - } - case 'Qmsg': { - const { QMSG_KEY, QMSG_QQ, QMSG_PUSH_TYPE } = pushConfig.config - const qmsg = new Qmsg(QMSG_KEY) - const response = await qmsg.send(`${title}\n${desp}`, QMSG_QQ, QMSG_PUSH_TYPE) - return response - } - case 'XiZhi': { - const { XI_ZHI_KEY } = pushConfig.config - const xiZhi = new XiZhi(XI_ZHI_KEY) - const response = await xiZhi.send(title, desp) - return response - } - case 'PushDeer': { - const { PUSH_DEER_PUSH_KEY, PUSH_DEER_ENDPOINT, PUSH_DEER_PUSH_TYPE } = pushConfig.config - const pushDeer = new PushDeer(PUSH_DEER_PUSH_KEY, PUSH_DEER_ENDPOINT) - const response = await pushDeer.send(title, desp, PUSH_DEER_PUSH_TYPE) - return response - } - case 'Discord': { - const { DISCORD_WEBHOOK, DISCORD_USERNAME } = pushConfig.config - const discord = new Discord(DISCORD_WEBHOOK, DISCORD_USERNAME) - const response = await discord.send(`${title}\n${desp}`) - return response - } - case 'Telegram': { - const { TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID } = pushConfig.config - const telegram = new Telegram({ - TELEGRAM_BOT_TOKEN, - TELEGRAM_CHAT_ID: Number(TELEGRAM_CHAT_ID), - }) - const response = await telegram.send(`${title}\n${desp}`) - return response - } - case 'OneBot': { - const { ONE_BOT_BASE_URL, ONE_BOT_ACCESS_TOKEN, ONE_BOT_MSG_TYPE, ONE_BOT_RECIEVER_ID } = pushConfig.config - const oneBot = new OneBot(ONE_BOT_BASE_URL, ONE_BOT_ACCESS_TOKEN) - const response = await oneBot.send(`${title}\n${desp}`, ONE_BOT_MSG_TYPE, Number(ONE_BOT_RECIEVER_ID)) - return response - } - default: - throw new Error('未匹配到任何推送方式!') - +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('未匹配到任何推送方式!') } + +// runPushAllInOne('测试推送', '测试推送', { +// type: 'ServerChanTurbo', +// config: { +// SERVER_CHAN_TURBO_SENDKEY: '', +// }, +// option: { +// }, +// }) diff --git a/src/interfaces/send.ts b/src/interfaces/send.ts index b661b7c..0393e34 100644 --- a/src/interfaces/send.ts +++ b/src/interfaces/send.ts @@ -16,14 +16,14 @@ export interface Send { * @date 2024-04-20 */ proxyUrl?: string - /** - * 发送新消息 + * 发送消息 * * @author CaoMeiYouRen - * @date 2024-04-20 - * @param args + * @date 2024-11-09 + * @param title 消息标题 + * @param [desp] 消息描述 + * @param [options] 发送选项 */ - send(...args: any[]): Promise - send(title: string, desp?: string, options?: T): Promise> + send(title: string, desp?: string, options?: any): Promise> } diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index 5f27c32..2cdd409 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -109,7 +109,7 @@ export class Dingtalk implements Send { * @param [desp] 消息的内容,支持 Markdown * @returns */ - async send(title: string, desp?: string): Promise> { + async send(title: string, desp?: string, option?: DingtalkOption): Promise> { Debugger('title: "%s", desp: "%s"', title, desp) if (!desp) { return this.push(new Text(title)) diff --git a/src/push/discord.ts b/src/push/discord.ts index 783893b..403678c 100644 --- a/src/push/discord.ts +++ b/src/push/discord.ts @@ -21,11 +21,11 @@ export type DiscordOption = { /** * 机器人显示的名称 */ - username: string + username?: string /** * 机器人头像的 Url */ - avatar_url: string + avatar_url?: string [key: string]: any } diff --git a/src/push/push-plus.ts b/src/push/push-plus.ts index c77c4eb..71110f0 100644 --- a/src/push/push-plus.ts +++ b/src/push/push-plus.ts @@ -5,9 +5,9 @@ import { SendResponse } from '@/interfaces/response' const Debugger = debug('push:push-plus') -export type TemplateType = 'html' | 'txt' | 'json' | 'markdown' | 'cloudMonitor' | 'jenkins' | 'route' +export type PushPlusTemplateType = 'html' | 'txt' | 'json' | 'markdown' | 'cloudMonitor' | 'jenkins' | 'route' -export type ChannelType = 'wechat' | 'webhook' | 'cp' | 'sms' | 'mail' +export type PushPlusChannelType = 'wechat' | 'webhook' | 'cp' | 'sms' | 'mail' export interface PushPlusConfig { /** @@ -20,11 +20,11 @@ export interface PushPlusOption { /** * 模板类型 */ - template: TemplateType + template?: PushPlusTemplateType /** * 渠道类型 */ - channel: ChannelType + channel?: PushPlusChannelType /** * 群组编码,不填仅发送给自己;channel为webhook时无效 */ diff --git a/src/push/xi-zhi.ts b/src/push/xi-zhi.ts index 1c2bc35..f788f75 100644 --- a/src/push/xi-zhi.ts +++ b/src/push/xi-zhi.ts @@ -39,7 +39,7 @@ export class XiZhi implements Send { } } - async send(title: string, desp?: string): Promise> { + async send(title: string, desp?: string, option?: XiZhiOption): Promise> { Debugger('title: "%s", desp: "%s"', title, desp) return ajax({ url: `https://xizhi.qqoq.net/${this.XI_ZHI_KEY}.send`,