public boolean sendDingDingMsg(List<String> adminPhones,String content){
log.info("发送钉钉通知");
String accessToken = getAccessToken();
if (CollectionUtils.isEmpty(adminPhones)){
throw new IgnoredException(ErrorCode.SYS_ERROR,"手机号为空");
}
for (String adminPhone : adminPhones) {
DingTalkClient client = new DefaultDingTalkClient(DingTalkConstant.GET_BY_MOBILE);
OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
req.setMobile(adminPhone);
req.setHttpMethod("GET");
OapiUserGetByMobileResponse rsp = null;
try {
rsp = client.execute(req,accessToken);
System.out.println("rsp = " + rsp.getUserid());
} catch (ApiException e) {
log.error("发送钉钉请求异常:{}",e.getMessage());
//throw new RuntimeException(e);
}
//获取到Urid就是在公司里要发送到那个人的id
String urid = rsp.getUserid();
DingTalkClient client2 = new DefaultDingTalkClient(DingTalkConstant.GET_BY_USER_ID);
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setUseridList(urid);
request.setAgentId(DingTalkConstant.AGENT_ID);
request.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
/* msg.setMsgtype("text");
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
msg.getText().setContent(content);*/
msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
//msg.getOa().getHead().setText("您有一项工作待处理");
msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
msg.getOa().getBody().setContent(content);
msg.setMsgtype("oa");
request.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response asyncsendV2Response;
try {
asyncsendV2Response = client2.execute(request,accessToken);
} catch (ApiException e) {
log.error("发送消息异常:{}",e.getMessage());
return false;
//throw new RuntimeException(e);
}
log.info("发送消息是否成功"+asyncsendV2Response.isSuccess());
//System.out.println(asyncsendV2Response.isSuccess());
}
return true;
}
/*public static void main(String[] args) {
sendDingDingMsg();
}*/
private String getAccessToken() {
DefaultDingTalkClient client =
new DefaultDingTalkClient(DingTalkConstant.GET_ACCESS_TOKEN_URL);
OapiGettokenRequest request = new OapiGettokenRequest();
if(dingtalkConfig.getAppKey()==null && dingtalkConfig.getAppSecret()==null){
log.error("AppKey和AppSecret为空");
}
request.setAppkey(dingtalkConfig.getAppKey());
//Appsecret
request.setAppsecret(dingtalkConfig.getAppSecret());
//请求方式
request.setHttpMethod("GET");
/* OapiGettokenResponse response = client.execute(request);
return response.getAccessToken();*/
try {
OapiGettokenResponse response = client.execute(request);
if (!Objects.isNull(response)) {
return response.getAccessToken();
} else {
log.error("获取accessToken响应为空");
}
} catch (ApiException e) {
// 处理异常
e.printStackTrace();
}
return null;
}
}
09-02
1078
12-06
2204
03-29
08-27
2532
04-07
7061
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交