钉钉开发(三)——Java推送钉钉待办任务

 

之前两篇博文中,讲解了如何推送钉钉群消息和工作通知,感兴趣的可以点击查看。

钉钉开发(一)——Java给钉钉群推送消息

钉钉开发(二)——Java推送钉钉工作通知

工作通知的功能已经很强大了,可以对接处理第三方工作任务。但问题是,这毕竟只是个通知,如果接收者看完了通知,忘了处理相关任务,怎么办呢。最好的办法就是发送待办任务!

1、准备工作参照钉钉开发(二)——Java推送钉钉工作通知中所写,此处不再赘述。

2、相关代码如下所示

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *@author: Chris.Mont
 *@date: 2021-06-07 10:53
 *@desc: 钉钉发送待办任务
 */
@Slf4j
@Service
public class DingWorkBiz {

    private String APP_KEY = "dingrxxxxxxxeuhwvgs";
    private String APP_SECRET = "8DjcxxxxxxRrJcooQ-SFRL74QlOmYqy8aNMotxxxxxxxxxxxe_JQNs";
    private Long AGENT_ID = 122229993700L;

    private String MESSAGE_URL = "https://www.baidu.com";
    private String PC_MESSAGE_URL = "https://www.baidu.com";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");

    /**
     * 获取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);
        return response.getAccessToken();
    }
    
    /**
     * 发送待办
     * @param mobile 发送消息人的电话,多个英文逗号拼接
     * @throws ApiException
     */
    public void sendTask(String mobile) throws ApiException {
        log.info("发送钉钉通知");
        String accessToken = getAccessToken();
        if(StringUtils.isBlank(mobile)){
            return;
        }
        //电话号码数组
        String[] split = mobile.split(",");
        for (String s : split) {
            //根据电话号码获取发送人id
            DingTalkClient mobilClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
            OapiUserGetByMobileRequest useReq = new OapiUserGetByMobileRequest();
            useReq.setMobile(s);
            useReq.setHttpMethod("GET");
            OapiUserGetByMobileResponse useRsp = mobilClient.execute(useReq, accessToken);
            //获取到Urid就是在公司里要发送到那个人的id
            String urid = useRsp.getUserid();
            OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
            req.setUserid(urid);
            req.setCreateTime(new Date().getTime());
            req.setTitle("今日学习强国,下班前完成");
            req.setUrl(MESSAGE_URL);
            req.setPcUrl(PC_MESSAGE_URL);
            List<OapiWorkrecordAddRequest.FormItemVo> list2 = new ArrayList<>();
            OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
            list2.add(obj3);
            obj3.setTitle("新人学习,必须完成");
            obj3.setContent("今日学习强国,你完成了吗?");
            req.setFormItemList(list2);
            //发起人id
            req.setOriginatorUserId(urid);
            req.setSourceName("学习~");
            req.setPcOpenType(2L);
            //流程业务id,避免多个业务冲突
            req.setBizId("111212");
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/add");
            OapiWorkrecordAddResponse rsp = client.execute(req, accessToken);
            System.out.println(rsp.getBody());
        }
    }

}

3、发送效果如图所示

点击任务,可以跳转到设置的链接,其功能和工作通知差不多。

相同的待办任务只能发送一次,以设置的bizId为准。 

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值