飞书告警模板

public class ParseFailedAlert {

  private static final Map<Integer, Integer> countMap = new HashMap<>();

  public static void sendAlert(int mlsId, String mlsNumber, Exception e) {
      //net.sf.json包
    countMap.merge(mlsId, 1, Integer::sum);
    JSONObject text = new JSONObject();
    JSONObject content = new JSONObject();
    JSONObject post = new JSONObject();
    JSONObject zhCn = new JSONObject();
    JSONArray contentArray = new JSONArray();
    JSONArray contentSubArray = new JSONArray();
    JSONArray countSubArray = new JSONArray();
    JSONObject msg = new JSONObject();
    msg.put("tag", "text");
    msg.put("text", e.toString());
    contentSubArray.add(msg);
    contentArray.add(contentSubArray);
    msg.put("text", String.format("failures: %d", countMap.get(mlsId)));
    countSubArray.add(msg);
    contentArray.add(countSubArray);
    String title = String.format("MLS %d-%s Parse Error", mlsId, mlsNumber);
    if (mlsId == 0) {
      title = "Unknown MLS Parse Error";
    }
    zhCn.put("title", title);
    zhCn.put("content", contentArray);
    post.put("zh_cn", zhCn);
    content.put("post", post);
    text.put("msg_type", "post");
    text.put("content", content);
    try {
      HttpTool.doPost("飞书机器人链接",
          text.toString());
    } catch (Exception exception) {
      exception.printStackTrace();
    }
  }

dopost请求可以使用现有的工具,比如OKHttpClient.httpPost

posted @ 2021-07-05 09:30  cristina95  阅读(341)  评论(0)    收藏  举报