louislam / uptime-kuma

A fancy self-hosted monitoring tool
https://uptime.kuma.pet
MIT License
60.73k stars 5.42k forks source link

Better Enterprise WeCom Notifications 更好的企业微信通知 #3145

Closed KeJunMao closed 1 year ago

KeJunMao commented 1 year ago

⚠️ Please verify that this bug has NOT been raised before.

🛡️ Security Policy

📝 Describe your problem

The repo is not open to the discussions, I am sorry to start a discussion here

我正在使用 docker 来运行 uptime-kuma。

我配置了企业微信机器人来推送通知,但是默认的通知只有文字,有效但不够好看。

我编写了一个更美观的 wecom.js, 希望可以帮到需要的人

Linked #1095

image

// notification-providers/webcom.js
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { setting } = require("../util-server");
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");

class WeCom extends NotificationProvider {
  name = "WeCom";
  async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
    let okMsg = "Sent Successfully.";
    let WeComUrl =
      "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" +
      notification.weComBotKey;
    let config = {
      headers: {
        "Content-Type": "application/json",
      },
    };

    // If heartbeatJSON is null, assume we're testing.
    if (heartbeatJSON == null) {
      await axios.post(
        WeComUrl,
        {
          msgtype: "text",
          text: {
            content: msg,
          },
        },
        config
      );
      return okMsg;
    }

    let address = "";
    let clientUrl = "";

    //#region computed address and clientUrl
    switch (monitorJSON["type"]) {
      case "ping":
        address = monitorJSON["hostname"];
        break;
      case "port":
      case "dns":
      case "steam":
        address = monitorJSON["hostname"];
        if (monitorJSON["port"]) {
          address += ":" + monitorJSON["port"];
        }
        break;
      default:
        address = monitorJSON["url"];
        break;
    }
    const baseURL = await setting("primaryBaseURL");
    if (baseURL) {
      clientUrl = `${baseURL}${getMonitorRelativeURL(monitorJSON["id"])}`;
    }
    // fallback address
    if (!clientUrl) {
      clientUrl = monitorJSON["type"] === "push" ? 'https://github.com/louislam/uptime-kuma' : address;
    }
    //#endregion

    const templateCard = {
      card_type: "text_notice",
      main_title: {
        title: monitorJSON["name"],
        desc: monitorJSON["type"] === "push" ? "Heartbeat" : address,
      },
      emphasis_content: {
        title: `❓`,
        desc: "Your service is unknown.",
      },
      horizontal_content_list: [
        {
          keyname: "Time",
          value: heartbeatJSON["time"],
        },
      ],
      card_action: {
        type: 1,
        url: clientUrl,
      },
    };

    if (heartbeatJSON["status"] === DOWN) {
      templateCard.emphasis_content = {
        title: "❌",
        desc: "Your service went down.",
      };
      templateCard.horizontal_content_list.push({
        keyname: "Error",
        value: heartbeatJSON["msg"] == null ? "N/A" : heartbeatJSON["msg"],
      });
    }
    if (heartbeatJSON["status"] === UP) {
      templateCard.emphasis_content = {
        title: "✅",
        desc: "Your service is up!",
      };
      templateCard.horizontal_content_list.push({
        keyname: "Ping",
        value:
          heartbeatJSON["ping"] == null ? "N/A" : heartbeatJSON["ping"] + " ms",
      });
    }

    await axios.post(
      WeComUrl,
      {
        msgtype: "template_card",
        template_card: templateCard,
      },
      config
    );
    return okMsg;
  }
}

module.exports = WeCom;

使用 docker-compose.yml 可以轻松覆盖原来的文件,下面是个例子

# docker-compose.yml
version: "3"

services:
  kuma:
    image: louislam/uptime-kuma:1
    volumes:
      - ./kuma/data:/app/data
      - ./kuma/notification-providers/webcom.js:/app/server/notification-providers/wecom.js
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - "3002:3001"

📝 Error Message(s) or Log

No response

🐻 Uptime-Kuma Version

1.x

💻 Operating System and Arch

Docker

🌐 Browser

Chrome 95.x

🐋 Docker Version

20.10.17, build 100c701

🟩 NodeJS Version

16.x

CommanderStorm commented 1 year ago

Translation according to deepl.com:

Title:

Better Enterprise WeChat Notifications

Content:

I'm using docker to run uptime-kuma. I configured an enterprise wecom bot to push notifications, but the default notifications are text only, which works but doesn't look good enough. I've written a better looking wecom.js, hopefully it will help someone who needs it

For those wondering: Wecom is buisness chat inside of wechat

CommanderStorm commented 1 year ago

@KeJunMao given that #1095 is merged: Can this issue be closed?

KeJunMao commented 1 year ago

Oh! No, I forgot to use English. Thank you for translating this issue for me.

1095 is awesome, but it's only text.

The issue want to solve is: Change the plain text notification of WeCom t to card notification

After: image

Before: image

hcql commented 1 year ago

@KeJunMao
notification-providers/webcom.js The content of the file is incorrect, It needs to be added on the last line

module.exports = WeCom;

CommanderStorm commented 1 year ago

@KeJunMao

I don't know what you mean by

The repo is not open to the discussions, I am sorry to start a discussion here

Providing code in issues is unlikely to get this code into master. Please provide code as a Pull request. See our contribution guide: https://github.com/louislam/uptime-kuma/blob/master/CONTRIBUTING.md

github-actions[bot] commented 1 year ago

We are clearing up our old issues and your ticket has been open for 3 months with no activity. Remove stale label or comment or this will be closed in 2 days.