Search in sources :

Example 1 with DingTalkClient

use of com.dingtalk.api.DingTalkClient in project dynamic-threadpool by acmenlt.

the class DingSendMessageHandler method execute.

private void execute(String secretKey, String title, String text, List<String> mobiles) {
    String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + secretKey;
    DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl);
    OapiRobotSendRequest request = new OapiRobotSendRequest();
    request.setMsgtype("markdown");
    OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
    markdown.setTitle(title);
    markdown.setText(text);
    OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
    at.setAtMobiles(mobiles);
    request.setAt(at);
    request.setMarkdown(markdown);
    try {
        dingTalkClient.execute(request);
    } catch (ApiException ex) {
        log.error("Ding failed to send message", ex);
    }
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient) OapiRobotSendRequest(com.dingtalk.api.request.OapiRobotSendRequest) ApiException(com.taobao.api.ApiException)

Example 2 with DingTalkClient

use of com.dingtalk.api.DingTalkClient in project MaxKey by dromara.

the class DingtalkAccessTokenService method requestToken.

public String requestToken() throws ApiException {
    DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
    OapiGettokenRequest request = new OapiGettokenRequest();
    request.setAppkey(appkey);
    request.setAppsecret(appsecret);
    request.setHttpMethod("GET");
    OapiGettokenResponse response = client.execute(request);
    _logger.info("response : " + response.getBody());
    if (response.getErrcode() == 0) {
        return response.getAccessToken();
    }
    return "";
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) OapiGettokenRequest(com.dingtalk.api.request.OapiGettokenRequest) OapiGettokenResponse(com.dingtalk.api.response.OapiGettokenResponse) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient)

Example 3 with DingTalkClient

use of com.dingtalk.api.DingTalkClient in project dynamic-threadpool by acmenlt.

the class DingSendMessageHandler method execute.

private void execute(NotifyConfigDTO notifyConfig, String title, String text, List<String> mobiles) {
    String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + notifyConfig.getSecretKey();
    String secret = notifyConfig.getSecret();
    if (StringUtil.isNotBlank(secret)) {
        long timestamp = System.currentTimeMillis();
        String stringToSign = timestamp + "\n" + secret;
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), StandardCharsets.UTF_8.name());
            serverUrl = serverUrl + "&timestamp=" + timestamp + "&sign=" + sign;
        } catch (Exception ex) {
            log.error("Failed to sign the message sent by nailing.", ex);
        }
    }
    DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl);
    OapiRobotSendRequest request = new OapiRobotSendRequest();
    request.setMsgtype("markdown");
    OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
    markdown.setTitle(title);
    markdown.setText(text);
    OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
    at.setAtMobiles(mobiles);
    request.setAt(at);
    request.setMarkdown(markdown);
    try {
        dingTalkClient.execute(request);
    } catch (ApiException ex) {
        log.error("Ding failed to send message", ex);
    }
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient) Mac(javax.crypto.Mac) ApiException(com.taobao.api.ApiException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) OapiRobotSendRequest(com.dingtalk.api.request.OapiRobotSendRequest) ApiException(com.taobao.api.ApiException)

Example 4 with DingTalkClient

use of com.dingtalk.api.DingTalkClient in project dingtalk-app-server by nju-softeng.

the class OAApi method getOAOutCome.

