Skip to content

Commit

Permalink
feat: 增加了 OneBot 推送支持
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Oct 22, 2023
1 parent 8a0e07f commit 223184e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</a>
</p>

> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord 等多种推送方式。
> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot 等多种推送方式。
>
> 温馨提示:出于安全考虑, **所有** 推送方式请在 **服务端** 使用!请勿在 **客户端(网页端)** 使用!网页端使用还将额外产生跨域问题。
Expand Down Expand Up @@ -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 |
Expand All @@ -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 可禁用代理
```

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -139,4 +140,4 @@
"git add"
]
}
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -10,3 +11,4 @@ export * from './push/telegram'
export * from './push/wechat-app'
export * from './push/wechat-robot'
export * from './push/xi-zhi'

107 changes: 107 additions & 0 deletions src/push/one-bot.ts
Original file line number Diff line number Diff line change
@@ -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<AxiosResponse<OneBotResponse>> {
Debugger('message: "%s", msgType: "%s", recieverId: "%s"', message, msgType, recieverId)
return ajax<OneBotResponse>({
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 : '',
},
})
}

}
2 changes: 1 addition & 1 deletion src/utils/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface AjaxConfig {
* @param config
* @returns
*/
export async function ajax(config: AjaxConfig): Promise<AxiosResponse<any>> {
export async function ajax<T = any>(config: AjaxConfig): Promise<AxiosResponse<T>> {
try {
Debugger('ajax config: %O', config)
const { url, query = {}, method = 'GET', baseURL = '' } = config
Expand Down

0 comments on commit 223184e

Please sign in to comment.