maven依赖
<!--钉钉-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
代码
package com.ruoyi.lab.utils;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.taobao.api.ApiException;
import java.util.ArrayList;
/**
* @ClassName DDMessageUtils
* @Description java对接钉钉发送消息通知
* @Author lgn
* @Date 16:46 2022/11/23
* @Version 1.0
**/
@Log4j
public class DDMessageUtils {
static String APP_KEY="dingxyd8mxocjzgbiyoj";
static String APP_SECRET="f168jCW6xAVRcGgvCXteTVIJV_WNuAhUZaLFCHEokzqBwMD_XakE7-bL-MIlrHJK";
static Long AGENT_ID = Long.parseLong("1903592231");
static String MESSAGE_URL = "https://www.dingtalk.com";
static String PC_MESSAGE_URL = "https://www.dingtalk.com";
public static void main(String[] args) throws ApiException {
String mobile="1800X141613,153034X3218,1733X111330";
String park="";
String alarmCode="";
String severityDesc="";
String msgcontent="报修单已挂起,请知悉!";
String content1="问题标题:账号登录不上了";
String content2="问题类型:软件";
String content3="问题描述:我的账号突然就登录不上了,显示账号已冻结,我现在XXXXXXXXX";
String content4="工单创建时间:2022-07-13 09:48:18";
String content5="工单挂起时间:2022-08-14 11:42:39";
String content6="当前处理人:张三";
sendNotification(mobile,park,alarmCode,severityDesc,msgcontent);
}
/**
* 获取AccessToken
* @return AccessToken
* @throws ApiException
*/
private static String getAccessToken() throws ApiException {
DefaultDingTalkClient client =
new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
//Appkey
request.setAppkey(APP_KEY);
//Appsecret
request.setAppsecret(APP_SECRET);
/*请求方式*/
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
return response.getAccessToken();
}
public static void sendNotification(String mobile, String park, String alarmCode, String severityDesc, String msgcontent) throws ApiException {
log.info("发送钉钉通知");
String accessToken = getAccessToken();
if(StringUtils.isBlank(mobile)){
return;
}
//电话号码数组
String[] split = mobile.split(",");
for (String s : split) {
DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
req.setMobile(s);
req.setHttpMethod("GET");
OapiUserGetByMobileResponse rsp = client2.execute(req, accessToken);
//获取到Urid就是在公司里要发送到那个人的id
String urid = rsp.getUserid();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setUseridList(urid);
request.setAgentId(AGENT_ID);
request.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
msg.getOa().setMessageUrl(MESSAGE_URL);
msg.getOa().setPcMessageUrl(PC_MESSAGE_URL);
msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
msg.getOa().getHead().setText("text");
msg.getOa().getHead().setBgcolor("FFBBBBBB");
msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
msg.getOa().getBody().setContent(msgcontent);
OapiMessageCorpconversationAsyncsendV2Request.Form formPark = new OapiMessageCorpconversationAsyncsendV2Request.Form();
formPark.setKey(park);
OapiMessageCorpconversationAsyncsendV2Request.Form formAlarmCode = new OapiMessageCorpconversationAsyncsendV2Request.Form();
formAlarmCode.setKey("************");
formAlarmCode.setValue(alarmCode);
OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form();
formSeverityDesc.setKey("************");
formSeverityDesc.setValue(severityDesc);
OapiMessageCorpconversationAsyncsendV2Request.Form formConner = new OapiMessageCorpconversationAsyncsendV2Request.Form();
formConner.setKey("************");
ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>();
objects.add(formPark);
objects.add(formSeverityDesc);
objects.add(formAlarmCode);
objects.add(formConner);
msg.getOa().getBody().setForm(objects);
msg.setMsgtype("oa");
request.setMsg(msg);
log.info("获取发送通知消息体和获取发送通知人完成");
OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,accessToken);
log.info("发送消息是否成功"+response.isSuccess());
System.out.println(response.isSuccess());
log.info("消息任务ID"+response.getTaskId());
System.out.println(response.getTaskId());
}
}
}