更改企业微信发送方式

This commit is contained in:
2025-12-04 16:37:03 +08:00
parent f8b635f060
commit 27551af823
2 changed files with 75 additions and 2 deletions

View File

@@ -175,4 +175,33 @@ public class WeChatUtil {
throw new RuntimeException("请求发送消息出错", e);
}
}
/**
* 发送企业微信 Markdown 消息
* @param toUser 接收用户,'|' 分隔
* @param markdown Markdown 内容
*/
public void sendMarkdownMessage(String toUser, String markdown) {
String accessToken = getAccessToken();
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
JSONObject msg = new JSONObject();
msg.put("touser", toUser);
msg.put("msgtype", "markdown");
msg.put("agentid", weChatConfig.getAgentId());
JSONObject md = new JSONObject();
md.put("content", markdown);
msg.put("markdown", md);
try {
String response = restTemplate.postForObject(url, msg, String.class);
JSONObject jsonResponse = JSONObject.parseObject(response);
if (jsonResponse.getIntValue("errcode") != 0) {
throw new RuntimeException("发送Markdown消息失败: " + jsonResponse.getString("errmsg"));
}
} catch (Exception e) {
throw new RuntimeException("请求发送Markdown消息出错", e);
}
}
}