在钉钉Demo中,已经提供了示例。
package com.alibaba.dingtalk.openapi.demo;
import com.alibaba.dingtalk.openapi.demo.auth.AuthHelper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.client.api.model.corp.MessageBody;
public class Demo {
public static void main(String[] args) throws Exception {
try {
JSONArray userLst = DeptUserUtility.getUserListByDept("6304333");
System.out.println(userLst.size());
for (int i = 0; i < userLst.size(); i++) {
JSONObject user = userLst.getJSONObject(i);
System.out.println(user.getString("name") + " : " + user.getString("userid"));
}
MessageBody.TextBody textBody = new MessageBody.TextBody();
String accessToken = AuthHelper.getAccessToken();
log("成功获取access token: ", accessToken);
} catch (OApiException e) {
e.printStackTrace();
}
}
private static void log(Object... msgs) {
StringBuilder sb = new StringBuilder();
for (Object o : msgs) {
if (o != null) {
sb.append(o.toString());
}
}
System.out.println(sb.toString());
}
}
在DeptUserUtility.getUserListByDept(“6304333”)方法中调用了AuthHelper.getAccessToken(),这个是获取access_token的,结合配置文件文件读取。
public static JSONArray getUserListByDept(String deptId) throws OApiException, Exception{
JSONArray userJSONArray = new JSONArray();
int offset = 0;
int size = 5;
CorpUserList corpUserList = new CorpUserList();
while (true) {
//分段,5位一取
corpUserList = UserHelper.getDepartmentUser(AuthHelper.getAccessToken(), Long.valueOf(deptId)
, (long)offset, size, null);
for(int j = 0; j < corpUserList.getUserlist().size(); j++){
String user = JSON.toJSONString(corpUserList.getUserlist().get(j));
userJSONArray.add(j + offset, JSONObject.parseObject(user, CorpUserDetail.class));
}
if (Boolean.TRUE.equals(corpUserList.isHasMore())) {
offset += size;
} else {
break;
}
}
return userJSONArray;
}
JSONArray depts = DeptUserUtility.getAllDepts();
System.out.println(depts.size());
for(int i=0;i
DeptUserUtility.getAllDepts()调用了DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), “1”);
public static JSONArray getAllDepts() throws OApiException, Exception{
List departmentLst = new ArrayList();
departmentLst = DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), "1");
JSONArray deptJSONArray = new JSONArray();
for(int i = 0; i < departmentLst.size(); i++){
String department = JSON.toJSONString(departmentLst.get(i));
deptJSONArray.add(i, JSONObject.parseObject(department, Department.class));
}
return deptJSONArray;
}
在DepartmentHelper.listDepartments(String accessToken,String parentDeptId)方法中提供了两种方式。
public static List listDepartments(String accessToken, String parentDeptId)
throws ServiceNotExistException, SdkInitException, ServiceException {
CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
List deptList = corpDepartmentService.getDeptList(accessToken, parentDeptId);
// String url = Env.OAPI_HOST + "/department/list?" +
// "access_token=" + accessToken;
// JSONObject response = HttpHelper.httpGet(url);
// if (response.containsKey("department")) {
// JSONArray arr = response.getJSONArray("department");
// List list = new ArrayList<>();
// for (int i = 0; i < arr.size(); i++) {
// list.add(arr.getObject(i, Department.class));
// }
return deptList;
// }
// else {
// throw new OApiResultException("department");
// }
}
// 获取所有部门成员信息
JSONArray users = DeptUserUtility.getAllDeptUsers();
System.out.println(users.size());
for(int i=0;i
实现方式为,先获取所有的部门,遍历所有的部门,调用根据部门编号获取所有的人员接口即可。
public static JSONArray getAllDeptUsers() throws OApiException, Exception{
List departmentLst = new ArrayList();
departmentLst = DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), "1");
JSONArray deptJSONArray = new JSONArray();
JSONArray userJSONArray = null;
for(int i = 0; i < departmentLst.size(); i++){
JSONObject userDepJSON = new JSONObject();
userDepJSON.put("dept", departmentLst.get(i).getName());
userJSONArray = new JSONArray();
int offset = 0;
int size = 5;
CorpUserList corpUserList = new CorpUserList();
while (true) {
//分段,5位一取
corpUserList = UserHelper.getDepartmentUser(AuthHelper.getAccessToken(), Long.valueOf(departmentLst.get(i).getId())
, (long)offset, size, null);
for(int j = 0; j < corpUserList.getUserlist().size(); j++){
String user = JSON.toJSONString(corpUserList.getUserlist().get(j));
userJSONArray.add(j + offset, JSONObject.parseObject(user, CorpUserDetail.class));
}
if (Boolean.TRUE.equals(corpUserList.isHasMore())) {
offset += size;
} else {
break;
}
}
userDepJSON.put("user", userJSONArray);
deptJSONArray.add(i, userDepJSON);
}
return deptJSONArray;
}
String userId = DeptUserUtility.getDingUserIdByName("黄宝康");
System.out.println("黄宝康的userId="+userId);// 043526291739810174
实现方法为获取所有的部门用户,然后遍历匹配。
public static String getDingUserIdByName(String name) throws OApiException, Exception {
String userId = "";
JSONArray deptUsersLst = getAllDeptUsers();
for (int i = 0; i < deptUsersLst.size(); i++) {
JSONArray usersLst = deptUsersLst.getJSONObject(i).getJSONArray("user");
for (int j = 0; j < usersLst.size(); j++) {
JSONObject user = usersLst.getJSONObject(j);
if (name.equals(user.getString("name"))) {
userId = user.getString("userid");
return userId;
}
}
}
return userId;
}
项目中调用
DDingUtil.sendTaskText(old.getStr("CREATOR"), old.getStr("task_number"), DDingUtil.WARN_CLOSE, DDingUtil.USER_DEAL);
还是调用MessageUtil.send方法,封装了下而已。
package com.wlkj.business.dingtask;
import java.util.ArrayList;
import com.alibaba.dingtalk.openapi.demo.DeptUserUtility;
import com.alibaba.dingtalk.openapi.demo.MessageUtil;
import com.alibaba.dingtalk.openapi.demo.Vars;
import com.dingtalk.open.client.api.model.corp.MessageBody;
import com.dingtalk.open.client.api.model.corp.MessageType;
public class DDingUtil {
/** 处理人 */
public static final String USER_DEAL = "0";
/** 关注人 */
public static final String USER_WATCH = "1";
/** 分配提醒 */
public static final String WARN_ASSIGN = "0";
/** 到期提醒 */
public static final String WARN_AT_TERM = "1";
/** 域名到期提醒 */
public static final String WARN_URL = "2";
/** 运维到期提醒 */
public static final String WARN_ITSM = "3";
/**工单被关闭提醒**/
public static final String WARN_CLOSE = "4";
/**
* 通过钉钉给工单相关人员发送文本消息
* @param username 收信人名
* @param tasknumber 工单号
* @param send_type 0:分配提醒/1:到期提醒
* @param send_user_type 0:处理人/1:关注人
*/
public static void sendTaskText(String user_name, String task_number, String send_type, String send_user_type){
try {
// 向钉钉用户(处理人)发送消息
ArrayList toUserLst = new ArrayList();
toUserLst.add(DeptUserUtility.getDingUserIdByName(user_name));
System.out.println("向钉钉用户(处理人)发送消息");
MessageBody.TextBody textBody = new MessageBody.TextBody();
StringBuffer content = new StringBuffer();
if (WARN_ASSIGN.equals(send_type)) {
//分配提醒
content.append(user_name);
content.append(",您有一条工单:");
content.append(task_number.substring(task_number.indexOf('-')+1));
if (USER_DEAL.equals(send_user_type)) {
content.append(",请尽快登录IT运维服务管理平台进行处理。");
} else if (USER_WATCH.equals(send_user_type)) {
content.append(",请登录IT运维服务管理平台查看详情。");
}
} else if (WARN_AT_TERM.equals(send_type) && USER_DEAL.equals(send_user_type)) {
//到期提醒
content.append(user_name);
content.append(",您有一条工单:");
content.append(task_number.substring(task_number.indexOf('-')+1));
content.append("即将到达截止时间,请尽快登录IT运维服务管理平台进行处理。");
}else if(WARN_CLOSE.equals(send_type)){
// 工单被处理人关闭提醒
content.append(user_name);
content.append(",您的一条工单:");
content.append(task_number.substring(task_number.indexOf('-')+1));
content.append("被处理人关闭,请登录IT运维服务管理平台查看详情。");
}
textBody.setContent(content.toString());
MessageUtil.send(toUserLst, "", Vars.AGENT_ID, MessageType.TEXT, textBody);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void sendProjectText(String user_name, String name, String send_type){
try {
// 向钉钉用户发送消息
ArrayList toUserLst = new ArrayList();
toUserLst.add(DeptUserUtility.getDingUserIdByName(user_name));
MessageBody.TextBody textBody = new MessageBody.TextBody();
StringBuffer content = new StringBuffer();
content.append(user_name);
content.append(",您负责的");
if (WARN_URL.equals(send_type)) {
//域名到期提醒
content.append("域名【" + name + "】");
} else if (WARN_ITSM.equals(send_type)) {
//运维到期提醒
content.append("运维【" + name + "】");
}
content.append("即将到期,请及时登录IT运维服务管理平台进行处理。");
textBody.setContent(content.toString());
MessageUtil.send(toUserLst, "", Vars.AGENT_ID, MessageType.TEXT, textBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}