提交 35d4ca09 编写于 作者: zhouweidong's avatar zhouweidong

补充agentid及异常处理

上级 07cfa742
package cn.ibizlab.core.extensions.helper;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiWorkrecordAddRequest;
import com.dingtalk.api.request.OapiWorkrecordUpdateRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiWorkrecordAddResponse;
import com.dingtalk.api.response.OapiWorkrecordUpdateResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 钉钉api辅助类
*/
public class DingTalkHelper {
/**
* 钉钉获取token api
*/
private static final String dingTalkTokenApi="https://oapi.dingtalk.com/gettoken";
/**
* 钉钉发送消息api
*/
private static final String dingTalkSendMsgApi="https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
/**
* 钉钉创建待办api
*/
private static final String dingTalkCreateWorkRecordApi="https://oapi.dingtalk.com/topapi/workrecord/add";
/**
* 钉钉完成待办api
*/
private static final String dingTalkFinishWorkRecordApi="https://oapi.dingtalk.com/topapi/workrecord/update";
private static DingTalkClient sendMsgClient;
private static DingTalkClient createWorkRecordClient;
private static DingTalkClient finishWorkRecordClient;
private static DingTalkClient getTokenClient;
static {
getTokenClient = new DefaultDingTalkClient(dingTalkTokenApi);
sendMsgClient = new DefaultDingTalkClient(dingTalkSendMsgApi);
createWorkRecordClient=new DefaultDingTalkClient(dingTalkCreateWorkRecordApi);
finishWorkRecordClient = new DefaultDingTalkClient(dingTalkFinishWorkRecordApi);
}
/**
* 发送工作消息通知
* @param agentId
* @param users
* @param content
* @param accessToken
*/
public static String sendWorkNotifyMsg(Long agentId,String users,String content,String accessToken) throws Exception{
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(agentId);
req.setUseridList(users);
OapiMessageCorpconversationAsyncsendV2Request.Msg dingtalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
dingtalkMsg.setMsgtype("text");
OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text();
obj2.setContent(content);
dingtalkMsg.setText(obj2);
req.setMsg(dingtalkMsg);
OapiMessageCorpconversationAsyncsendV2Response rsp = sendMsgClient.execute(req, accessToken);
return rsp.getBody();
}
/**
* 发送链接消息
* @param agentId
* @param users
* @param title
* @param content
* @param msgUrl
* @param accessToken
* @return
*/
public static String sendLinkMsg(Long agentId,String users,String title , String content,String msgUrl,String picUrl,String accessToken)throws Exception {
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(agentId);
req.setUseridList(users);
OapiMessageCorpconversationAsyncsendV2Request.Msg dingTalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
dingTalkMsg.setMsgtype("link");
dingTalkMsg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
dingTalkMsg.getLink().setTitle(title);
dingTalkMsg.getLink().setText(content);
dingTalkMsg.getLink().setMessageUrl(msgUrl);
dingTalkMsg.getLink().setPicUrl(msgUrl);
req.setMsg(dingTalkMsg);
OapiMessageCorpconversationAsyncsendV2Response rsp= sendMsgClient.execute(req, accessToken);
return rsp.getBody();
}
/**
* 创建待办
* @param user
* @param title
* @param content
* @param msgUrl
* @param pcUrl
* @param accessToken
* @return
*/
public static OapiWorkrecordAddResponse createWorkRecord(String user, String title , String content, String msgUrl, String pcUrl, String accessToken) throws Exception{
OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
req.setUserid(user);
req.setCreateTime(new Date().getTime());
req.setTitle(title);
req.setUrl(msgUrl);
req.setPcUrl(pcUrl);
List<OapiWorkrecordAddRequest.FormItemVo> list2 = new ArrayList<>();
OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
list2.add(obj3);
obj3.setTitle(title);
obj3.setContent(content);
req.setFormItemList(list2);
OapiWorkrecordAddResponse rsp = createWorkRecordClient.execute(req, accessToken);
return rsp;
}
/**
* 完成待办
* @param user
* @param recordId
* @param accessToken
* @return
*/
public static String finishWorkRecord(String user,String recordId,String accessToken) throws Exception{
OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
req.setUserid(user);
req.setRecordId(recordId);
OapiWorkrecordUpdateResponse rsp = finishWorkRecordClient.execute(req, accessToken);
return rsp.getBody();
}
}
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.extensions.domain.Template;
import cn.ibizlab.core.extensions.helper.DingTalkHelper;
import cn.ibizlab.core.notify.domain.MsgBody;
import cn.ibizlab.core.notify.domain.MsgOpenAccess;
import cn.ibizlab.core.notify.domain.MsgTemplate;
......@@ -237,10 +238,10 @@ public class NotifyCoreService {
dingTalkMsgService(msg);
}
for(MsgBody msg : dingtalk_link){
dingtalkLinkMsgService(msg);
dingTalkLinkMsgService(msg);
}
for(MsgBody msg : dingtalk_workRecord){
dingtalkWorkRecordService(msg);
dingTalkWorkRecordService(msg);
}
}
......@@ -248,22 +249,81 @@ public class NotifyCoreService {
* 发送钉钉消息
* @param msg
*/
@SneakyThrows
private boolean dingTalkMsgService(MsgBody msg){
String msgUsers=getMsgUsers(msg);
MsgTemplate template=templateService.getById(msg.getTemplateId());
MsgOpenAccess openAccess =template.getOpenaccess();
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(Long.parseLong(template.getTemplateId()));
req.setUseridList(msgUsers);
OapiMessageCorpconversationAsyncsendV2Request.Msg dingtalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
dingtalkMsg.setMsgtype("text");
OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text();
obj2.setContent(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
dingtalkMsg.setText(obj2);
req.setMsg(dingtalkMsg);
OapiMessageCorpconversationAsyncsendV2Response rsp = sendMsgClient.execute(req, openAccess.getAccessToken());
saveCallResult(msg,rsp.getBody());
private void dingTalkMsgService(MsgBody msg){
try {
String msgUsers=getMsgUsers(msg);
MsgTemplate template=templateService.getById(msg.getTemplateId());
MsgOpenAccess openAccess =template.getOpenaccess();
String result = DingTalkHelper.sendWorkNotifyMsg(openAccess.getAgentId(),msgUsers,msg.getContent(),openAccess.getAccessToken());
saveCallResult(msg,result);
} catch (Exception e) {
log.error("发送[{}]消息失败,失败原因为:[{}]",msg.getMsgId(),e);
saveErrorResult(msg,e.getMessage());
}
}
/**
* 发送钉钉链接消息
* @return
*/
public void dingTalkLinkMsgService(MsgBody msg){
try {
String msgUsers=getMsgUsers(msg);
MsgTemplate template=getTempLate(msg.getTemplateId());
MsgOpenAccess openAccess =getOpenAccess(template.getAccessId());
String result =DingTalkHelper.sendLinkMsg(openAccess.getAgentId(),msgUsers,msg.getSubject(),msg.getContent(),msg.getMsgLink(),msg.getMsgLink(),openAccess.getAccessToken());
saveCallResult(msg,result);
} catch (Exception e) {
log.error("发送[{}]消息失败,失败原因为:[{}]",msg.getMsgId(),e);
saveErrorResult(msg,e.getMessage());
}
}
/**
* 发送钉钉待办
* @param msg
*/
public void dingTalkWorkRecordService(MsgBody msg){
try {
String msgUser=getMsgUsers(msg);
MsgTemplate template=getTempLate(msg.getTemplateId());
MsgOpenAccess openAccess =getOpenAccess(template.getAccessId());
OapiWorkrecordAddResponse rsp=DingTalkHelper.createWorkRecord(msgUser,msg.getSubject(),msg.getContent(),msg.getMsgLink(),msg.getMsgLinkPc(),openAccess.getAccessToken());
if(rsp.isSuccess()){
msg.setUserData(rsp.getRecordId());
}
saveCallResult(msg,rsp.getBody());
} catch (Exception e) {
log.error("创建[{}]工作待办失败,失败原因为:[{}]",msg.getMsgId(),e);
saveErrorResult(msg,e.getMessage());
}
}
/**
* 完成钉钉待办
* @param msgId
* @return
*/
public boolean finishDingTalkWorkRecord(String msgId){
String result;
MsgBody msg=null;
try {
msg=msgBodyService.getById(msgId);
if(msg==null){
throw new BadRequestAlertException(String.format("无法获取到[%s]待办信息",msgId),"","");
}
String msgUser=getMsgUsers(msg);
MsgTemplate template=getTempLate(msg.getTemplateId());
MsgOpenAccess openAccess =getOpenAccess(template.getAccessId());
result = DingTalkHelper.finishWorkRecord(msgUser,msg.getUserData(),openAccess.getAccessToken());
} catch (Exception e) {
result=String.format("完成[{}]工作待办失败,失败原因为:[{}]",msgId,e);
log.error(result);
}
if(msg!=null){
msg.setUserData2(result);
msgBodyService.update(msg);
}
return true;
}
......@@ -352,65 +412,6 @@ public class NotifyCoreService {
return true;
}
/**
* 发送钉钉链接消息
* @return
*/
@SneakyThrows
public Long dingtalkLinkMsgService(MsgBody msg){
String msgUsers=getMsgUsers(msg);
MsgTemplate template=templateService.getById(msg.getTemplateId());
if(template==null){
throw new BadRequestAlertException(String.format("发送链接消息失败,无法获取到[%s]对应的rt模板",msg.getTemplateId()),"","");
}
MsgOpenAccess openAccess =template.getOpenaccess();
if (openAccess == null){
openAccess = openAccessService.get(template.getAccessId());
}
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(Long.parseLong(template.getTemplateId()));
req.setUseridList(msgUsers);
OapiMessageCorpconversationAsyncsendV2Request.Msg dingtalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
dingtalkMsg.setMsgtype("link");
dingtalkMsg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
dingtalkMsg.getLink().setTitle(StringUtils.isEmpty(msg.getSubject())?template.getTemplateName():msg.getSubject());
dingtalkMsg.getLink().setText(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
dingtalkMsg.getLink().setMessageUrl(StringUtils.isEmpty(msg.getMsgLink())?template.getTemplateUrl():msg.getMsgLink());
dingtalkMsg.getLink().setPicUrl("test");
req.setMsg(dingtalkMsg);
OapiMessageCorpconversationAsyncsendV2Response rsp = sendMsgClient.execute(req, openAccess.getAccessToken());
saveCallResult(msg,rsp.getBody());
return rsp.getTaskId();
}
/**
* 发送钉钉待办
* @param msg
*/
@SneakyThrows
public void dingtalkWorkRecordService(MsgBody msg){
String msgUsers=getMsgUsers(msg);
OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
MsgTemplate template=templateService.getById(msg.getTemplateId());
MsgOpenAccess openAccess =template.getOpenaccess();
req.setUserid(msgUsers);
req.setCreateTime(new Date().getTime());
req.setTitle(StringUtils.isEmpty(msg.getSubject())?template.getTemplateName():msg.getSubject());
req.setUrl(StringUtils.isEmpty(msg.getMsgLink())?template.getTemplateUrl():msg.getMsgLink());
req.setPcUrl(StringUtils.isEmpty(msg.getMsgLinkPc())?template.getTemplateUrl():msg.getMsgLinkPc());
List<OapiWorkrecordAddRequest.FormItemVo> list2 = new ArrayList<>();
OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
list2.add(obj3);
obj3.setTitle(StringUtils.isEmpty(msg.getSubject())?template.getTemplateName():msg.getSubject());
obj3.setContent(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
req.setFormItemList(list2);
OapiWorkrecordAddResponse rsp = createWorkRecordClient.execute(req, openAccess.getAccessToken());
if(rsp.isSuccess()){
msg.setUserData(rsp.getRecordId());
}
saveCallResult(msg,rsp.getBody());
}
/**
* 存储消息进消息表
* @param msg
......@@ -421,7 +422,7 @@ public class NotifyCoreService {
String userIds=msg.getToUsers();
List<MsgBody> msgList=new ArrayList<>();
for(int msgType:getMsgType(msg.getMsgType())){
MsgTemplate msgTemplate =getMsgTempLate(templateId);
MsgTemplate msgTemplate =getTempLate(templateId);
msg.setMsgName(msg.getSubject());
msg.setTemplateId(msgTemplate.getTid());
msg.setToUsers(userIds);
......@@ -440,7 +441,7 @@ public class NotifyCoreService {
*/
@SneakyThrows
public boolean sendDingTalkLinkMsg(MsgBody msg){
getMsgTempLate(msg.getTemplateId());
getTempLate(msg.getTemplateId());
msg.setMsgName(msg.getSubject());
msg.setMsgType(65);
return msgBodyService.create(msg);
......@@ -455,7 +456,7 @@ public class NotifyCoreService {
public String createDingTalkWorkRecord(MsgBody msg){
if(StringUtils.isEmpty(msg.getToUsers()))
throw new BadRequestAlertException("无法获取到用户信息","","");
getMsgTempLate(msg.getTemplateId());
getTempLate(msg.getTemplateId());
msg.setMsgName(msg.getSubject());
msg.setMsgType(66);
msg.setMsgId(String.valueOf(msg.getDefaultKey(true)));
......@@ -498,32 +499,6 @@ public class NotifyCoreService {
return true;
}
/**
* 完成钉钉待办
* @param msgId
* @return
*/
@SneakyThrows
public boolean finishDingTalkWorkRecord(String msgId){
if(StringUtils.isEmpty(msgId)){
throw new BadRequestAlertException("无法获取到消息标识","","");
}
MsgBody msg=msgBodyService.getById(msgId);
if(msg==null){
throw new BadRequestAlertException("无法获取消息","","");
}
String msgUsers=getMsgUsers(msg);
MsgTemplate template=templateService.getById(msg.getTemplateId());
MsgOpenAccess openAccess =template.getOpenaccess();
OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
req.setUserid(msgUsers);
req.setRecordId(msg.getUserData());
OapiWorkrecordUpdateResponse rsp = finishWorkRecordClient.execute(req, openAccess.getAccessToken());
msg.setUserData2(rsp.getBody());
msgBodyService.update(msg);
return rsp.getResult();
}
/**
* 获取消息类型
* @param msgType
......@@ -621,20 +596,15 @@ public class NotifyCoreService {
}
/**
* 获取消息模板
* @param templateId
* @return
* 存储错误信息
* @param msg
* @param errInfo
*/
private MsgTemplate getMsgTempLate(String templateId){
MsgTemplate msgTemplate=templateService.getById(templateId);
if(msgTemplate==null){
throw new BadRequestAlertException(String.format("发送消息失败,无法获取到[%s]对应的rt模板",templateId),"","");
}
String openAccessId=msgTemplate.getAccessId();
if(StringUtils.isEmpty(openAccessId)){
throw new BadRequestAlertException(String.format("发送消息失败,无法获取到消息模板[%s]对应的开放平台[%s]",templateId,openAccessId),"","");
}
return msgTemplate;
private void saveErrorResult(MsgBody msg,String errInfo){
msg.setIsError(1);
msg.setErrorInfo(errInfo);
msg.setIsSend(1);
msgBodyService.update(msg);
}
/**
......@@ -652,4 +622,30 @@ public class NotifyCoreService {
return useMsg;
}
/**
* 获取消息模板
* @param templateId
* @return
*/
private MsgTemplate getTempLate(String templateId){
MsgTemplate template=templateService.getById(templateId);
if(template==null){
throw new BadRequestAlertException(String.format("发送链接消息失败,无法获取到[%s]对应的rt模板",templateId),"","");
}
return template;
}
/**
* 获取开放平台
* @param openAccessId
* @return
*/
private MsgOpenAccess getOpenAccess(String openAccessId){
MsgOpenAccess openAccess =openAccessService.getById(openAccessId);
if (openAccess == null){
throw new BadRequestAlertException(String.format("发送链接消息失败,无法获取到[%s]对应的开放平台",openAccessId),"","");
}
return openAccess;
}
}
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="mac (generated)" id="1730281588526-1">
<preConditions onFail="MARK_RAN" >
<not>
<columnExists tableName="IBZOPENACCESS" columnName="AGENT_ID" />
</not>
</preConditions>
<addColumn tableName="IBZOPENACCESS">
<column name="AGENT_ID" type="BIGINT"/>
</addColumn>
</changeSet>
</databaseChangeLog>
......@@ -9,6 +9,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
@RestController
public class NotifyCoreResource {
......@@ -61,7 +62,7 @@ public class NotifyCoreResource {
* @return
*/
@RequestMapping(method = RequestMethod.POST,value = "/notify/dingtalk/finishworkrecord/{msgid}")
public ResponseEntity<Boolean> finishDingTalkWorkRecord(@PathVariable("msgid") String msgId){
public ResponseEntity<Boolean> finishDingTalkWorkRecord(@Validated @NotNull @PathVariable("msgid") String msgId){
return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.finishDingTalkWorkRecord(msgId));
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册