注:不能通过JS调用,JS调用会出现跨域问题
支持文本型,markdown型,卡片型
package com.fr.data;
import com.fr.log.FineLoggerFactory;
import com.fr.script.Calculator;
import com.fr.data.DefinedSubmitJob;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class WXRobotMessageSubmitJob5 extends DefinedSubmitJob {
private static final Gson gson = new Gson();
private Object webhookUrl;
private Object jsonMessage;
private Object msgType;
private Object mainTitle;
private Object mainDesc; // 描述参数
private Object emphasisTitle; // 强调内容的标题
private Object emphasisDesc; // 强调内容的描述
private Object quoteTitle;
private Object quoteText;
private Object subTitleText;
private Object horizontalContentList;
private Object cardActionUrl;
public void doJob(Calculator calculator) throws Exception {
FineLoggerFactory.getLogger().warn("准备发送企业微信Msg");
try {
String webhookUrl = this.getValue(this.webhookUrl);
String jsonMessage = this.getValue(this.jsonMessage);
String msgType=this.getValue(this.msgType);
String mainTitle=this.getValue(this.mainTitle);
String mainDesc=this.getValue(this.mainDesc);
String emphasisTitle=this.getValue(this.emphasisTitle);
String emphasisDesc=this.getValue(this.emphasisDesc);
String quoteTitle=this.getValue(this.quoteTitle);
String quoteText=this.getValue(this.quoteText);
String subTitleText=this.getValue(this.subTitleText);
String horizontalContentList=this.getValue(this.horizontalContentList);
String cardActionUrl=this.getValue(this.cardActionUrl);
if (jsonMessage == null || jsonMessage.isEmpty()) {
jsonMessage = generateWeChatMessage(msgType, mainTitle, mainDesc,
emphasisTitle, emphasisDesc, quoteTitle, quoteText, subTitleText, horizontalContentList, cardActionUrl);
}
FineLoggerFactory.getLogger().warn("准备发送企业微信Msg:webhookUrl:" + webhookUrl + " jsonMessage: " + jsonMessage);
sendMessage(webhookUrl, jsonMessage);
} catch (Exception e) {
StackTraceElement stackTraceElement = e.getStackTrace();
FineLoggerFactory.getLogger().error(e.getMessage(), e);
FineLoggerFactory.getLogger().error(stackTraceElement.getFileName() + stackTraceElement.getLineNumber(), e);
}
return;
}
private static void sendMessage(String webhookUrl, String jsonMessage) {
try {
URL url = new URL(webhookUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
// 将 JSON 字符串写入请求体
try (OutputStream os = connection.getOutputStream()) {
byte input = jsonMessage.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// 处理响应
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
FineLoggerFactory.getLogger().warn("企业微信发送成功.");
} else {
FineLoggerFactory.getLogger().error("Failed to send message. HTTP error code: " + responseCode);
}
} catch (Exception e) {
StackTraceElement stackTraceElement = e.getStackTrace();
FineLoggerFactory.getLogger().error(e.getMessage(), e);
FineLoggerFactory.getLogger().error(stackTraceElement.getFileName() + stackTraceElement.getLineNumber(), e);
}
}
private String getValue(Object input) {
String re = null;
if (input instanceof JobValue) {
if(((JobValue) input).getState().checkChanged()||((JobValue) input).getState().checkInsert())
re = ((JobValue) input).getValue().toString();
} else if (input instanceof String) {
re = (String) input;
}else if(input!=null) {
re=input.toString();
}
return re;
}
public static String generateWeChatMessage(
String msgType,
String mainTitle,
String mainDesc,
String emphasisTitle,
String emphasisDesc,
String quoteTitle,
String quoteText,
String subTitleText,
String horizontalContentList,
String cardActionUrl) {
switch (msgType) {
case "text":
return generateTextMessage(mainTitle, mainDesc, emphasisTitle, emphasisDesc,
quoteTitle, quoteText, subTitleText, horizontalContentList);
case "markdown":
return generateMarkdownMessage(mainTitle, mainDesc, emphasisTitle, emphasisDesc,
quoteTitle, quoteText, subTitleText, horizontalContentList);
case "template_card":
return generateTemplateCardMessage(mainTitle, mainDesc, emphasisTitle, emphasisDesc,
quoteTitle, quoteText, subTitleText, horizontalContentList, cardActionUrl);
default:
throw new IllegalArgumentException("Unsupported message type: " + msgType);
}
}
// 生成文本消息的方法
// 生成文本消息的方法
public static String generateTextMessage(String mainTitle, String mainDesc,
String emphasisTitle, String emphasisDesc,
String quoteTitle, String quoteText,
String subTitleText, String horizontalContentList) {
StringBuilder textMessage = new StringBuilder();
textMessage.append(mainTitle).append("\n"); // mainTitle 换行
textMessage.append(mainDesc).append("\n\n"); // mainDesc 换两行
textMessage.append(emphasisTitle).append("\n"); // emphasisTitle 换行
textMessage.append(emphasisDesc).append("\n\n"); // emphasisDesc 换两行
// 处理 quoteTitle 和 quoteText
if (quoteTitle != null && !quoteTitle.isEmpty()) {
textMessage.append(quoteTitle).append("\n"); // quoteTitle 换行
}
if (quoteText != null && !quoteText.isEmpty()) {
textMessage.append(quoteText).append("\n\n"); // quoteText 换两行
}
// 处理 subTitleText
if (subTitleText != null && !subTitleText.isEmpty()) {
textMessage.append(subTitleText).append("\n"); // subTitleText 换行
}
// 解析 horizontalContentList,并转为 key:value 风格
if (horizontalContentList != null && !horizontalContentList.isEmpty()) {
JsonArray jsonArray = new Gson().fromJson(horizontalContentList, JsonArray.class);
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject item = jsonArray.get(i).getAsJsonObject();
String keyName = item.get("keyname").getAsString();
String value = item.get("value").getAsString();
textMessage.append(keyName).append(": ").append(value).append("\n"); // 采用每行key:value的样式
}
}
// 包装返回格式
JsonObject responseJson = new JsonObject();
responseJson.addProperty("msgtype", "text");
JsonObject textContent = new JsonObject();
textContent.addProperty("content", textMessage.toString());
responseJson.add("text", textContent);
return gson.toJson(responseJson);
}
// 生成Markdown消息的方法
public static String generateMarkdownMessage(String mainTitle, String mainDesc,
String emphasisTitle, String emphasisDesc,
String quoteTitle, String quoteText,
String subTitleText, String horizontalContentList) {
StringBuilder markdownMessage = new StringBuilder();
markdownMessage.append("# **").append(mainTitle).append("**\n");
markdownMessage.append("<font color=\"comment\">").append(mainDesc).append("</font>\n \n");
markdownMessage.append("# **<font color=\"warning\">").append(emphasisTitle).append("</font>**\n");
markdownMessage.append("<font color=\"comment\">").append(emphasisDesc).append("</font>\n \n");
// 处理 quoteTitle 和 quoteText
if (quoteTitle != null && !quoteTitle.isEmpty()) {
markdownMessage.append("<font color=\"comment\">").append(quoteTitle).append("</font>\n");
}
if (quoteText != null && !quoteText.isEmpty()) {
markdownMessage.append("<font color=\"comment\">").append(quoteText).append("</font>\n \n");
}
// 处理 subTitleText
if (subTitleText != null && !subTitleText.isEmpty()) {
markdownMessage.append("## ").append(subTitleText).append("\n");
}
// 解析 horizontalContentList,并转为 # **key**:value: 风格
if (horizontalContentList != null && !horizontalContentList.isEmpty()) {
JsonArray jsonArray = new Gson().fromJson(horizontalContentList, JsonArray.class);
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject item = jsonArray.get(i).getAsJsonObject();
String keyName = item.get("keyname").getAsString();
String value = item.get("value").getAsString();
markdownMessage.append("<font color=\"comment\">").append(keyName).append("</font>: ").append(value).append("\n");
}
}
// 包装返回格式
JsonObject responseJson = new JsonObject();
responseJson.addProperty("msgtype", "markdown");
JsonObject markdownContent = new JsonObject();
markdownContent.addProperty("content", markdownMessage.toString());
responseJson.add("markdown", markdownContent);
return gson.toJson(responseJson);
}
// 生成Template Card消息的方法
public static String generateTemplateCardMessage(
String mainTitle,
String mainDesc, // 描述参数
String emphasisTitle, // 强调内容的标题
String emphasisDesc, // 强调内容的描述
String quoteTitle,
String quoteText,
String subTitleText,
String horizontalContentList,
String cardActionUrl) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("msgtype", "template_card");
JsonObject templateCard = new JsonObject();
templateCard.addProperty("card_type", "text_notice");
// 解析 MainTitle 和 MainDesc 为 JSON 对象
JsonObject mainTitleObj = new JsonObject();
mainTitleObj.addProperty("title", mainTitle);
mainTitleObj.addProperty("desc", mainDesc);
templateCard.add("main_title", mainTitleObj);
// 新增强调内容
JsonObject emphasisContent = new JsonObject();
emphasisContent.addProperty("title", emphasisTitle);
emphasisContent.addProperty("desc", emphasisDesc);
templateCard.add("emphasis_content", emphasisContent);
// 处理引用区域
if (quoteTitle != null && !quoteTitle.isEmpty()) {
JsonObject quoteArea = new JsonObject();
quoteArea.addProperty("title", quoteTitle);
if (quoteText != null && !quoteText.isEmpty()) {
quoteArea.addProperty("quote_text", quoteText);
}
templateCard.add("quote_area", quoteArea);
}
// 处理副标题
if (subTitleText != null && !subTitleText.isEmpty()) {
templateCard.addProperty("sub_title_text", subTitleText);
}
// 解析 horizontalContentList 为 JSON 数组
if (horizontalContentList != null && !horizontalContentList.isEmpty()) {
JsonArray horizontalContentArray = new Gson().fromJson(horizontalContentList, JsonArray.class);
templateCard.add("horizontal_content_list", horizontalContentArray);
}
// 创建卡片点击事件
// 处理副标题
if (cardActionUrl == null || cardActionUrl.isEmpty()) {
cardActionUrl="https://www.baidu.com";
}
JsonObject cardAction = new JsonObject();
cardAction.addProperty("type", 1); // 假设为 URL 跳转
cardAction.addProperty("url", cardActionUrl); // 必填
templateCard.add("card_action", cardAction);
// 将模板卡片添加到消息 JSON
messageJson.add("template_card", templateCard);
// 转换为 JSON 字符串
return new Gson().toJson(messageJson);
}
}