useridlist = new ArrayList();
246 | useridlist.add(userDetail.getUserid());
247 | useridlist.add(userDetail2.getUserid());
248 | UserHelper.batchDeleteUser(accessToken, useridlist);
249 | log("成功批量删除成员", "成员列表useridlist=", useridlist);
250 |
251 | // 删除成员
252 | // User user3 = new User("id_yuhuan3", "name_yuhuan3");
253 | // user3.email = "yuhua2n@abc.com";
254 | // user3.mobile = "18611111111";
255 | // user3.department = new ArrayList();
256 | // user3.department.add(departmentId);
257 | CorpUserDetail userDetail3 = new CorpUserDetail();
258 | userDetail3.setUserid("id_yuhuan3");
259 | userDetail3.setName("name_yuhuan3");
260 | userDetail3.setMobile("13146654734");
261 | userDetail3.setDepartment(new ArrayList());
262 | userDetail3.getDepartment().add(departmentId);
263 |
264 |
265 | UserHelper.createUser(accessToken, userDetail3);
266 | UserHelper.deleteUser(accessToken, userDetail3.getUserid());
267 | log("成功删除成员", "成员userid=", userDetail3.getUserid());
268 |
269 | // 删除部门
270 | DepartmentHelper.deleteDepartment(accessToken, departmentId);
271 | log("成功删除部门", " 部门id=", departmentId);
272 |
273 | } catch (OApiException e) {
274 | e.printStackTrace();
275 | }
276 | }
277 |
278 | private static void log(Object... msgs) {
279 | StringBuilder sb = new StringBuilder();
280 | for (Object o : msgs) {
281 | if (o != null) {
282 | sb.append(o.toString());
283 | }
284 | }
285 | System.out.println(sb.toString());
286 | }
287 | }
288 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/Env.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo;
2 |
3 |
4 | /**
5 | * 企业应用接入时的常量定义
6 | */
7 | public class Env {
8 |
9 | /**
10 | * 企业corpid
11 | */
12 | public static final String CORP_ID = "";
13 |
14 | /**
15 | * 应用agentId
16 | */
17 | public static final String AGENT_ID = "";
18 |
19 | /**
20 | * 应用的appkey
21 | */
22 | public static final String APP_KEY = "";
23 |
24 | /**
25 | * 应用的appsecret
26 | */
27 | public static final String APP_SECRET = "";
28 |
29 | /**
30 | * 回调host
31 | */
32 | public static final String CALLBACK_URL_HOST = "";
33 |
34 | /**
35 | * 加解密需要用到的token,企业可以随机填写。如 "123456"
36 | */
37 | public static final String TOKEN = "123456";
38 |
39 | /**
40 | * 数据加密密钥。用于回调数据的加密,长度固定为43个字符,从a-z, A-Z, 0-9共62个字符中选取,您可以随机生成
41 | */
42 | public static final String ENCODING_AES_KEY = "abcdefghijabcdefghijabcdefghijabcdefghij123";
43 |
44 | /**
45 | * DING API地址
46 | */
47 | public static final String OAPI_HOST = "https://oapi.dingtalk.com";
48 |
49 | /**
50 | * 删除企业回调接口url
51 | */
52 | public static final String DELETE_CALLBACK = "https://oapi.dingtalk.com/call_back/delete_call_back";
53 |
54 | /**
55 | * 注册企业回调接口url
56 | */
57 | public static final String REGISTER_CALLBACK = "https://oapi.dingtalk.com/call_back/register_call_back";
58 |
59 | /**
60 | * 企业应用后台地址,用户管理后台免登使用
61 | */
62 | public static final String OA_BACKGROUND_URL = "";
63 |
64 | public static final String SSO_Secret = "";
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/OApiException.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo;
2 |
3 | public class OApiException extends Exception {
4 |
5 | public static final int ERR_RESULT_RESOLUTION = -2;
6 |
7 | public OApiException(String field) {
8 | this(ERR_RESULT_RESOLUTION, "Cannot resolve field " + field + " from oapi resonpse");
9 | }
10 |
11 | public OApiException(int errCode, String errMsg) {
12 | super("error code: " + errCode + ", error message: " + errMsg);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/auth/AuthHelper.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.auth;
2 |
3 | import com.alibaba.dingtalk.openapi.demo.Env;
4 | import com.alibaba.dingtalk.openapi.demo.OApiException;
5 | import com.alibaba.dingtalk.openapi.demo.utils.FileUtils;
6 | import com.alibaba.dingtalk.openapi.demo.utils.HttpHelper;
7 | import com.alibaba.fastjson.JSON;
8 | import com.alibaba.fastjson.JSONObject;
9 | import com.dingtalk.oapi.lib.aes.DingTalkJsApiSingnature;
10 | import com.dingtalk.open.client.ServiceFactory;
11 | import com.dingtalk.open.client.api.model.corp.JsapiTicket;
12 | import com.dingtalk.open.client.api.service.corp.CorpConnectionService;
13 | import com.dingtalk.open.client.api.service.corp.JsapiService;
14 |
15 | import javax.servlet.http.HttpServletRequest;
16 | import java.net.URLDecoder;
17 | import java.util.HashMap;
18 | import java.util.Map;
19 |
20 | /**
21 | * AccessToken和jsticket的获取封装
22 | */
23 | public class AuthHelper {
24 |
25 | /**
26 | * 调整到1小时50分钟
27 | */
28 | public static final long cacheTime = 1000 * 60 * 55 * 2;
29 |
30 | /**
31 | * 在此方法中,为了避免频繁获取access_token,
32 | * 在距离上一次获取access_token时间在两个小时之内的情况,
33 | * 将直接从持久化存储中读取access_token
34 | *
35 | * 因为access_token和jsapi_ticket的过期时间都是7200秒
36 | * 所以在获取access_token的同时也去获取了jsapi_ticket
37 | * 注:jsapi_ticket是在前端页面JSAPI做权限验证配置的时候需要使用的
38 | * 具体信息请查看开发者文档--权限验证配置
39 | */
40 | public static String getAccessToken() throws OApiException {
41 | long curTime = System.currentTimeMillis();
42 | JSONObject accessTokenValue = (JSONObject) FileUtils.getValue("accesstoken", Env.APP_KEY);
43 | String accToken = "";
44 | JSONObject jsontemp = new JSONObject();
45 | if (accessTokenValue == null || curTime - accessTokenValue.getLong("begin_time") >= cacheTime) {
46 | try {
47 | ServiceFactory serviceFactory = ServiceFactory.getInstance();
48 | CorpConnectionService corpConnectionService = serviceFactory.getOpenService(CorpConnectionService.class);
49 | accToken = corpConnectionService.getCorpToken(Env.APP_KEY, Env.APP_SECRET);
50 | // save accessToken
51 | JSONObject jsonAccess = new JSONObject();
52 | jsontemp.clear();
53 | jsontemp.put("access_token", accToken);
54 | jsontemp.put("begin_time", curTime);
55 | jsonAccess.put(Env.APP_KEY, jsontemp);
56 | //真实项目中最好保存到数据库中
57 | FileUtils.write2File(jsonAccess, "accesstoken");
58 |
59 | } catch (Exception e) {
60 | e.printStackTrace();
61 | }
62 | } else {
63 | return accessTokenValue.getString("access_token");
64 | }
65 | return accToken;
66 | }
67 |
68 | /**
69 | * 获取JSTicket, 用于js的签名计算
70 | * 正常的情况下,jsapi_ticket的有效期为7200秒,所以开发者需要在某个地方设计一个定时器,定期去更新jsapi_ticket
71 | */
72 | public static String getJsapiTicket(String accessToken) throws OApiException {
73 | JSONObject jsTicketValue = (JSONObject) FileUtils.getValue("jsticket", Env.APP_KEY);
74 | long curTime = System.currentTimeMillis();
75 | String jsTicket = "";
76 |
77 | if (jsTicketValue == null || curTime -
78 | jsTicketValue.getLong("begin_time") >= cacheTime) {
79 | ServiceFactory serviceFactory;
80 | try {
81 | serviceFactory = ServiceFactory.getInstance();
82 | JsapiService jsapiService = serviceFactory.getOpenService(JsapiService.class);
83 |
84 | JsapiTicket JsapiTicket = jsapiService.getJsapiTicket(accessToken, "jsapi");
85 | jsTicket = JsapiTicket.getTicket();
86 |
87 | JSONObject jsonTicket = new JSONObject();
88 | JSONObject jsontemp = new JSONObject();
89 | jsontemp.clear();
90 | jsontemp.put("ticket", jsTicket);
91 | jsontemp.put("begin_time", curTime);
92 | jsonTicket.put(Env.APP_KEY, jsontemp);
93 | FileUtils.write2File(jsonTicket, "jsticket");
94 | } catch (Exception e) {
95 | e.printStackTrace();
96 | }
97 | return jsTicket;
98 | } else {
99 | return jsTicketValue.getString("ticket");
100 | }
101 | }
102 |
103 | public static String sign(String ticket, String nonceStr, long timeStamp, String url) throws OApiException {
104 | try {
105 | return DingTalkJsApiSingnature.getJsApiSingnature(url, nonceStr, timeStamp, ticket);
106 | } catch (Exception ex) {
107 | throw new OApiException(0, ex.getMessage());
108 | }
109 | }
110 |
111 | /**
112 | * 计算当前请求的jsapi的签名数据
113 | *
114 | * 如果签名数据是通过ajax异步请求的话,签名计算中的url必须是给用户展示页面的url
115 | *
116 | * @param request
117 | * @return
118 | */
119 | public static String getConfig(HttpServletRequest request) {
120 | String urlString = request.getRequestURL().toString();
121 | String queryString = request.getQueryString();
122 |
123 | String queryStringEncode = null;
124 | String url;
125 | if (queryString != null) {
126 | queryStringEncode = URLDecoder.decode(queryString);
127 | url = urlString + "?" + queryStringEncode;
128 | } else {
129 | url = urlString;
130 | }
131 | /**
132 | * 确认url与配置的应用首页地址一致
133 | */
134 | System.out.println(url);
135 |
136 | /**
137 | * 随机字符串
138 | */
139 | String nonceStr = "abcdefg";
140 | long timeStamp = System.currentTimeMillis() / 1000;
141 | String signedUrl = url;
142 | String accessToken = null;
143 | String ticket = null;
144 | String signature = null;
145 |
146 | try {
147 | accessToken = AuthHelper.getAccessToken();
148 |
149 | ticket = AuthHelper.getJsapiTicket(accessToken);
150 | signature = AuthHelper.sign(ticket, nonceStr, timeStamp, signedUrl);
151 |
152 | } catch (OApiException e) {
153 | e.printStackTrace();
154 | }
155 |
156 | Map configValue = new HashMap<>();
157 | configValue.put("jsticket", ticket);
158 | configValue.put("signature", signature);
159 | configValue.put("nonceStr", nonceStr);
160 | configValue.put("timeStamp", timeStamp);
161 | configValue.put("corpId", Env.CORP_ID);
162 | configValue.put("agentId", Env.AGENT_ID);
163 |
164 | String config = JSON.toJSONString(configValue);
165 | return config;
166 | }
167 |
168 | public static String getSsoToken() throws OApiException {
169 | String url = "https://oapi.dingtalk.com/sso/gettoken?corpid=" + Env.CORP_ID + "&corpsecret=" + Env.SSO_Secret;
170 | JSONObject response = HttpHelper.httpGet(url);
171 | String ssoToken;
172 | if (response.containsKey("access_token")) {
173 | ssoToken = response.getString("access_token");
174 | } else {
175 | throw new OApiException("Sso_token");
176 | }
177 | return ssoToken;
178 |
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/department/DepartmentHelper.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.department;
2 |
3 | import com.dingtalk.open.client.ServiceFactory;
4 | import com.dingtalk.open.client.api.model.corp.Department;
5 | import com.dingtalk.open.client.api.service.corp.CorpDepartmentService;
6 | import com.dingtalk.open.client.common.SdkInitException;
7 | import com.dingtalk.open.client.common.ServiceException;
8 | import com.dingtalk.open.client.common.ServiceNotExistException;
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * 部门相关API
14 | *
15 | * https://open-doc.dingtalk.com/docs/doc.htm?treeId=371&articleId=106817&docType=1
16 | */
17 | public class DepartmentHelper {
18 |
19 | /**
20 | * 创建部门
21 | */
22 | public static String createDepartment(String accessToken, String name,
23 | String parentId, String order, boolean createDeptGroup) throws Exception {
24 |
25 | CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
26 | return corpDepartmentService.deptCreate(accessToken, name, parentId, order, createDeptGroup);
27 | }
28 |
29 | /**
30 | * 获取部门列表
31 | */
32 | public static List listDepartments(String accessToken, String parentDeptId)
33 | throws ServiceNotExistException, SdkInitException, ServiceException {
34 | CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
35 | List deptList = corpDepartmentService.getDeptList(accessToken, parentDeptId);
36 | return deptList;
37 | }
38 |
39 |
40 | /**
41 | * 删除部门
42 | */
43 | public static void deleteDepartment(String accessToken, Long id) throws Exception {
44 | CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
45 | corpDepartmentService.deptDelete(accessToken, id);
46 | }
47 |
48 | /**
49 | * 更新部门
50 | */
51 | public static void updateDepartment(String accessToken, long id, String name,
52 | String parentId, String order, Boolean createDeptGroup,
53 | boolean autoAddUser, String deptManagerUseridList, boolean deptHiding, String deptPerimits,
54 | String userPerimits, Boolean outerDept, String outerPermitDepts,
55 | String outerPermitUsers, String orgDeptOwner) throws Exception {
56 | CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
57 | corpDepartmentService.deptUpdate(accessToken, id, name, parentId, order, createDeptGroup,
58 | autoAddUser, deptManagerUseridList, deptHiding, deptPerimits, userPerimits,
59 | outerDept, outerPermitDepts, outerPermitUsers, orgDeptOwner);
60 |
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/eventchange/eventChangeHelper.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.eventchange;
2 |
3 | import com.alibaba.dingtalk.openapi.demo.Env;
4 | import com.alibaba.dingtalk.openapi.demo.OApiException;
5 | import com.alibaba.dingtalk.openapi.demo.utils.HttpHelper;
6 | import com.alibaba.fastjson.JSONObject;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * 通讯录回调相关事件
12 | *
13 | * https://open-doc.dingtalk.com/docs/doc.htm?treeId=371&articleId=104975&docType=1
14 | */
15 | public class eventChangeHelper {
16 |
17 | /**
18 | * 注册事件回调接口
19 | */
20 | public static String registerEventChange(String accessToken, List callBackTag, String token, String aesKey, String url) throws OApiException {
21 | String signUpUrl = Env.OAPI_HOST + "/call_back/register_call_back?" +
22 | "access_token=" + accessToken;
23 | JSONObject args = new JSONObject();
24 | args.put("call_back_tag", callBackTag);
25 | args.put("token", token);
26 | args.put("aes_key", aesKey);
27 | args.put("url", url);
28 |
29 | JSONObject response = HttpHelper.httpPost(signUpUrl, args);
30 | if (response.containsKey("errcode")) {
31 | return response.getString("errcode");
32 | } else {
33 | return null;
34 | }
35 | }
36 |
37 | //查询事件回调接口
38 | public static String getEventChange(String accessToken) throws OApiException {
39 | String url = Env.OAPI_HOST + "/call_back/get_call_back?" +
40 | "access_token=" + accessToken;
41 | JSONObject response = HttpHelper.httpGet(url);
42 | return response.toString();
43 | }
44 |
45 | //更新事件回调接口
46 | public static String updateEventChange(String accessToken, List callBackTag, String token, String aesKey, String url) throws OApiException {
47 | String signUpUrl = Env.OAPI_HOST + "/call_back/update_call_back?" +
48 | "access_token=" + accessToken;
49 | JSONObject args = new JSONObject();
50 | args.put("call_back_tag", callBackTag);
51 | args.put("token", token);
52 | args.put("aes_key", aesKey);
53 | args.put("url", url);
54 |
55 | JSONObject response = HttpHelper.httpPost(signUpUrl, args);
56 | if (response.containsKey("errcode")) {
57 | return response.getString("errcode");
58 | } else {
59 | return null;
60 | }
61 | }
62 |
63 | //删除事件回调接口
64 | public static String deleteEventChange(String accessToken) throws OApiException {
65 | String url = Env.OAPI_HOST + "/call_back/delete_call_back?" +
66 | "access_token=" + accessToken;
67 | JSONObject response = HttpHelper.httpGet(url);
68 | return response.toString();
69 | }
70 |
71 |
72 | public static String getFailedResult(String accessToken) throws OApiException {
73 | String url = Env.OAPI_HOST + "/call_back/get_call_back_failed_result?" +
74 | "access_token=" + accessToken;
75 | JSONObject response = HttpHelper.httpGet(url);
76 | return response.toString();
77 | }
78 |
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/media/MediaHelper.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.media;
2 |
3 | import com.alibaba.dingtalk.openapi.demo.Env;
4 | import com.alibaba.dingtalk.openapi.demo.utils.HttpHelper;
5 | import com.alibaba.fastjson.JSONObject;
6 | import com.dingtalk.open.client.ServiceFactory;
7 | import com.dingtalk.open.client.api.model.corp.UploadResult;
8 | import com.dingtalk.open.client.api.service.corp.MediaService;
9 |
10 | import java.io.File;
11 |
12 | /**
13 | * 管理多媒体文件
14 | * https://open-doc.dingtalk.com/docs/doc.htm?source=search&treeId=373&articleId=104971&docType=1
15 | */
16 | public class MediaHelper {
17 |
18 | /**
19 | * 资源文件类型
20 | */
21 | public static final String TYPE_IMAGE = "image";
22 | public static final String TYPE_VOICE = "voice";
23 | public static final String TYPE_VIDEO = "video";
24 | public static final String TYPE_FILE = "file";
25 |
26 |
27 | public static class MediaUploadResult {
28 | public String type;
29 | public String media_id;
30 | public String created_at;
31 | }
32 |
33 | /**
34 | * 上传多媒体文件
35 | *
36 | */
37 | public static UploadResult upload(String accessToken, String type, File file) throws Exception {
38 |
39 | MediaService mediaService = ServiceFactory.getInstance().getOpenService(MediaService.class);
40 | UploadResult uploadResult = mediaService.uploadMediaFile(accessToken, type, file);
41 | return uploadResult;
42 | }
43 |
44 | /**
45 | * 下载多媒体文件,目前sdk没有封装此接口,需要通过HTTP访问
46 | */
47 | public static void download(String accessToken, String mediaId, String fileDir) throws Exception {
48 | String url = Env.OAPI_HOST + "/media/downloadFile?" +
49 | "access_token=" + accessToken + "&media_id=" + mediaId;
50 | JSONObject response = HttpHelper.downloadMedia(url, fileDir);
51 | System.out.println(response);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/ConversationMessageDelivery.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 |
5 | public class ConversationMessageDelivery extends MessageDelivery {
6 |
7 | public String sender;
8 | public String cid;
9 | public String agentid;
10 |
11 | public ConversationMessageDelivery(String sender, String cid,
12 | String agentId) {
13 | this.sender = sender;
14 | this.cid = cid;
15 | this.agentid = agentId;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/ImageMessage.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 |
4 | public class ImageMessage extends Message {
5 |
6 | public String media_id;
7 |
8 | public ImageMessage(String mediaId) {
9 | super();
10 | media_id = mediaId;
11 | }
12 |
13 | @Override
14 | public String type() {
15 | return "image";
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/LightAppMessageDelivery.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 |
5 | public class LightAppMessageDelivery extends MessageDelivery {
6 |
7 | public String touser;
8 | public String toparty;
9 | public String agentid;
10 |
11 | public LightAppMessageDelivery(String toUsers, String toParties, String agentId) {
12 | this.touser = toUsers;
13 | this.toparty = toParties;
14 | this.agentid = agentId;
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/LinkMessage.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 | public class LinkMessage extends Message {
4 |
5 | public String messageUrl;
6 | public String picUrl;
7 | public String title;
8 | public String text;
9 |
10 | public LinkMessage(String messageUrl, String picUrl, String title, String text) {
11 | super();
12 | this.messageUrl = messageUrl;
13 | this.picUrl = picUrl;
14 | this.title = title;
15 | this.text = text;
16 | }
17 |
18 | @Override
19 | public String type() {
20 | return "link";
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/Message.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 |
4 | public abstract class Message {
5 | public abstract String type();
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/MessageDelivery.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.alibaba.fastjson.JSONObject;
5 | import com.dingtalk.open.client.api.model.corp.MessageBody;
6 |
7 | public class MessageDelivery {
8 |
9 | public String msgType;
10 | public MessageBody message;
11 |
12 | public MessageDelivery withMessage(String msgType, MessageBody msg) {
13 | this.msgType = msgType;
14 | this.message = msg;
15 | return this;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/MessageHelper.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 | import com.dingtalk.open.client.ServiceFactory;
4 | import com.dingtalk.open.client.api.model.corp.MessageSendResult;
5 | import com.dingtalk.open.client.api.service.corp.MessageService;
6 |
7 | /**
8 | * 发送消息
9 | */
10 | public class MessageHelper {
11 |
12 | public static class Receipt {
13 | String invaliduser;
14 | String invalidparty;
15 | }
16 |
17 | /**
18 | * 发送普通消息
19 | *
20 | * @param accessToken
21 | * @param delivery
22 | * @return
23 | * @throws Exception
24 | */
25 | public static Receipt send(String accessToken, LightAppMessageDelivery delivery)
26 | throws Exception {
27 | MessageService messageService = ServiceFactory.getInstance().getOpenService(MessageService.class);
28 | MessageSendResult reulst = messageService.sendToCorpConversation(accessToken, delivery.touser,
29 | delivery.toparty, delivery.agentid, delivery.msgType, delivery.message);
30 | Receipt receipt = new Receipt();
31 | receipt.invaliduser = reulst.getInvaliduser();
32 | receipt.invalidparty = reulst.getInvalidparty();
33 | return receipt;
34 | }
35 |
36 |
37 | public static String send(String accessToken, ConversationMessageDelivery delivery)
38 | throws Exception {
39 | MessageService messageService = ServiceFactory.getInstance().getOpenService(MessageService.class);
40 | return messageService.sendToNormalConversation(accessToken, delivery.sender,
41 | delivery.cid, delivery.msgType, delivery.message);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/alibaba/dingtalk/openapi/demo/message/OAMessage.java:
--------------------------------------------------------------------------------
1 | package com.alibaba.dingtalk.openapi.demo.message;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | {
7 | "message_url": "http://dingtalk.com",
8 | "head": {
9 | "bgcolor": "FFCC0000"
10 | },
11 | "body": {
12 | "title": "标题",
13 | "form": [
14 | {
15 | "key": "姓名",
16 | "value": "张三"
17 | },
18 | {
19 | "key": "年龄",
20 | "value": "30"
21 | }
22 | ],
23 | "rich": {
24 | "num": "15.6",
25 | "unit": "元"
26 | },
27 | "content": "大段文本",
28 | "image": "@lADOAAGXIszazQKA",
29 | "file_count": "3",
30 | "author": "李四"
31 | }
32 | */
33 | public class OAMessage extends Message {
34 |
35 | public String message_url;
36 | public Head head;
37 | public Body body;
38 |
39 |
40 | @Override
41 | public String type() {
42 | return "oa";
43 | }
44 |
45 | //content
46 | public static class Head {
47 | public String bgcolor;
48 | }
49 |
50 | public static class Body {
51 | public String title;
52 | public List