UNPKG

@memo28.pro/notification

Version:

企业级消息通知解决方案 - 支持多平台消息推送的轻量级 TypeScript SDK

2 lines (1 loc) 1.17 kB
"use strict";const n=require("./plugin.js"),o="wxcom";class a extends n.Base{constructor(e){super(),this.platform=o,e&&this.setWebhook(e)}buildWxComMessages(e){const s=[],r=e.getMessages();for(const t of r)t.type==="text"?s.push({msgtype:"text",text:{content:t.content}}):t.type==="markdown"&&s.push({msgtype:"markdown",markdown:{content:t.content}});return s}async send(){if(!this.webhook)return console.error("Webhook URL is required"),!1;if(!this.msgBuilder)return console.error("MessageBuilder is required"),!1;try{const e=this.buildWxComMessages(this.msgBuilder);return e.length===0?(console.warn("No messages to send"),!0):(await Promise.all(e.map(r=>this.sendSingleMessage(r)))).every(r=>r)}catch(e){return console.error("Failed to send messages:",e),!1}}async sendSingleMessage(e){try{const s=await fetch(this.webhook,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!s.ok)return console.error(`HTTP error! status: ${s.status}`),!1;const r=await s.json();return r.errcode!==0?(console.error("WeChat API error:",r.errmsg),!1):!0}catch(s){return console.error("Network error:",s),!1}}}exports.Wxcom=a;exports.platformWxCom=o;