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),
    });
  }
}
相关推荐
xiaoyan2015几秒前
electron35+deepseek-v3桌面端ai聊天模板
vue.js·electron·deepseek
Lin_熊米1 分钟前
仿 ElementUI 搭建自己的 vue 组件库
前端·vue.js·elementui
Bl_a_ck7 分钟前
npm、nvm、nrm
前端·vue.js·npm·node.js·vue
diang40 分钟前
Vue3 + Ant Design 实现 Excel 模板导入表格数据
前端·vue.js
doria小鱼40 分钟前
Vue3+Swiper实现PC端横向滑动拖拽
前端·vue.js
李梦晓41 分钟前
@vueuse/motion、motion-v、@motionone/vue三个动画库的区别和联系
前端·vue.js
D哈迪斯1 小时前
vue动态组件实现动态表单的方法
前端·javascript·vue.js
KeyNG_Jykxg1 小时前
🎨Element Plus X 上新! 组件升级🥳
前端·javascript·vue.js
琦遇2 小时前
Vue3使用Swiper实现列表内容分两行循环滚动
前端·javascript·vue.js
贰貮2 小时前
使用Vue 3与.NET 8.0通过SignalR实现实时通信,并结合JWT身份验证
vue.js·websocket·.net·.netcore