public interface DingTalkService {
/**
* 工作通知--发送会议通知到钉钉
* @param useridList
* @param content
*/
ResponseVo<Object> notice(String useridList, String content);
/**
* 查询钉钉推送消息结果, 仅支持查询24小时内的任务。
* @param taskId 发送消息时钉钉返回的任务ID
* @return
*/
ResponseVo<Object> getNoticeResult(Long taskId);
}
@Service
public class DingTalkServiceImpl implements DingTalkService {
private static final Logger logger = LoggerFactory.getLogger(DingTalkService.class);
/**
* 发送钉钉工作通知
* @param useridList 用户id
* useridList 最长支持100个用户ID; user123,user456
* @param content 推送内容
* @return
*/
@Override
public ResponseVo<Object> notice(String useridList, String content) {
if(StringUtils.isEmpty(useridList)){
return ResponseVo.fail("发送失败,用户ID不能为空!");
}
ExternalAccessConfig config = getSystemConfig();
if (null == config){
logger.error("发送失败,获取系统配置参数失败!");
return ResponseVo.fail("发送失败,获取系统配置参数失败!");
}
String access_token = DingTalkAccessUtil.getToken(config.getExtAppId(), config.getExtSecret());
if(StringUtil.isEmpty(access_token)){
logger.error("不合法的appKey或appSecret");
return ResponseVo.fail("发送失败,不合法的appKey或appSecret!");
}
DingTalkClient client = new DefaultDingTalkClient(DingTalkAccessUtil.POST_NOTICE_URI);
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setAgentId(Long.parseLong(config.getExtAgentId()));//必填
request.setUseridList(useridList);//必填
request.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("text");//推送文本消息
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
msg.getText().setContent(content);//消息内容,最长不超过2048个字节
request.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response rsp = null;
try {
rsp = client.execute(request, access_token);
logger.info("钉钉发送工作通知结果:{}", rsp.getBody());
} catch (ApiException e) {
e.printStackTrace();
}
return ResponseVo.success();
}
@Override
public ResponseVo<Object> getNoticeResult(Long taskId){
ExternalAccessConfig config = getSystemConfig();
if (null == config){
logger.error("发送失败,获取系统配置参数失败!");
return ResponseVo.fail("发送失败,获取系统配置参数失败!");
}
String access_token = DingTalkAccessUtil.getToken(config.getExtAgentId(), config.getExtSecret());
if(StringUtil.isEmpty(access_token)){
logger.error("不合法的appKey或appSecret");
return ResponseVo.fail("发送失败,不合法的appKey或appSecret!");
}
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult");
OapiMessageCorpconversationGetsendresultRequest req = new OapiMessageCorpconversationGetsendresultRequest();
req.setAgentId(Long.parseLong(config.getExtAgentId()));
req.setTaskId(taskId);//发送消息时钉钉返回的任务ID, 仅支持查询24小时内的任务。
OapiMessageCorpconversationGetsendresultResponse rsp = null;
try {
rsp = client.execute(req, access_token);
logger.info("查询通知发送结果:{}", rsp.getBody());
} catch (ApiException e) {
e.printStackTrace();
}
return ResponseVo.success();
}
/**
* 批量发送消息,上限100
* @param sendAddress
*/
private void batchNotice(String sendAddress, String message){
List<String> list = Arrays.asList(sendAddress.split(","));
if(!list.isEmpty() && list.size() > 0){
for (int i = 1; i <= list.size() / 100 + 1; i++) {
List<String> userList = listPage(i, 100, list);
if(!userList.isEmpty() && userList.size() > 0){
String userIdList = getUserIdListStr(userList);
this.notice(userIdList, message);
}
}
}
}
}

902

被折叠的 条评论
为什么被折叠?



