From 223184e8d3e16322fee21cc1bc2d037077d65491 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sun, 22 Oct 2023 15:36:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=BA=86=20OneBot=20?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++-- package.json | 7 +-- src/index.ts | 2 + src/push/one-bot.ts | 107 ++++++++++++++++++++++++++++++++++++++++++++ src/utils/ajax.ts | 2 +- 5 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 src/push/one-bot.ts diff --git a/README.md b/README.md index 8ac6b80..8af825e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@

-> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord 等多种推送方式。 +> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot 等多种推送方式。 > > 温馨提示:出于安全考虑, **所有** 推送方式请在 **服务端** 使用!请勿在 **客户端(网页端)** 使用!网页端使用还将额外产生跨域问题。 @@ -118,12 +118,18 @@ const DISCORD_WEBHOOK = 'https://discord.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxxx const DISCORD_USERNAME = 'Discord Bot' const discord = new Discord(DISCORD_WEBHOOK, DISCORD_USERNAME) discord.send('你好,我很可爱 - Discord') + +// 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) ``` **代理支持** | 环境变量 | 作用 | 例子 | -| ----------- | ----------------------------------- | ---------------------- | +|-------------|-------------------------------------|------------------------| | NO_PROXY | 设置是否禁用代理 | true | | HTTP_PROXY | 设置 http 代理 | http://127.0.0.1:8101 | | HTTPS_PROXY | 设置 https 代理 | http://127.0.0.1:8101 | @@ -135,7 +141,7 @@ discord.send('你好,我很可爱 - Discord') // 在 nodejs 项目中可通过直接设置环境变量来设置代理 process.env.HTTP_PROXY = 'http://127.0.0.1:8101' // 当请求是 http 的时候走 HTTP_PROXY process.env.HTTPS_PROXY = 'http://127.0.0.1:8101' // 当请求是 https 的时候走 HTTPS_PROXY -process.env.SOCKS_PROXY = 'socks://127.0.0.1:8100' // 当 HTTP_PROXY 和 HTTPS_PROXY 均为设置时走 SOCKS_PROXY +process.env.SOCKS_PROXY = 'socks://127.0.0.1:8100' // 当 HTTP_PROXY 和 HTTPS_PROXY 均未设置时走 SOCKS_PROXY // process.env.NO_PROXY = true // 设置 NO_PROXY 可禁用代理 ``` diff --git a/package.json b/package.json index 2068a92..04acbbc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "push-all-in-one", "version": "3.3.0", - "description": "Push All In One!支持 Server酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer 等多种推送方式", + "description": "Push All In One!支持 Server酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot 等多种推送方式", "author": "CaoMeiYouRen", "license": "MIT", "main": "dist/index.js", @@ -37,7 +37,8 @@ "xi-zhi", "PushDeer", "pushdeer", - "Discord" + "Discord", + "OneBot" ], "scripts": { "lint": "cross-env NODE_ENV=production eslint src *.js --fix --ext .ts,.js", @@ -139,4 +140,4 @@ "git add" ] } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 99256ac..56e64b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ export * from './push/custom-email' export * from './push/dingtalk' export * from './push/discord' export * from './push/i-got' +export * from './push/one-bot' export * from './push/push-deer' export * from './push/push-plus' export * from './push/qmsg' @@ -10,3 +11,4 @@ export * from './push/telegram' export * from './push/wechat-app' export * from './push/wechat-robot' export * from './push/xi-zhi' + diff --git a/src/push/one-bot.ts b/src/push/one-bot.ts new file mode 100644 index 0000000..3904619 --- /dev/null +++ b/src/push/one-bot.ts @@ -0,0 +1,107 @@ +import { AxiosResponse } from 'axios' +import debug from 'debug' +import { Send } from '@/interfaces/send' +import { ajax } from '@/utils/ajax' +import { warn } from '@/utils/helper' + +const Debugger = debug('push:one-bot') + +export type OneBotMsgType = 'private' | 'group' + +export interface OneBotData { + ClassType: string + message_id: number +} +export interface OneBotResponse { + status: string + retcode: number + data: OneBotData + echo?: any +} + +/** + * OneBot。官方文档:https://github.com/botuniverse/onebot-11 + * 本项目实现的版本为 OneBot 11 + * @author CaoMeiYouRen + * @date 2023-10-22 + * @export + * @class OneBot + */ +export class OneBot implements Send { + + /** + * OneBot 协议版本号 + * + * @author CaoMeiYouRen + * @date 2023-10-22 + * @static + */ + static version = 11 + + /** + * OneBot HTTP 基础路径 + * + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + * @example http://127.0.0.1 + */ + private ONE_BOT_BASE_URL: string + /** + * OneBot AccessToken + * 出于安全原因,请务必设置 AccessToken + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + */ + private ONE_BOT_ACCESS_TOKEN?: string + + /** + * Creates an instance of OneBot. + * @author CaoMeiYouRen + * @date 2023-10-22 + * @param ONE_BOT_BASE_URL OneBot HTTP 基础路径 + * @param [ONE_BOT_ACCESS_TOKEN] 出于安全原因,请务必设置 AccessToken + * + */ + constructor(ONE_BOT_BASE_URL: string, ONE_BOT_ACCESS_TOKEN?: string) { + this.ONE_BOT_BASE_URL = ONE_BOT_BASE_URL + this.ONE_BOT_ACCESS_TOKEN = ONE_BOT_ACCESS_TOKEN + Debugger('set ONE_BOT_BASE_URL: "%s", ONE_BOT_ACCESS_TOKEN: "%s"', ONE_BOT_BASE_URL, ONE_BOT_ACCESS_TOKEN) + if (!this.ONE_BOT_BASE_URL) { + throw new Error('ONE_BOT_BASE_URL 是必须的!') + } + if (!this.ONE_BOT_ACCESS_TOKEN) { + warn('未提供 ONE_BOT_ACCESS_TOKEN !出于安全原因,请务必设置 AccessToken!') + } + } + + /** + * + * @author CaoMeiYouRen + * @date 2023-10-22 + * @param message 要发送的消息 + * @param msgType 消息类型 + * @param recieverId 用户/群组 ID,即 QQ 号或群号 + */ + async send(message: string, msgType: OneBotMsgType, recieverId: number): Promise> { + Debugger('message: "%s", msgType: "%s", recieverId: "%s"', message, msgType, recieverId) + return ajax({ + baseURL: this.ONE_BOT_BASE_URL, + url: '/send_msg', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.ONE_BOT_ACCESS_TOKEN}`, + }, + data: { + auto_escape: true, + message_type: msgType, + message, + group_id: msgType === 'group' ? recieverId : '', + user_id: msgType === 'private' ? recieverId : '', + }, + }) + } + +} diff --git a/src/utils/ajax.ts b/src/utils/ajax.ts index 58bc6b5..e991bf0 100644 --- a/src/utils/ajax.ts +++ b/src/utils/ajax.ts @@ -24,7 +24,7 @@ interface AjaxConfig { * @param config * @returns */ -export async function ajax(config: AjaxConfig): Promise> { +export async function ajax(config: AjaxConfig): Promise> { try { Debugger('ajax config: %O', config) const { url, query = {}, method = 'GET', baseURL = '' } = config