import { Injectable, Logger } from '@nestjs/common'; /** * 企业微信群机器人 Webhook 出站(单向告警,与智能机器人长连接无关)。 * @see https://developer.work.weixin.qq.com/document/path/91770 */ @Injectable() export class WecomWebhookAlertService { private readonly logger = new Logger(WecomWebhookAlertService.name); isEnabled(): boolean { return ( process.env.WECOM_ALERT_ENABLED === 'true' && !!(process.env.WECOM_ALERT_WEBHOOK_URL || '').trim() ); } async sendMarkdown(content: string): Promise { if (!this.isEnabled()) return false; const url = (process.env.WECOM_ALERT_WEBHOOK_URL || '').trim(); try { const res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ msgtype: 'markdown', markdown: { content: content.slice(0, 4000) }, }), }); const data = (await res.json().catch(() => ({}))) as { errcode?: number; errmsg?: string; }; if (!res.ok || (data.errcode != null && data.errcode !== 0)) { this.logger.warn( `wecom webhook failed: HTTP ${res.status} errcode=${data.errcode} ${data.errmsg ?? ''}`, ); return false; } return true; } catch (e) { this.logger.warn( `wecom webhook network error: ${e instanceof Error ? e.message : String(e)}`, ); return false; } } }