钉钉发消息加@

This commit is contained in:
majintao
2025-06-04 15:06:30 +08:00
parent ecc8d89009
commit 852e62208a
@@ -30,6 +30,28 @@ class DingDingMessageUtil {
}
}
static void sendTextMessageAndAt(String access_token, String msg, String atMobile) throws Exception {
HttpRequest request = new HttpRequest();
HttpPost httppost = new HttpPost("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
httppost.addHeader("Content-Type", "application/json; charset=utf-8");
// 创建消息体,只包含一个手机号
String textMsg = "{ " +
"\"msgtype\": \"text\", " +
"\"text\": {\"content\": \"" + msg + "\"}, " +
"\"at\": {\"atMobiles\": [\"" + atMobile + "\"]}" + // 只 @ 一个手机号
"}";
StringEntity se = new StringEntity(textMsg, "utf-8");
httppost.setEntity(se);
HttpResponse response = request.getHttpClient().execute(httppost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("消息发送结果:" + result);
}
}
static void sendMarkdownMessage(String access_token, String title, String msg){
HttpRequest request = new HttpRequest()
JSONObject requestBody = new JSONObject()