Skip to content

Commit

Permalink
refactor: 重构 ServerChanTurbo/ServerChanV3 到新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 284a56d commit 3ae9c5b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 27 deletions.
18 changes: 17 additions & 1 deletion src/push/qmsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export type QmsgOption = (QmsgPrivateMsgOption | QmsgGroupMsgOption) & {
bot?: string
}

export interface QmsgResponse {
/**
* 本次请求是否成功
*/
success: boolean
/**
* 本次请求结果描述
*/
reason: string
/**
* 错误代码。错误代码目前不可靠,如果要判断是否成功请使用success
*/
code: number
info: any
}

/**
* Qmsg酱。使用说明见 [Qmsg酱](https://qmsg.zendee.cn/docs)
*
Expand Down Expand Up @@ -77,7 +93,7 @@ export class Qmsg implements Send {
* @param [desp] 消息描述
* @param [option] QmsgOption 选项
*/
async send(title: string, desp: string, option: QmsgOption): Promise<SendResponse> {
async send(title: string, desp: string, option: QmsgOption): Promise<SendResponse<QmsgResponse>> {
Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option)
const { qq, type = 'send', bot } = option || {}
const msg = `${title}${desp ? `\n${desp}` : ''}`
Expand Down
56 changes: 40 additions & 16 deletions src/push/server-chan-turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ export type ChannelValue = 98 | 66 | 1 | 2 | 3 | 8 | 0 | 88 | 18 | 9

export type Channel = `${ChannelValue}` | `${ChannelValue}|${ChannelValue}`

export interface ServerChanTurboConfig {
/**
* Server酱 Turbo 的 SCTKEY
* 请前往 https://sct.ftqq.com/sendkey 领取
*/
SERVER_CHAN_TURBO_SCTKEY: string
}

/**
* 附加参数
*/
export type ServerChanTurboOptions = {
export type ServerChanTurboOption = {
/**
* 消息卡片内容,选填。最大长度 64。如果不指定,将自动从 desp 中截取生成。
*/
Expand Down Expand Up @@ -42,6 +50,20 @@ export type ServerChanTurboOptions = {
openid?: string
}

export interface ServerChanTurboResponse {
// 0 表示成功,其他值表示失败
code: number
message: string
data: {
// 推送消息的 ID
pushid: string
// 推送消息的阅读凭证
readkey: string
error: string
errno: number
}
}

/**
* Server 酱·Turbo
* 文档 https://sct.ftqq.com/
Expand All @@ -56,14 +78,15 @@ export class ServerChanTurbo implements Send {
/**
*
* @author CaoMeiYouRen
* @date 2021-02-27
* @param SCTKEY 请前往 https://sct.ftqq.com/sendkey 领取
* @date 2024-11-08
* @param config 请前往 https://sct.ftqq.com/sendkey 领取
*/
constructor(SCTKEY: string) {
this.SCTKEY = SCTKEY
Debugger('set SCTKEY: "%s"', SCTKEY)
constructor(config: ServerChanTurboConfig) {
const { SERVER_CHAN_TURBO_SCTKEY } = config
this.SCTKEY = SERVER_CHAN_TURBO_SCTKEY
Debugger('set SCTKEY: "%s"', this.SCTKEY)
if (!this.SCTKEY) {
throw new Error('SCTKEY 是必须的!')
throw new Error('SERVER_CHAN_TURBO_SCTKEY 是必须的!')
}
}
/**
Expand All @@ -77,19 +100,20 @@ export class ServerChanTurbo implements Send {
* 发送消息
*
* @author CaoMeiYouRen
* @date 2021-02-25
* @param text 消息的标题
* @param desp 消息的内容,支持 Markdown
* @date 2024-11-08
* @param title 消息的标题
* @param [desp=''] 消息的内容,支持 Markdown
* @param [option={}] 额外发送选项
*/
async send(text: string, desp: string = '', options: ServerChanTurboOptions = {}): Promise<SendResponse> {
Debugger('text: "%s", desp: "%s", options: %O', text, desp, options)
if (options.noip === 1 || options.noip === true) {
options.noip = '1'
async send(title: string, desp: string = '', option: ServerChanTurboOption = {}): Promise<SendResponse<ServerChanTurboResponse>> {
Debugger('title: "%s", desp: "%s", option: %O', title, desp, option)
if (option.noip === 1 || option.noip === true) {
option.noip = '1'
}
const data = {
text,
text: title,
desp,
...options,
...option,
}
return ajax({
url: `https://sctapi.ftqq.com/${this.SCTKEY}.send`,
Expand Down
55 changes: 45 additions & 10 deletions src/push/server-chan-v3.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import debug from 'debug'
import { Send } from '@/interfaces/send'
import { ajax } from '@/utils/ajax'
import { SendResponse } from '@/interfaces/response'

const Debugger = debug('push:server-chan-v3')

/**
* 附加参数
*/
export type ServerChanV3Options = {
export type ServerChanV3Option = {
tags?: string | string[] // 标签列表,多个标签使用竖线分隔;也可以用数组格式,数组格式下不要加竖线
short?: string // 推送消息的简短描述,用于指定消息卡片的内容部分,尤其是在推送markdown的时候
}

export interface ServerChanV3Config {
/**
* 请前往 https://sc3.ft07.com/sendkey 领取
*/
SERVER_CHAN_V3_SENDKEY: string
}

export interface ServerChanV3Response {
// 0 表示成功,其他值表示失败
code: number
message: string
errno: number
data: {
// 推送消息的 ID
pushid: string
meta: {
android: any
devices: any[]
}
}
}

/**
* Server酱³
* 文档:https://sc3.ft07.com/doc
Expand All @@ -34,11 +57,14 @@ export class ServerChanV3 implements Send {
private uid: string = ''

/**
* 创建 ServerChanV3 实例
* @author CaoMeiYouRen
* @date 2024-10-04
* @param sendkey 请前往 https://sc3.ft07.com/sendkey 领取
* @date 2024-11-08
* @param config 请前往 https://sc3.ft07.com/sendkey 领取
*/
constructor(sendkey: string) {
constructor(config: ServerChanV3Config) {
const { SERVER_CHAN_V3_SENDKEY } = config
const sendkey = SERVER_CHAN_V3_SENDKEY
this.sendkey = sendkey
Debugger('set sendkey: "%s"', sendkey)
if (!this.sendkey) {
Expand All @@ -50,15 +76,24 @@ export class ServerChanV3 implements Send {
}
}

async send(text: string, desp: string = '', options: ServerChanV3Options = {}): Promise<any> {
Debugger('text: "%s", desp: "%s", options: %O', text, desp, options)
if (Array.isArray(options.tags)) {
options.tags = options.tags.join('|')
/**
* 发送消息
*
* @author CaoMeiYouRen
* @date 2024-11-08
* @param title 消息的标题
* @param [desp=''] 消息的内容,支持 Markdown
* @param [option={}] 额外发送选项
*/
async send(title: string, desp: string = '', option: ServerChanV3Option = {}): Promise<SendResponse<ServerChanV3Response>> {
Debugger('title: "%s", desp: "%s", option: %O', title, desp, option)
if (Array.isArray(option.tags)) {
option.tags = option.tags.join('|')
}
const data = {
text,
text: title,
desp,
...options,
...option,
}
return ajax({
url: `https://${this.uid}.push.ft07.com/send/${this.sendkey}.send`,
Expand Down

0 comments on commit 3ae9c5b

Please sign in to comment.