public int getOAOutCome(String processInstanceId) {
    try {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
        OapiProcessinstanceGetRequest req = new OapiProcessinstanceGetRequest();
        req.setProcessInstanceId(processInstanceId);
        OapiProcessinstanceGetResponse rsp = client.execute(req, getAccessToken());
        if (rsp.getProcessInstance().getStatus().equals("COMPLETED")) {
            if (rsp.getProcessInstance().getResult().equals("agree"))
                return 1;
            else
                return 0;
        } else
            return -1;
    } catch (Exception e) {
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) OapiProcessinstanceGetResponse(com.dingtalk.api.response.OapiProcessinstanceGetResponse) OapiProcessinstanceGetRequest(com.dingtalk.api.request.OapiProcessinstanceGetRequest) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ApiException(com.taobao.api.ApiException) ResponseStatusException(org.springframework.web.server.ResponseStatusException)

Example 5 with DingTalkClient

use of com.dingtalk.api.DingTalkClient in project dingtalk-app-server by nju-softeng.

the class OAApi method createAbsentOA.

public String createAbsentOA(AbsentOA absentOA) {
    try {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
        OapiProcessinstanceCreateRequest request = new OapiProcessinstanceCreateRequest();
        request.setAgentId(BaseApi.AGENTID);
        // 设置process code
        request.setProcessCode(absentOAProcessCode);
        request.setOriginatorUserId(userService.getUserid(absentOA.getUser().getId()));
        request.setDeptId(-1L);
        // 设置表单内容
        List<OapiProcessinstanceCreateRequest.FormComponentValueVo> form = new ArrayList<>();
        OapiProcessinstanceCreateRequest.FormComponentValueVo type = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        form.add(type);
        type.setName("请假类型");
        type.setValue(absentOA.getType());
        OapiProcessinstanceCreateRequest.FormComponentValueVo start = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        form.add(start);
        start.setName("开始时间");
        start.setValue(absentOA.getDingTalkSchedule().getStart().toString());
        OapiProcessinstanceCreateRequest.FormComponentValueVo end = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        form.add(end);
        end.setName("结束时间");
        end.setValue(absentOA.getDingTalkSchedule().getEnd().toString());
        // OapiProcessinstanceCreateRequest.FormComponentValueVo dayNum = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        // form.add(dayNum);
        // dayNum.setName("结束时间");
        // dayNum.setValue(absentOA.getDayNum().toString());
        OapiProcessinstanceCreateRequest.FormComponentValueVo reason = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        form.add(reason);
        reason.setName("请假缘由");
        reason.setValue(absentOA.getReason());
        request.setFormComponentValues(form);
        // 设置审批人
        List<OapiProcessinstanceCreateRequest.ProcessInstanceApproverVo> processInstanceApproverVoList = new ArrayList<>();
        OapiProcessinstanceCreateRequest.ProcessInstanceApproverVo processInstanceApproverVo = new OapiProcessinstanceCreateRequest.ProcessInstanceApproverVo();
        processInstanceApproverVoList.add(processInstanceApproverVo);
        request.setApprovers(absentOA.getDingTalkSchedule().getOrganizer().getUserid());
        OapiProcessinstanceCreateResponse rsp = client.execute(request, getAccessToken());
        return rsp.getProcessInstanceId();
    } catch (Exception e) {
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) OapiProcessinstanceCreateRequest(com.dingtalk.api.request.OapiProcessinstanceCreateRequest) ArrayList(java.util.ArrayList) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient) ApiException(com.taobao.api.ApiException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) OapiProcessinstanceCreateResponse(com.dingtalk.api.response.OapiProcessinstanceCreateResponse) ResponseStatusException(org.springframework.web.server.ResponseStatusException)

Aggregations

DefaultDingTalkClient (com.dingtalk.api.DefaultDingTalkClient)13 DingTalkClient (com.dingtalk.api.DingTalkClient)13 ApiException (com.taobao.api.ApiException)9 OapiRobotSendRequest (com.dingtalk.api.request.OapiRobotSendRequest)6 Mac (javax.crypto.Mac)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 ResponseStatusException (org.springframework.web.server.ResponseStatusException)3 OapiGettokenRequest (com.dingtalk.api.request.OapiGettokenRequest)1 OapiProcessInstanceTerminateRequest (com.dingtalk.api.request.OapiProcessInstanceTerminateRequest)1 OapiProcessinstanceCreateRequest (com.dingtalk.api.request.OapiProcessinstanceCreateRequest)1 OapiProcessinstanceGetRequest (com.dingtalk.api.request.OapiProcessinstanceGetRequest)1 OapiV2DepartmentGetRequest (com.dingtalk.api.request.OapiV2DepartmentGetRequest)1 OapiV2DepartmentListsubRequest (com.dingtalk.api.request.OapiV2DepartmentListsubRequest)1 OapiV2UserListRequest (com.dingtalk.api.request.OapiV2UserListRequest)1 OapiGettokenResponse (com.dingtalk.api.response.OapiGettokenResponse)1 OapiProcessInstanceTerminateResponse (com.dingtalk.api.response.OapiProcessInstanceTerminateResponse)1 OapiProcessinstanceCreateResponse (com.dingtalk.api.response.OapiProcessinstanceCreateResponse)1 OapiProcessinstanceGetResponse (com.dingtalk.api.response.OapiProcessinstanceGetResponse)1 OapiV2DepartmentGetResponse (com.dingtalk.api.response.OapiV2DepartmentGetResponse)1 OapiV2DepartmentListsubResponse (com.dingtalk.api.response.OapiV2DepartmentListsubResponse)1