From 94a8a0c4551cb131038afb54fef24d3756b57915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A3=B4=E6=B5=A9=E5=AE=87?= <617594538@qq.com> Date: Mon, 6 May 2024 15:02:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pnkx-admin/src/test/java/NotifyTest.java | 4 +- .../pnkx/common/notify/FeishuISysNotify.java | 56 ++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/pnkx-admin/src/test/java/NotifyTest.java b/pnkx-admin/src/test/java/NotifyTest.java index 7d6348a..4d568d5 100644 --- a/pnkx-admin/src/test/java/NotifyTest.java +++ b/pnkx-admin/src/test/java/NotifyTest.java @@ -1,6 +1,8 @@ import com.pnkx.common.notify.FeishuISysNotify; import org.junit.Test; +import java.io.IOException; + /** * NotifyTest * @@ -13,6 +15,6 @@ public class NotifyTest { @Test public void notifyTest() { - FeishuISysNotify.sendNotification("测试通知"); + FeishuISysNotify.sendNotification("您有待办", "/todo"); } } diff --git a/pnkx-common/src/main/java/com/pnkx/common/notify/FeishuISysNotify.java b/pnkx-common/src/main/java/com/pnkx/common/notify/FeishuISysNotify.java index 29e1384..1502270 100644 --- a/pnkx-common/src/main/java/com/pnkx/common/notify/FeishuISysNotify.java +++ b/pnkx-common/src/main/java/com/pnkx/common/notify/FeishuISysNotify.java @@ -1,5 +1,6 @@ package com.pnkx.common.notify; +import com.pnkx.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,10 +25,10 @@ public class FeishuISysNotify { // 飞书机器人的webhook地址 private static final String webhookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/ae03114d-aa0d-4348-b12e-9bd7e2911399"; - public static void sendNotification(String message) { + public static void sendNotification(String message, String link) { try { URL url = new URL(webhookUrl); - HttpURLConnection connection = getHttpURLConnection(message, url); + HttpURLConnection connection = getHttpURLConnection(message, link, url); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { @@ -43,21 +44,62 @@ public class FeishuISysNotify { /** * 获取HttpURLConnection + * * @param message 通知内容 - * @param url webhook地址 + * @param link 超链接地址 + * @param url webhook地址 * @return HttpURLConnection * @throws IOException 异常 */ - private static HttpURLConnection getHttpURLConnection(String message, URL url) throws IOException { + private static HttpURLConnection getHttpURLConnection(String message, String link, URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.setDoOutput(true); - String jsonInputString = "{\"msg_type\":\"text\",\"content\":{\"text\":\"" + message + "\"}}"; - + StringBuilder jsonInputString = new StringBuilder(); + jsonInputString.append("{\n" + + " \"msg_type\": \"interactive\",\n" + + " \"card\": {\n" + + " \"elements\": [\n" + + " {\n" + + " \"tag\": \"div\",\n" + + " \"text\": {\n" + + " \"content\": \"" + message + "\",\n" + + " \"tag\": \"lark_md\"\n" + + " }\n" + + " }\n"); + if (StringUtils.isNotEmpty(link)) { + jsonInputString.append(", {\n" + + " \"actions\": [\n" + + " {\n" + + " \"tag\": \"button\",\n" + + " \"text\": {\n" + + " \"content\": \"详情查看\",\n" + + " \"tag\": \"lark_md\"\n" + + " },\n" + + " \"url\": \"https://admin.pnkx.top:8" + link + "\",\n" + + " \"type\": \"default\",\n" + + " \"value\": {\n" + + " \n" + + " }\n" + + " }\n" + + " ],\n" + + " \"tag\": \"action\"\n" + + " }\n"); + } + jsonInputString.append(" ],\n" + + " \"header\": {\n" + + " \"title\": {\n" + + " \"content\": \"【Pei你看雪博客】系统通知\",\n" + + " \"tag\": \"plain_text\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}"); + logger.info("飞书通知内容:{}", jsonInputString); try (OutputStream os = connection.getOutputStream()) { - byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); + byte[] input = jsonInputString.toString().getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); } return connection;