Skip to content

Commit

Permalink
refactor: 重构 OneBot 推送为 新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 2c30bc6 commit b636613
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/push/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ export class Discord implements Send {
* @param [desp] 消息的描述。最多 2000 个字符
* @param [option] 额外选项
*/
async send(title: string, desp: string = '', option?: DiscordOption): Promise<SendResponse<DiscordResponse>> {
async send(title: string, desp?: string, option?: DiscordOption): Promise<SendResponse<DiscordResponse>> {
Debugger('title: "%s", desp: "%s", option: %o', title, desp, option)
const { username, avatar_url, ...args } = option || {}
const proxyUrl = this.proxyUrl
const content = `${title}\n${desp}`.trim()
const content = `${title}${desp ? `\n${desp}` : ''}`
return ajax({
url: this.DISCORD_WEBHOOK,
method: 'POST',
Expand Down
3 changes: 1 addition & 2 deletions src/push/i-got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ export class IGot implements Send {
* @author CaoMeiYouRen
* @date 2021-03-03
* @param title 请求标题
* @param [content] 请求正文
* @param [url] 推送携带的url
* @param [desp] 请求正文
* @param [option] 额外选项
* @returns
*/
Expand Down
66 changes: 55 additions & 11 deletions src/push/one-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,54 @@ const Debugger = debug('push:one-bot')

export type OneBotMsgType = 'private' | 'group'

export interface OneBotConfig {
/**
* OneBot HTTP 基础路径
*/
ONE_BOT_BASE_URL: string
/**
* OneBot AccessToken
* 出于安全原因,请务必设置 AccessToken
*/
ONE_BOT_ACCESS_TOKEN?: string
}

export interface PrivateMsgOption {
/**
* 消息类型
*/
message_type: 'private'
/**
* 对方 QQ 号
*/
user_id: number
}

export interface GroupMsgOption {
/**
* 消息类型
*/
message_type: 'group'
/**
* 群号
*/
group_id: number

}

export type OneBotOption = (PrivateMsgOption | GroupMsgOption) & {
/**
* 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效
*/
auto_escape?: boolean
}

export interface OneBotData {
ClassType: string
// 消息 ID
message_id: number
}

export interface OneBotResponse {
status: string
retcode: number
Expand Down Expand Up @@ -57,14 +101,13 @@ export class OneBot implements Send {
private ONE_BOT_ACCESS_TOKEN?: string

/**
* Creates an instance of OneBot.
* 创建 OneBot 实例
* @author CaoMeiYouRen
* @date 2023-10-22
* @param ONE_BOT_BASE_URL OneBot HTTP 基础路径
* @param [ONE_BOT_ACCESS_TOKEN] 出于安全原因,请务必设置 AccessToken
*
* @date 2024-11-08
* @param config OneBot 配置
*/
constructor(ONE_BOT_BASE_URL: string, ONE_BOT_ACCESS_TOKEN?: string) {
constructor(config: OneBotConfig) {
const { ONE_BOT_BASE_URL, ONE_BOT_ACCESS_TOKEN } = config
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)
Expand All @@ -84,8 +127,10 @@ export class OneBot implements Send {
* @param msgType 消息类型
* @param recieverId 用户/群组 ID,即 QQ 号或群号
*/
async send(message: string, msgType: OneBotMsgType, recieverId: number): Promise<SendResponse<OneBotResponse>> {
Debugger('message: "%s", msgType: "%s", recieverId: "%s"', message, msgType, recieverId)
async send(title: string, desp?: string, option?: OneBotOption): Promise<SendResponse<OneBotResponse>> {
Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option)
const { message_type, ...args } = option || {}
const message = `${title}${desp ? `\n${desp}` : ''}`
return ajax<OneBotResponse>({
baseURL: this.ONE_BOT_BASE_URL,
url: '/send_msg',
Expand All @@ -96,10 +141,9 @@ export class OneBot implements Send {
},
data: {
auto_escape: false,
message_type: msgType,
message_type,
message,
group_id: msgType === 'group' ? recieverId : '',
user_id: msgType === 'private' ? recieverId : '',
...args,
},
})
}
Expand Down
1 change: 0 additions & 1 deletion src/push/push-plus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AxiosResponse } from 'axios'
import debug from 'debug'
import { Send } from '../interfaces/send'
import { ajax } from '@/utils/ajax'
Expand Down

0 comments on commit b636613

Please sign in to comment.