钉钉机器人调研(企业微应用机器人)
package demo.test.ding;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import java.util.ArrayList;
import java.util.List;
public class RobotChatController {
public static String getToken(String appKey, String appSecret) {
String token = "";
try {
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appKey);
request.setAppsecret(appSecret);
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
token = response.getAccessToken();
return token;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return token;
}
public static String getUserId(String phone, String token) {
String userId = "";
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
req.setMobile(phone);
OapiV2UserGetbymobileResponse rsp = client.execute(req, token);
userId = rsp.getResult().getUserid();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return userId;
}
public static String createChatroom(String userId, List<String> memberList, String chatroomName, String token){
String chatroomId = "";
try{
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/create");
OapiChatCreateRequest req = new OapiChatCreateRequest();
req.setName(chatroomName);
req.setOwner(userId);
req.setUseridlist(memberList);
OapiChatCreateResponse rsp = client.execute(req, token);
chatroomId = rsp.getChatid();
return chatroomId;
}catch (Exception e){
System.out.println(e.getMessage());
}
return chatroomId;
}
public String sendMessage(String chatId,String content,String msgType,String token){
String msg="";
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/send");
OapiChatSendRequest req = new OapiChatSendRequest();
req.setChatid(chatId);
OapiChatSendRequest.Msg obj1 = new OapiChatSendRequest.Msg();
OapiChatSendRequest.Text obj2 = new OapiChatSendRequest.Text();
obj2.setContent(content);
obj1.setText(obj2);
obj1.setMsgtype(msgType);
req.setMsg(obj1);
OapiChatSendResponse rsp = client.execute(req, token);
return rsp.getErrorCode();
}catch (Exception e){
System.out.println(e.getMessage());
}
return msg;
}
public static void main(String[] args) {
String agentId = "3268888";
String appKey = "ding2h8z8hdhaaaabbbb";
String AppSecret = "2MbsExjaAgf6gyZcJjQRhJ_ryaJ-b42ux2bGIBPkcLz-aaaabbbb";
String token = getToken(appKey, AppSecret);
System.out.println(token);
String phone = "18288889999";
String userId = getUserId(phone, token);
System.out.println(userId);
List<String> memberList = new ArrayList<>();
memberList.add("015412088889999");
memberList.add("22686633311788889999");
memberList.add("185739510788889999");
String chatroomName = "公司员工关怀群";
String chatroom = createChatroom(userId, memberList, chatroomName, token);
System.out.println(chatroom);
}
}