由于业务需要 系统对接钉钉的开放接口 发送消息 , 经参考与查找资料后发现 要调用钉钉的发送消息接口 都需要有钉钉的应用才可以调用接口
由于是自己系统对接肯定是选企业内部应用了
而企业内部应用又分微应用 & 小程序两种
应用类型介绍 - 钉钉开放平台 其中应用类型的区别需要自己看看
其实就仅仅完成消息通知 如下图类似的效果的话 应用类型无论那个都没什么区别
本文选用的是 微应用
1.创建应用配置 前提你是管理员/子管理员身份 1.1直接创建创建成功后
可以在管理页面看到几个必要的参数
1.2其中还有个关键的参数也注意获取 这里CorpId 是企业标识id 后续发送接口中会用到2.应用申请权限
由于我不是管理员 没有这个页面的截图 但这个设置是在应用的权限管理菜单处 修改
用到那个 申请那个
3.选用确定消息发送类型
钉钉开放的对接接口中能发送的有以下几种
消息通知概述 - 钉钉开放平台
其中使用模板发送工作通知 仅能第三方应用才能使用
- 工作通知
2.企业群消息
企业群窗口消息 以应用名义 直接发消息到企业群里的
3.普通消息
普通对话窗口 ,调用后需要选择对话 以员工个人账号发送消息 本文使用的为 工作通知
3.接入口列表1.GET https://oapi.dingtalk.com/gettoken?appkey=appkey&appsecret=appsecret 获取企业内部应用的access_token https://developers.dingtalk.com/document/app/obtain-orgapp-token
2.POST https://oapi.dingtalk.com/topapi/v2/user/getbymobile?access_token=ACCESS_TOKE 根据手机号获取userid
https://developers.dingtalk.com/document/app/query-users-by-phone-number
3.POST https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=ACCESS_TOKEN 发送工作通知
https://developers.dingtalk.com/document/app/asynchronous-sending-of-enterprise-session-messages
4.POST https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult?access_token=ACCESS_TOKEN 获取工作通知消息的发送结果
https://developers.dingtalk.com/document/app/gets-the-result-of-sending-messages-asynchronously-to-the-enterprise
3.1流程依次对应上述接口1.获取token
2.根据tonken & 手机号获取userid
3. 根据tonken & userid 发送通知
4.根据tonken & 第3步返回的 taskid 获取结果
4.下载sdkhttps://developers.dingtalk.com/document/app/download-the-server-side-sdk
这个链接下载java sdk
4.1.下载的sdk 有两个 将其导入自己的项目 4.2.也可以用mavevn 导入到本地-Dfile : 安装jar文件路径
-DgroupId: 赋予名称
-Dversion: 版本号
-Dpackaging : 打包方式
4.2.1mvn命令 :mvn install:install-file -Dfile=D:AliyunDependencytaobao-sdk-java-auto_1479188381469-20210927.jar -DgroupId=com.taobao -DartifactId=taobao-sdk-java-auto -Dversion=20210927 -Dpackaging=jar mvn install:install-file -Dfile=D:AliyunDependencytaobao-sdk-java-auto_1479188381469-20210927-source.jar -DgroupId=com.taobao -DartifactId=taobao-sdk-java-auto-source -Dversion=20210927 -Dpackaging=jar4.2.2安装后 项目引用
5.代码com.taobao taobao-sdk-java-auto20210927 com.taobao taobao-sdk-java-auto-source20210927
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.OapiMessageCorpconversationGetsendresultRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.TaobaoResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@EnableAutoConfiguration
class ReptileTests {
private static final String corpId = "xxxx";
private static final Long agentId = 1L;
private static final String appKey = "xx";
private static final String appSecret = "xx";
@Test
void dingding() throws Exception {
// 1. 获取accessToken
String accessToken = this.getAccessToken();
// 2. 获取userid
String userByMobile = this.getUserByMobile(accessToken, "13928776144");
// 3. 发送消息
Long taskId = this.send(accessToken, userByMobile);
// 4. 获取通知结果
this.getSendResult(accessToken, taskId);
}
public String getAccessToken() throws Exception {
OapiGettokenRequest request = new OapiGettokenRequest();
// 应用app_key
request.setAppkey(appKey);
// 应用app_secret
request.setAppsecret(appSecret);
// 请求方式 get
request.setHttpMethod("GET");
// 发送请求
OapiGettokenResponse response = (OapiGettokenResponse)
getResponse("https://oapi.dingtalk.com/gettoken", request, null);
// 判空
isEmpty(response, new Exception().getStackTrace()[0].getMethodName());
return response.getAccessToken();
}
public String getUserByMobile(String accessToken, String mobile) throws Exception {
OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
// 手机号码
req.setMobile(mobile);
OapiV2UserGetbymobileResponse rsp = (OapiV2UserGetbymobileResponse)
getResponse("https://oapi.dingtalk.com/topapi/v2/user/getbymobile", req, accessToken);
isEmpty(rsp, new Exception().getStackTrace()[0].getMethodName());
System.out.println(rsp.getBody());
return rsp.getResult().getUserid();
}
public Long send(String accessToken, String userIds) throws Exception {
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setAgentId(agentId);
request.setUseridList(userIds);
request.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("text");
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
msg.getText().setContent("今天测试成功");
request.setMsg(msg);
msg.setMsgtype("image");
msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
msg.getImage().setMediaId("@lADOdvRYes0CbM0CbA");
request.setMsg(msg);
msg.setMsgtype("file");
msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
msg.getFile().setMediaId("@lADOdvRYes0CbM0CbA");
request.setMsg(msg);
msg.setMsgtype("link");
msg.setlink(new OapiMessageCorpconversationAsyncsendV2Request.link());
msg.getlink().setTitle("test");
msg.getlink().setText("test");
msg.getlink().setMessageUrl("test");
msg.getlink().setPicUrl("test");
request.setMsg(msg);
msg.setMsgtype("markdown");
msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
msg.getMarkdown().setText("##### text");
msg.getMarkdown().setTitle("### Title");
request.setMsg(msg);
msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
msg.getOa().getHead().setText("head");
msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
msg.getOa().getBody().setContent("xxx");
msg.setMsgtype("oa");
request.setMsg(msg);
msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
msg.getActionCard().setTitle(DateUtil.now() + "发送测试消息");
msg.getActionCard().setMarkdown("### 测试123111");
msg.getActionCard().setSingleTitle("测试测试");
msg.getActionCard().setSingleUrl("http://airsky.vicp.io:20408/fantas/web/#/");
msg.setMsgtype("action_card");
request.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response rsp = (OapiMessageCorpconversationAsyncsendV2Response)
getResponse("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2", request, accessToken);
isEmpty(rsp, new Exception().getStackTrace()[0].getMethodName());
System.out.println(rsp.getBody());
return rsp.getTaskId();
}
public OapiMessageCorpconversationGetsendresultResponse.AsyncSendResult getSendResult(String accessToken,
Long taskId
) throws Exception {
OapiMessageCorpconversationGetsendresultRequest request = new OapiMessageCorpconversationGetsendresultRequest();
request.setAgentId(agentId);
request.setTaskId(taskId);
OapiMessageCorpconversationGetsendresultResponse rsp = (OapiMessageCorpconversationGetsendresultResponse)
getResponse("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult", request, accessToken);
isEmpty(rsp, new Exception().getStackTrace()[0].getMethodName());
System.out.println(rsp.getBody());
return rsp.getSendResult();
}
private static Object getResponse(String url, TaobaoRequest request, String accessToken) {
try {
// 1. 构建钉钉http请求连接
DingTalkClient client = new DefaultDingTalkClient(url);
// 2. 发送请求
if (StrUtil.isNotBlank(accessToken)) {
return client.execute(request, accessToken);
} else {
return client.execute(request);
}
} catch (ApiException e) {
System.out.println("钉钉请求异常");
e.printStackTrace();
}
return null;
}
private static void isEmpty(TaobaoResponse response, String methodName) throws Exception {
// System.out.println(response.getBody());
// 1. 判空
if (ObjectUtil.isNotNull(response)) {
// 2. 判错误
if (!response.isSuccess()) {
throw new Exception(response.getBody());
}
} else {
throw new Exception("系统请求钉钉" + methodName + "请求返回为null");
}
}
}



