钉钉发布工作通知

2 篇文章 0 订阅
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);
            }
        }
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值