1、多 key 默认拼成 1 条企微通知 2、UTF-8 超过 4096 字节 → 按 key 拆开,每条带完整抬头,依次发送 3、条间间隔 200ms,降低机器人限频风险 4、CI 控制台会标明「超长已按 key 拆为 N 条」并逐条打印
This commit is contained in:
@@ -9,6 +9,7 @@ import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -46,6 +47,15 @@ public class WeComNotifier {
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() == 200) {
|
||||
// 企微成功时 body 一般为 {"errcode":0,...}
|
||||
if (response.body() != null && response.body().contains("\"errcode\":0")) {
|
||||
return true;
|
||||
}
|
||||
if (response.body() != null && response.body().contains("errcode")
|
||||
&& !response.body().contains("\"errcode\":0")) {
|
||||
System.err.println("[WeComNotifier] 通知失败: " + response.body());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
System.err.println("[WeComNotifier] 通知失败, HTTP " + response.statusCode() + ": " + response.body());
|
||||
@@ -55,4 +65,36 @@ public class WeComNotifier {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按序发送多条 Markdown(用于超长按 key 拆分后的通知)。
|
||||
*
|
||||
* @return 成功条数
|
||||
*/
|
||||
public int sendMarkdownMessages(String webhookUrl, List<String> markdownMessages) {
|
||||
if (markdownMessages == null || markdownMessages.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
int ok = 0;
|
||||
for (int i = 0; i < markdownMessages.size(); i++) {
|
||||
String content = markdownMessages.get(i);
|
||||
boolean success = sendMarkdown(webhookUrl, content);
|
||||
if (success) {
|
||||
ok++;
|
||||
}
|
||||
System.out.println("[WeComNotifier] 第 " + (i + 1) + "/" + markdownMessages.size()
|
||||
+ " 条发送" + (success ? "成功" : "失败")
|
||||
+ " (" + content.getBytes(StandardCharsets.UTF_8).length + " bytes)");
|
||||
// 避免触发机器人频率限制(约 20 条/分钟)
|
||||
if (i < markdownMessages.size() - 1) {
|
||||
try {
|
||||
Thread.sleep(200L);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user