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()); }
本文来自博客园,作者:void_main(),转载请注明原文链接:https://www.cnblogs.com/void--main/p/16532164.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)