钉钉服务端开发记录demo1


1、注册企业账号。


2、普通企业版Demo地址:
https://github.com/ddtalk/HarleyCorp


下载后导入eclipse   jdk修改为1.7

4.将您的CorpID和CorpSecret配置在Env.java文件

这2个值在钉钉OA管理后台(https://oa.dingtalk.com/)微应用》微应用设置里面


5.如果测试demo代码

关键点agentid如果得来,点击微应用》应用中心》可以新建一个也可以用已有的,随便一个》点击查看agentid,都可以用来发消息。


package com.alibaba.dingtalk.openapi.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.dingtalk.openapi.demo.auth.AuthHelper;
import com.alibaba.dingtalk.openapi.demo.department.DepartmentHelper;
import com.alibaba.dingtalk.openapi.demo.media.MediaHelper;
import com.alibaba.dingtalk.openapi.demo.message.ConversationMessageDelivery;
import com.alibaba.dingtalk.openapi.demo.message.LightAppMessageDelivery;
import com.alibaba.dingtalk.openapi.demo.message.MessageHelper;
import com.alibaba.dingtalk.openapi.demo.user.User;
import com.alibaba.dingtalk.openapi.demo.user.UserHelper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.client.api.model.corp.CorpUserDetail;
import com.dingtalk.open.client.api.model.corp.CorpUserDetailList;
import com.dingtalk.open.client.api.model.corp.CorpUserList;
import com.dingtalk.open.client.api.model.corp.Department;
import com.dingtalk.open.client.api.model.corp.MessageBody;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body.Form;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body.Rich;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Head;
import com.dingtalk.open.client.api.model.corp.MessageType;
import com.dingtalk.open.client.api.model.corp.UploadResult;

public class Demo {

	public static void main(String[] args) throws Exception {

		try {

			List departments = new ArrayList();
			departments = DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), "1");
			JSONObject usersJSON = new JSONObject();
			
			System.out.println("depart num:"+departments.size());
			for(int i = 0;i formList = new ArrayList
(); Form form = new Form(); form.setKey("key"); form.setValue("value"); formList.add(form); body.setForm(formList); oaBody.setBody(body); // 发送微应用消息 String toUsers = Vars.TO_USER; String toParties = Vars.TO_PARTY; String agentId = Vars.AGENT_ID; LightAppMessageDelivery lightAppMessageDelivery = new LightAppMessageDelivery(toUsers, toParties, agentId); lightAppMessageDelivery.withMessage(MessageType.TEXT, textBody); MessageHelper.send(accessToken, lightAppMessageDelivery); log("成功发送 微应用文本消息"); lightAppMessageDelivery.withMessage(MessageType.IMAGE, imageBody); MessageHelper.send(accessToken, lightAppMessageDelivery); log("成功发送 微应用图片消息"); lightAppMessageDelivery.withMessage(MessageType.LINK, linkBody); MessageHelper.send(accessToken, lightAppMessageDelivery); log("成功发送 微应用link消息"); lightAppMessageDelivery.withMessage(MessageType.OA, oaBody); MessageHelper.send(accessToken, lightAppMessageDelivery); log("成功发送 微应用oa消息"); // 发送会话消息 // String sender = Vars.SENDER; // String cid = Vars.CID;//cid需要通过jsapi获取,具体详情请查看开放平台文档--->客户端文档--->会话 // ConversationMessageDelivery conversationMessageDelivery = new ConversationMessageDelivery(sender, cid, // agentId); // // conversationMessageDelivery.withMessage(MessageType.TEXT, textBody); // MessageHelper.send(accessToken, conversationMessageDelivery); // log("成功发送 会话文本消息"); // conversationMessageDelivery.withMessage(MessageType.IMAGE, imageBody); // MessageHelper.send(accessToken, conversationMessageDelivery); // log("成功发送 会话图片消息"); // conversationMessageDelivery.withMessage(MessageType.LINK, linkBody); // MessageHelper.send(accessToken, conversationMessageDelivery); // log("成功发送 会话link消息"); // 更新成员 userDetail.setMobile("11177776666"); UserHelper.updateUser(accessToken, userDetail); log("成功更新成员", "成员信息=", userDetail); // 获取成员 CorpUserDetail userDetail11 = UserHelper.getUser(accessToken, userDetail.getUserid()); log("成功获取成员", "成员userid=", userDetail11.getUserid()); // 获取部门成员 CorpUserList userList = UserHelper.getDepartmentUser(accessToken, departmentId, null, null, null); log("成功获取部门成员", "部门成员user=", userList.getUserlist()); // 获取部门成员(详情) CorpUserDetailList userList2 = UserHelper.getUserDetails(accessToken, departmentId, null, null, null); log("成功获取部门成员详情", "部门成员详情user=", userList2.getUserlist()); // 批量删除成员 // User user2 = new User("id_yuhuan2", "name_yuhuan2"); // user2.email = "[email protected]"; // user2.mobile = "18611111111"; // user2.department = new ArrayList(); // user2.department.add(departmentId); // UserHelper.createUser(accessToken, user2); CorpUserDetail userDetail2 = new CorpUserDetail(); userDetail2.setUserid("id_yuhuan2"); userDetail2.setName("name_yuhuan2"); userDetail2.setEmail("[email protected]"); userDetail2.setMobile("18612124926"); userDetail2.setDepartment(new ArrayList()); userDetail2.getDepartment().add(departmentId); UserHelper.createUser(accessToken, userDetail2); List useridlist = new ArrayList(); useridlist.add(userDetail.getUserid()); useridlist.add(userDetail2.getUserid()); UserHelper.batchDeleteUser(accessToken, useridlist); log("成功批量删除成员", "成员列表useridlist=", useridlist); // 删除成员 // User user3 = new User("id_yuhuan3", "name_yuhuan3"); // user3.email = "[email protected]"; // user3.mobile = "18611111111"; // user3.department = new ArrayList(); // user3.department.add(departmentId); CorpUserDetail userDetail3 = new CorpUserDetail(); userDetail3.setUserid("id_yuhuan3"); userDetail3.setName("name_yuhuan3"); userDetail3.setMobile("13146654734"); userDetail3.setDepartment(new ArrayList()); userDetail3.getDepartment().add(departmentId); UserHelper.createUser(accessToken, userDetail3); UserHelper.deleteUser(accessToken, userDetail3.getUserid()); log("成功删除成员", "成员userid=", userDetail3.getUserid()); // 删除部门 DepartmentHelper.deleteDepartment(accessToken, departmentId); log("成功删除部门", " 部门id=", departmentId); } 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()); } }





你可能感兴趣的:(第三方平台)