一.钉钉开发平台配置
钉钉开发后台链接:open-dev.dingtalk.com/fe/app#/corp/app(需管理员账户或已开启开发权限账户才可配置)
1.创建应用程序
2.配置小程序
创建酷应用---添加(配置)小程序---凭证与基本信息(记住AgentId,Client ID也叫AppKey,Client Secret 这三个id)---权限管理(需要申请想要的权限,我为了方便将个人权限与通讯录管理权限全部开通);
关于权限管理有不明白的可以点击了解更多,里面有详细讲解,不过内容不是最新版本
二、开发端配置
1.引入依赖
第一个依赖:处理向钉钉API发送的请求
第二个依赖:输出日志与判断字符串是否为空
<!-- 钉钉消息推送 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--日志与工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
2.编写代码
将AgentId,Client ID也叫AppKey,Client Secret 带入即可,MESSAGE_URL表示在手机上点击收到的消息跳转的页面,PC_MESSAGE_URL则是电脑端跳转页面(不填也没事)
package org.DingDingPush;
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.taobao.api.ApiException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Description: 钉钉个人消息推送
* @author: QiTan
* @date: 2023/12/25 13:11
*/
public class DingDingPrivatePush {
Log log = LogFactory.getLog(DingDingPrivatePush.class);
//钉钉机器人
private static String APP_KEY = "填入Client ID";
private static String APP_SECRET = "填入Client Secret";
private static Long AGENT_ID = 填入AgentId,记得后面加L,这是Long类型;
private static String MESSAGE_URL = "手机端跳转页面链接";
private static String PC_MESSAGE_URL = "电脑端跳转页面链接";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
public static void main(String[] args) {
String mobile="17856917978";
DingDingPrivatePush oa=new DingDingPrivatePush();
String title="天空飘来五个字,那都不是事!";
oa.sendOA(mobile,title);
}
/**
* 获取AccessToken
* @return AccessToken
* @throws ApiException
*/
private 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);
// System.out.println(response.getAccessToken());
return response.getAccessToken();
}
/**
* 发送OA消息
* @param mobile 发送消息人的电话,多个英文逗号拼接
* @throws ApiException
*/
public void sendOA(String mobile,String title) {
log.info("发送钉钉通知");
String accessToken = null;
try {
accessToken = getAccessToken();
} catch (ApiException e) {
log.info("获取AccessToken失败:"+e);
}
if(StringUtils.isBlank(mobile)){
return;
}
//电话号码数组
String[] split = mobile.split(",");
for (String s : split) {
try {
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();
//根据用户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(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);
//设置head
msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
msg.getOa().getHead().setText("待办事宜");
msg.getOa().getHead().setBgcolor("00409eff");
//设置body
msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
msg.getOa().getBody().setTitle(title);
msg.getOa().getBody().setContent("创建人:" + userName + "\n创建时间:" + sdf.format(new Date()));
//消息类型
msg.setMsgtype("oa");
request.setMsg(msg);
log.info("获取发送通知消息体和获取发送通知人完成");
OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,accessToken);
log.info("发送消息是否成功:"+response.isSuccess());
log.info("消息任务ID:"+response.getTaskId());
} catch (ApiException e) {
e.printStackTrace();
}
}
}
}
3.实现效果
注意事项:最常见的应该是接口权限未开通,接口调用了(每月有额度),但是一直未发送消息,根据控制台提示开通相应权限即可