钉钉消息提醒java

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;
    }
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值