VUE 集成企微机器人通知
message-robot
便于线上异常问题及时发现处理,项目中集成企微机器人通知,及时接收问题并处理
企微机器人通知工具类
export class MessageRobotUtil {
constructor() {}
/**
* 发送 markdown 消息
* @param robotKey 机器人 ID
* @param title 消息标题
* @param items 消息内容
*/
public sendMarkdownMessage(robotKey: string, title: string, items: Record<string, string>) {
const content = this.generateMarkdownMessageContent(title, items);
this.sendMessage(robotKey, {
msgtype: "markdown",
markdown: { content: content },
});
}
/**
* 构建 markdown 消息内容
* @param title 消息标题
* @param items 消息内容 键值对
* @returns
*/
private generateMarkdownMessageContent(title: string, items: Record<string, any>) {
let content = `### 【${title}】`;
content += "\n";
for (const key in items) {
content += `> ${key}: <font color="warning">${items[key]}</font>\n`;
}
return content;
}
/**
* 消息发送
* @param robotKey 机器人 ID
* @param message 消息内容
*/
private sendMessage(robotKey: string, message: Record<string, any>) {
// 跟地址 https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=****
const url = "/cgi-bin/webhook/send?key=" + robotKey;
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(message),
});
}
}