Search in sources :

Example 1 with OapiRobotSendRequest

use of com.dingtalk.api.request.OapiRobotSendRequest 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 OapiRobotSendRequest

use of com.dingtalk.api.request.OapiRobotSendRequest 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 3 with OapiRobotSendRequest

use of com.dingtalk.api.request.OapiRobotSendRequest in project metersphere by metersphere.

the class DingNoticeSender method sendNailRobot.

public void sendNailRobot(MessageDetail messageDetail, NoticeModel noticeModel, String context) {
    List<Receiver> receivers = noticeModel.getReceivers();
    DingTalkClient client = new DefaultDingTalkClient(messageDetail.getWebhook());
    OapiRobotSendRequest request = new OapiRobotSendRequest();
    request.setMsgtype("text");
    OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
    text.setContent("消息通知: \n" + context);
    request.setText(text);
    if (CollectionUtils.isNotEmpty(receivers)) {
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        List<String> userIds = receivers.stream().map(Receiver::getUserId).distinct().collect(Collectors.toList());
        List<String> phoneList = super.getUserDetails(userIds).stream().map(UserDetail::getPhone).distinct().collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(phoneList)) {
            LogUtil.info("钉钉收件人地址: {}", userIds);
            at.setAtMobiles(phoneList);
            request.setAt(at);
        }
    }
    try {
        client.execute(request);
    } catch (ApiException e) {
        LogUtil.error(e.getMessage(), e);
    }
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) Receiver(io.metersphere.notice.domain.Receiver) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient) UserDetail(io.metersphere.notice.domain.UserDetail) OapiRobotSendRequest(com.dingtalk.api.request.OapiRobotSendRequest) ApiException(com.taobao.api.ApiException)

Example 4 with OapiRobotSendRequest

use of com.dingtalk.api.request.OapiRobotSendRequest in project olive by ClareTung.

the class DingTalkAlarmService method sendMessage.

public void sendMessage(String content) {
    try {
        Long timestamp = System.currentTimeMillis();
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        String serverUrl = webhook + "&timestamp=" + timestamp + "&sign=" + sign;
        DingTalkClient client = new DefaultDingTalkClient(serverUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(content);
        request.setText(text);
        client.execute(request);
    } catch (ApiException e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
    }
}
Also used : DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) DefaultDingTalkClient(com.dingtalk.api.DefaultDingTalkClient) DingTalkClient(com.dingtalk.api.DingTalkClient) Mac(javax.crypto.Mac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) OapiRobotSendRequest(com.dingtalk.api.request.OapiRobotSendRequest) ApiException(com.taobao.api.ApiException)

Example 5 with OapiRobotSendRequest

use of com.dingtalk.api.request.OapiRobotSendRequest in project hippo4j by longtai-cn.

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)

Aggregations

DefaultDingTalkClient (com.dingtalk.api.DefaultDingTalkClient)6 DingTalkClient (com.dingtalk.api.DingTalkClient)6 OapiRobotSendRequest (com.dingtalk.api.request.OapiRobotSendRequest)6 ApiException (com.taobao.api.ApiException)6 Mac (javax.crypto.Mac)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 Receiver (io.metersphere.notice.domain.Receiver)1 UserDetail (io.metersphere.notice.domain.UserDetail)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1