更改企业微信发送方式
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user