前言
今天公司通知需求需要使用钉钉的待办任务,查了下API发现待办任务在上个月的时候更新了接口,旧版的使用不了,而新版的接口也还没有教程,故写了这篇文章。
这里是API链接 钉钉新增待办任务
一、准备工作
-
首先要在钉钉开发者后台创建一个应用,然后开放待办任务下的所有权限。
-
然后要下载钉钉jar包,我用的是21年八九月的包,在maven上没法自动导入,这里提供下载链接。
链接:https://pan.baidu.com/s/1YG3l8THirXVtrhgHWZbLwQ 提取码:qqs5 -
接着是新版待办要使用的sdk,这里使用maven导入就可以。
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dingtalk</artifactId>
<version>1.1.96</version>
</dependency>
二、代码部分
1.前置方法
下面是封装好的小组件,用于获取Token等
AppKey和AppSecret
/**
* 获取钉钉token
* @param appKey AppKey
* @param appSecret AppSecret
* @return token字符串
*/
public static String getToken(String appKey, String appSecret) {
//token两小时更新一次
DingTalkClient getTokenclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appKey);
request.setAppsecret(appSecret);
request.setHttpMethod("GET");
OapiGettokenResponse response = null;
try {
response = getTokenclient.execute(request);
} catch (ApiException e) {
e.printStackTrace();
}
return response.getAccessToken();
}
/**
* 通过手机号获取用户的userId
* @param mobile 手机号
* @param appKey AppKey
* @param appSecret AppSecret
* @return userId
*/
public static String getUserId (String mobile, String appKey, String appSecret) {
//获取用户urid
DingTalkClient getUridClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
req.setMobile(mobile);
OapiV2UserGetbymobileResponse rsp = null;
try {
rsp = getUridClient.execute(req, getToken(appKey, appSecret));
} catch (ApiException e) {
e.printStackTrace();
}
return rsp.getResult().getUserid();
}
/**
* 通过userid获取用户对象
* @param userId userid
* @param appKey AppKey
* @param appSecret AppSecret
* @return 用户对象
*/
public static OapiV2UserGetResponse.UserGetResponse getUserInfo (String userId, String appKey, String appSecret) {
//获取用户urid
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest req = new OapiV2UserGetRequest();
req.setUserid(userId);
req.setLanguage("zh_CN");
OapiV2UserGetResponse rsp = null;
try {
rsp = client.execute(req, getToken(appKey, appSecret));
} catch (ApiException e) {
e.printStackTrace();
}
return rsp.getResult();
}
2.核心方法
根据手机号发送待办任务
public static void sendWorkRecordByMobile (String mobile, String title, String context, Long date, String appKey, String appSecret) {
String unionId = getUserInfo(getUserId(mobile, appKey, appSecret), appKey, appSecret).getUnionid();
com.aliyun.dingtalktodo_1_0.Client client = null;
try {
client = createClient();
} catch (Exception e) {
e.printStackTrace();
}
CreateTodoTaskHeaders createTodoTaskHeaders = new CreateTodoTaskHeaders();
createTodoTaskHeaders.xAcsDingtalkAccessToken = getToken(appKey, appSecret);
CreateTodoTaskRequest.CreateTodoTaskRequestNotifyConfigs notifyConfigs = new CreateTodoTaskRequest.CreateTodoTaskRequestNotifyConfigs()
.setDingNotify("1");
// CreateTodoTaskRequest.CreateTodoTaskRequestDetailUrl detailUrl = new CreateTodoTaskRequest.CreateTodoTaskRequestDetailUrl()
// // app内打开的链接
// .setAppUrl("https://www.dingtalk.com")
// // pc端打开的链接
// .setPcUrl("https://www.dingtalk.com");
CreateTodoTaskRequest createTodoTaskRequest = new CreateTodoTaskRequest()
// 待办标题
.setSubject(title)
.setCreatorId(unionId)
// 待办内容
.setDescription(context)
// 截止时间,使用时间戳
.setDueTime(date)
// 接收消息的人
.setExecutorIds(java.util.Arrays.asList(
unionId
))
// .setDetailUrl(detailUrl)
.setIsOnlyShowExecutor(true)
// 紧急程度,分4档,此处为2档普通
.setPriority(20)
.setNotifyConfigs(notifyConfigs);
try {
client.createTodoTaskWithOptions(unionId, createTodoTaskRequest, createTodoTaskHeaders, new RuntimeOptions());
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.message);
}
} catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.message);
}
}
}
注意:新版发送待办使用的是unionid而不是userid
调用方法
sendWorkRecordByMobile("手机号", "待办测试", "待办内容", "截止时间戳", AppKey, AppSecret);