1、完成钉钉认证才能使用此功能
2、需要登录控制台进行创建应用操作 https://open-dev.dingtalk.com/fe/app
3、需要设置 权限范围及通讯录权限设置 参考 https://www.ngui.cc/el/778161.html?action=onClick
pom
<dependency><groupId>com.aliyun</groupId><artifactId>dingtalk</artifactId><version>1.5.46</version> </dependency> <dependency><groupId>com.aliyun</groupId><artifactId>alibaba-dingtalk-service-sdk</artifactId><version>2.0.0</version> </dependency>
实体分为授权跟参数
授权实体
@Data public class DingDingAuthorization {private String appKey;private String appSecret;private String agentId; }
参数实体
@Data public class DingDingMsgData {private String mobile;private String title;private String content;private String url; }
发送调用方法类
@Slf4j @Component public class DingDingSendMsgHandle {public boolean sendDingDingOneMessage(DingDingAuthorization dingAuthorization, DingDingMsgData dingDingMsgData) {return DingDingUtil.sendDingDingMessage(dingAuthorization,dingDingMsgData);}public void sendDingDingBatchMessage(DingDingAuthorization dingAuthorization, List<DingDingMsgData> dingDingMsgData){dingDingMsgData.stream().forEach(s->{DingDingUtil.sendDingDingMessage(dingAuthorization,s);});} }
钉钉发送工具类
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiGettokenRequest; import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; import com.dingtalk.api.request.OapiUserGetByMobileRequest; import com.dingtalk.api.request.OapiUserGetRequest; import com.dingtalk.api.response.OapiGettokenResponse; import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; import com.dingtalk.api.response.OapiUserGetByMobileResponse; import com.dingtalk.api.response.OapiUserGetResponse; import com.lzjldiot.modules.sendChannel.dingding.entity.DingDingAuthorization; import com.lzjldiot.modules.sendChannel.dingding.entity.DingDingMsgData; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @Slf4j public class DingDingUtil {public static String getAccessToken(String appKey ,String appSecret) throws ApiException {DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");OapiGettokenRequest request = new OapiGettokenRequest();request.setAppkey(appKey);request.setAppsecret(appSecret);// /*请求方式*/request.setHttpMethod("GET");OapiGettokenResponse response = client.execute(request);return response.getAccessToken();}public static boolean sendDingDingMessage(DingDingAuthorization dingAuthorization, DingDingMsgData dingDingMsgData) {try {String accessToken = getAccessToken(dingAuthorization.getAppKey(),dingAuthorization.getAppSecret());if (StringUtils.isBlank(dingDingMsgData.getMobile())) {return false;}DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();req.setMobile(dingDingMsgData.getMobile());req.setHttpMethod("GET");OapiUserGetByMobileResponse rsp = client2.execute(req, accessToken);//判断不是ok 代表找不到该用户if (!rsp.getErrmsg().equals("ok")){return false;}else {// 获取到Urid就是在公司里要发送到那个人的idString urid = rsp.getUserid();// 根据用户id获取用户详情DingTalkClient userDetail = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");OapiUserGetRequest userReq = new OapiUserGetRequest();userReq.setUserid(urid);userReq.setHttpMethod("GET");OapiUserGetResponse userRsp = userDetail.execute(userReq, accessToken);String userName = userRsp.getName();DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();request.setUseridList(urid);request.setAgentId(Long.parseLong(dingAuthorization.getAgentId()));request.setToAllUser(false);OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());// 跳转链接//例如https://www.baidu.com//设置手机端可以打开链接msg.getOa().setMessageUrl(dingDingMsgData.getUrl());//设置PC端可以打开链接msg.getOa().setPcMessageUrl(dingDingMsgData.getUrl());// 设置headmsg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());msg.getOa().getHead().setText("待办事宜");msg.getOa().getHead().setBgcolor("00409eff");// 设置bodymsg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());msg.getOa().getBody().setTitle(dingDingMsgData.getTitle());msg.getOa().getBody().setContent(dingDingMsgData.getContent());// 消息类型msg.setMsgtype("oa");request.setMsg(msg);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());return response.isSuccess();}}catch (ApiException e){e.printStackTrace();}return false;} }
调用
@Autowired private DingDingSendMsgHandle dingDingSendMsgHandle;
//授权参数自行组装
DingDingAuthorization dingDingAuthorization = new DingDingAuthorization(); //发送参数自行组装 DingDingMsgData dingDingMsgData = new DingDingMsgData();boolean i= dingDingSendMsgHandle.sendDingDingOneMessage(dingDingAuthorization,dingDingMsgData);
结果示例