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 @   void_main()  阅读(2272)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示