1.概述

本文主要介绍如何集成钉钉发送消息。

2.实现步骤

2.1 获取TOKEN

/**
     * 根据appKey和密钥获取访问的token。
     *
     * @return
     * @throws Exception
     */
    public static String getAccessToken (String appKey,String secret) throws ApiException {
        String accessToken="";
        Map<String,Object> tokenMapCache=(Map<String, Object>) CacheUtil.get(REGION,DING_TOKEN_KEY);
        if(tokenMapCache!=null){
            accessToken=tokenMapCache.get("accessToken")==null?"":tokenMapCache.get("accessToken").toString();
            Long oldTime=tokenMapCache.get("curTime")==null?0:Long.parseLong(tokenMapCache.get("curTime").toString());
            Long curTime=System.currentTimeMillis();
            //过期时间之内
            if(StringUtils.isNotEmpty(accessToken)&&(curTime-oldTime)<DING_TOKEN_EXPIRE_TIME){
                return accessToken;
            }
        }
        //没有缓存  就发请求获取
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();

        if(StringUtils.isEmpty(appKey)|| StringUtils.isEmpty(secret)){
            throw new ApiException("没有配置钉钉应用id或秘钥");
        }
        request.setAppkey(appKey);
        request.setAppsecret(secret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        if(response.getErrcode()!=0){
            throw new ApiException(response.getErrmsg());
        }
        Long curTime=System.currentTimeMillis();
        accessToken=response.getAccessToken();
        Map<String,Object> tokenMap=new HashMap<String,Object>();
        tokenMap.put("curTime",curTime);
        tokenMap.put("accessToken",accessToken);
        CacheUtil.set(REGION,DING_TOKEN_KEY,tokenMap);
        return accessToken;
    }

2.2 发送消息

  1. 发送消息
public static OapiMessageCorpconversationAsyncsendV2Response sendMessage(DdMessage message, String token) throws ApiException {

        //获取访问token。

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        //选择钉钉消息通知人
        request.setUseridList(message.getReceivers());
        request.setAgentId(Long.valueOf(message.getAgentId()));
        request.setToAllUser(false);


        request.setMsg(message.getMsg());
        OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request, token);
        return response;
    }
  1. 构建消息
public static OapiMessageCorpconversationAsyncsendV2Request.Msg buildLinkMsg(String subject,String content,String url,String picUrl){
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("link");
        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());

        msg.getLink().setTitle(subject);
        msg.getLink().setText(content);
        msg.getLink().setMessageUrl(url);
        msg.getLink().setPicUrl(picUrl);

        return msg;
    }

构建文本消息

public static OapiMessageCorpconversationAsyncsendV2Request.Msg buildTextMsg(String content){
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        OapiMessageCorpconversationAsyncsendV2Request.Text text= new OapiMessageCorpconversationAsyncsendV2Request.Text();
        text.setContent(content);
        msg.setText(text);

        return msg;
    }

构建Markdown消息

 public static OapiMessageCorpconversationAsyncsendV2Request.Msg buildMarkDownMsg(String title,String content){
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("markdown");
        OapiMessageCorpconversationAsyncsendV2Request.Markdown markdown= new OapiMessageCorpconversationAsyncsendV2Request.Markdown();

        markdown.setTitle(title);
        markdown.setText(content);

        msg.setMarkdown(markdown);

        return msg;
    }

构建卡片消息

public static OapiMessageCorpconversationAsyncsendV2Request.Msg buildCardMsg(String title,String content,String singleTitle,String url){
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("action_card");
        OapiMessageCorpconversationAsyncsendV2Request.ActionCard card= new OapiMessageCorpconversationAsyncsendV2Request.ActionCard();

        card.setTitle(title);
        card.setMarkdown(content);
        card.setSingleTitle(singleTitle);
        card.setSingleUrl(url);

        msg.setActionCard(card);


        return msg;
    }
  1. 发送消息过程
String token= DdUtil.getAccessToken("dingfsnwhhnb5okeojbj","3V7_FoZVvwb7jtKYgKiAqSXSbMk6-Z-H3MT3avYrgKU8fh1d38hJAU0ZwAufwn1L");
        DdMessage message=new DdMessage();
        message.setAgentId("1096470722");
        message.setReceivers("0168020944121096");
//        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=DdMessageUtil.buildLinkMsg("你好","测试钉钉消息","http://www.baidu.com","https://img.alicdn.com/tfs/TB10oldD6DpK1RjSZFrXXa78VXa-240-240.png");
//        message.setMsg(msg);

//        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=DdMessageUtil.buildTextMsg("测试钉钉文本消息消息");
//        message.setMsg(msg);
        //OapiMessageCorpconversationAsyncsendV2Request.Msg msg=DdMessageUtil.buildCardMsg("标题测试","内容测试测试钉钉文本消息消息","查看信息","http://www.163.com");

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg=DdMessageUtil.buildMarkDownMsg("标题测试","内容测试测试钉钉文本消息消息");

        message.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = DdMessageUtil.sendMessage(message, token);
        System.err.println("ok");
文档更新时间: 2021-03-29 11:53   作者:zyg