java springboot 对接钉钉发消息

一、需要在钉钉开发后台登录有开发者权限的账号建立相关应用并拿到相关参数

二、引入maven

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>

 

三、直接上工具类代码(可以用code换取ddid)

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.OapiV2UserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;


@Configuration
public class DingdingUtil {


    @Value("${dingding.Appkey}")
    private String Appkey;


    @Value("${dingding.Appsecret}")
    private String Appsecret;


    @Value("${dingding.AgentId}")
    private Long AgentId;

    public String getToken(){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.setAppkey(Appkey);
            req.setAppsecret(Appsecret);
            req.setHttpMethod("GET");
            OapiGettokenResponse rsp = client.execute(req);
//            System.out.println(rsp.getBody());
            return rsp.getAccessToken();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String getUserId(String code) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
        OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
        req.setCode(code);
        OapiV2UserGetuserinfoResponse rsp = client.execute(req, getToken());
//        System.out.println(rsp.getBody());

        if (rsp.getErrcode()!=0) {
           throw new ServiceException(rsp.getErrmsg());
        }
       return rsp.getResult().getUserid();
    }

    public void sendMsg(String ddId,String text) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(AgentId);
        request.setUseridList(ddId);
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        text = DateUtils.getTime() +"\n\n"+text;
        msg.getText().setContent(text);
        request.setMsg(msg);

        /*msg.setMsgtype("link");
        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
        msg.getLink().setTitle("这是个标题");
        msg.getLink().setText("这是个内容");
        msg.getLink().setMessageUrl("http://218.24.35.83:81");
        msg.getLink().setPicUrl("test");
        request.setMsg(msg);*/

        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, getToken());
//        System.out.println(rsp.getBody());
    }
}

四、其他相关接口

/**
     * 钉钉登录入口
     * @param res
     * @param corpId
     * @throws IOException
     */
    @RequestMapping("/loginDingding")
    public void loginDingding(HttpServletResponse res,String corpId) throws IOException {
        res.sendRedirect("/index.html?corpId="+corpId+"&myip="+serverConfig.getUrl());
    }

 

posted @ 2022-07-29 14:33  void_main()  阅读(2421)  评论(1)    收藏  举报