feat: 通知

This commit is contained in:
裴浩宇 2024-05-06 15:02:29 +08:00
parent 82536d2ff1
commit 94a8a0c455
2 changed files with 52 additions and 8 deletions

View File

@ -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");
}
}

View File

@ -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;