From b6366134b3fcac32d7924684f097a87a1a4ecd5e Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Fri, 8 Nov 2024 18:49:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20OneBot=20?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E4=B8=BA=20=E6=96=B0=E7=89=88=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/discord.ts | 4 +-- src/push/i-got.ts | 3 +- src/push/one-bot.ts | 66 +++++++++++++++++++++++++++++++++++-------- src/push/push-plus.ts | 1 - 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/push/discord.ts b/src/push/discord.ts index 270b101..783893b 100644 --- a/src/push/discord.ts +++ b/src/push/discord.ts @@ -78,11 +78,11 @@ export class Discord implements Send { * @param [desp] 消息的描述。最多 2000 个字符 * @param [option] 额外选项 */ - async send(title: string, desp: string = '', option?: DiscordOption): Promise> { + async send(title: string, desp?: string, option?: DiscordOption): Promise> { 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', diff --git a/src/push/i-got.ts b/src/push/i-got.ts index d700203..9d296c3 100644 --- a/src/push/i-got.ts +++ b/src/push/i-got.ts @@ -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 */ diff --git a/src/push/one-bot.ts b/src/push/one-bot.ts index 60344ab..f1d6ce1 100644 --- a/src/push/one-bot.ts +++ b/src/push/one-bot.ts @@ -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 @@ -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) @@ -84,8 +127,10 @@ export class OneBot implements Send { * @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) + async send(title: string, desp?: string, option?: OneBotOption): Promise> { + Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) + const { message_type, ...args } = option || {} + const message = `${title}${desp ? `\n${desp}` : ''}` return ajax({ baseURL: this.ONE_BOT_BASE_URL, url: '/send_msg', @@ -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, }, }) } diff --git a/src/push/push-plus.ts b/src/push/push-plus.ts index e0fe01f..e25a9ef 100644 --- a/src/push/push-plus.ts +++ b/src/push/push-plus.ts @@ -1,4 +1,3 @@ -import { AxiosResponse } from 'axios' import debug from 'debug' import { Send } from '../interfaces/send' import { ajax } from '@/utils/ajax'