| | |
| | | import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; |
| | | import com.dingtalk.api.response.OapiUserListidResponse; |
| | | import com.dingtalk.api.response.OapiV2UserGetbymobileResponse; |
| | | import com.ruoyi.common.core.domain.DingAsyncSendMsgReq; |
| | | import com.ruoyi.common.core.domain.DingAsyncSendMsgResp; |
| | | import com.ruoyi.common.core.domain.DingTalkReqVo; |
| | | import com.ruoyi.common.core.domain.DingTokenResp; |
| | | import com.ruoyi.common.utils.DingTalkUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.smartor.service.DingTalkService; |
| | | import com.taobao.api.ApiException; |
| | |
| | | |
| | | @Value("${dingAppSecret}") |
| | | private String dingAppSecret; |
| | | |
| | | @Value("${dingAppAgentId}") |
| | | private Long dingAppAgentId; |
| | | |
| | | @Override |
| | | public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) { |
| | |
| | | OapiGettokenResponse response = client.execute(request); |
| | | return response.getAccessToken(); |
| | | } |
| | | |
| | | /** |
| | | * 获取post access_token |
| | | */ |
| | | private String getAccessTokenPost() throws ApiException { |
| | | DefaultDingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.com/v1.0/oauth2/{corpId}/token"); |
| | | OapiGettokenRequest request = new OapiGettokenRequest(); |
| | | //Appkey |
| | | request.setAppkey(dingAppid); |
| | | //Appsecret |
| | | request.setAppsecret(dingAppSecret); |
| | | /*请求方式*/ |
| | | request.setHttpMethod("Post"); |
| | | OapiGettokenResponse response = client.execute(request); |
| | | return response.getAccessToken(); |
| | | } |
| | | |
| | | // /** |
| | | // * 发送钉钉消息-景宁 |
| | | // * @param sMessageContent |
| | | // * @throws Exception |
| | | // */ |
| | | // public void sendDingTalkText(String sMessageContent) throws Exception { |
| | | // String CORP_ID = "你的企业corpid"; |
| | | // String CORP_SECRET = "企业应用的corpsecret"; |
| | | // Long AGENT_ID = 4992829038L; // 你的应用agentId |
| | | // |
| | | // try { |
| | | // // 1. 获取access_token |
| | | // DingTokenResp tokenResp = DingTalkUtils.getAccessToken(CORP_ID, CORP_SECRET); |
| | | // if (tokenResp.getErrcode() != 0) { |
| | | // System.err.println("获取token失败:" + tokenResp.getErrmsg()); |
| | | // return; |
| | | // } |
| | | // String accessToken = tokenResp.getAccess_token(); |
| | | // System.out.println("access_token=" + accessToken); |
| | | // |
| | | // // 2. 组装发送请求 |
| | | // DingAsyncSendMsgReq sendReq = new DingAsyncSendMsgReq(); |
| | | // sendReq.setAgent_id(AGENT_ID); |
| | | // sendReq.setUserid_list("user01,user02"); // 接收人钉钉userId,多个逗号 |
| | | // sendReq.setTo_all_user(false); |
| | | // // 设置消息内容,这里用文本消息 |
| | | // sendReq.setMsg(DingTalkUtils.buildTextMsg("你好,这是异步发送钉钉消息测试")); |
| | | // |
| | | // // 3. 调用异步发送接口 |
| | | // DingAsyncSendMsgResp sendResp = DingTalkUtils.asyncSendCorpMsg(accessToken, sendReq); |
| | | // if (sendResp.getErrcode() == 0) { |
| | | // System.out.println("提交消息任务成功,task_id=" + sendResp.getTask_id()); |
| | | // System.out.println("消息实际发送结果,需要在钉钉后台配置回调接收"); |
| | | // } else { |
| | | // System.err.println("提交消息任务失败:code=" + sendResp.getErrcode() + ",msg=" + sendResp.getErrmsg()); |
| | | // } |
| | | // |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | |
| | | /** |
| | | * 发送钉钉消息-景宁 |
| | | * @param sMessageContent |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void sendDingTalkText(String sMessageContent, String userId) throws Exception { |
| | | Long agentId = dingAppAgentId; |
| | | String accessToken = getAccessToken(); |
| | | DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); |
| | | OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); |
| | | request.setAgentId(agentId); |
| | | request.setUseridList(userId); |
| | | request.setToAllUser(false); |
| | | |
| | | OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); |
| | | msg.setMsgtype("text"); |
| | | OapiMessageCorpconversationAsyncsendV2Request.Text text = new OapiMessageCorpconversationAsyncsendV2Request.Text(); |
| | | text.setContent(sMessageContent); |
| | | msg.setText(text); |
| | | request.setMsg(msg); |
| | | |
| | | OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken); |
| | | System.out.println(rsp.getBody()); |
| | | } |
| | | |
| | | /** |
| | | * 根据电话获取用户 |
| | | * @param mobile |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public String getDingTalkUserByMobile(String mobile) throws Exception { |
| | | String accessToken = getAccessToken(); |
| | | DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile"); |
| | | OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest(); |
| | | req.setMobile(mobile); |
| | | OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken); |
| | | return rsp.getBody(); |
| | | } |
| | | |
| | | } |