钉钉调用新版待办任务

前言

今天公司通知需求需要使用钉钉的待办任务,查了下API发现待办任务在上个月的时候更新了接口,旧版的使用不了,而新版的接口也还没有教程,故写了这篇文章。

这里是API链接 钉钉新增待办任务


一、准备工作

  1. 首先要在钉钉开发者后台创建一个应用,然后开放待办任务下的所有权限。

  2. 然后要下载钉钉jar包,我用的是21年八九月的包,在maven上没法自动导入,这里提供下载链接。
    链接:https://pan.baidu.com/s/1YG3l8THirXVtrhgHWZbLwQ 提取码:qqs5

  3. 接着是新版待办要使用的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);
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值