60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
// shared/lib/wechat/wechat-pusher.ts — 企业微信 Webhook 通用客户端
|
|
export interface PushResult {
|
|
success: boolean
|
|
responseCode: number | null
|
|
responseBody: string | null
|
|
error?: string
|
|
}
|
|
|
|
export class WeChatPusher {
|
|
private webhookUrl: string
|
|
|
|
constructor(webhookUrl: string) {
|
|
this.webhookUrl = webhookUrl
|
|
}
|
|
|
|
// 推送 markdown 消息
|
|
async pushMarkdown(title: string, content: string): Promise<PushResult> {
|
|
return this.push({
|
|
msgtype: 'markdown',
|
|
markdown: { content: `## ${title}\n${content}` },
|
|
})
|
|
}
|
|
|
|
// 推送纯文本消息
|
|
async pushText(content: string): Promise<PushResult> {
|
|
return this.push({ msgtype: 'text', text: { content } })
|
|
}
|
|
|
|
// 通用推送
|
|
private async push(message: Record<string, unknown>): Promise<PushResult> {
|
|
try {
|
|
const res = await fetch(this.webhookUrl, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(message),
|
|
signal: AbortSignal.timeout(5000),
|
|
})
|
|
const body = await res.text().catch(() => null)
|
|
return { success: res.ok, responseCode: res.status, responseBody: body }
|
|
} catch (err) {
|
|
return { success: false, responseCode: null, responseBody: null, error: 'Network request failed' }
|
|
}
|
|
}
|
|
}
|
|
|
|
// 兼容适配器(降低 issue-ai 迁移成本)
|
|
export class WeChatPusherCompat {
|
|
private pusher: WeChatPusher
|
|
|
|
constructor(webhookUrl: string) {
|
|
this.pusher = new WeChatPusher(webhookUrl)
|
|
}
|
|
|
|
// 兼容旧接口:返回 Promise<boolean>
|
|
async pushText(text: string, _webhookUrl?: string): Promise<boolean> {
|
|
const result = await this.pusher.pushText(text)
|
|
return result.success
|
|
}
|
|
}
|