Search in sources :

Example 11 with DingTalkClient

use of com.dingtalk.api.DingTalkClient 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 12 with DingTalkClient

use of com.dingtalk.api.DingTalkClient 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)

Example 13 with DingTalkClient

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

the class OpenChangeTimedTask method sendDingMessage.

public void sendDingMessage(OpenChangeInfo itemInfo) {
    String gitHubItemUrl = itemInfo.getHtml_url();
    String markdownText = String.format("<font color='#2a9d8f'>[通知] </font> GitHub Star Fork 变更通知 \n\n" + " --- \n\n " + "<font color='#708090' size=2>项目名称:HIPPO-JAVA</font> \n\n" + "<font color='#708090' size=2>项目地址:[%s](%s)</font> \n\n" + " --- \n\n " + "<font color='#708090' size=2>Hippo Stars Add:</font><font color='#FF8C00' size=2>%s</font> \n\n " + "<font color='#708090' size=2>Hippo Forks Add:</font><font color='#FF8C00' size=2>%s</font> \n\n " + " --- \n\n  " + "<font color='#708090' size=2>Hippo Now Star:[%d](" + gitHubItemUrl + "/stargazers)</font> \n\n" + "<font color='#708090' size=2>Hippo Now Fork:[%d](" + gitHubItemUrl + "/members)</font> \n\n" + "<font color='#708090' size=2>Hippo Open Issue:[%d](" + gitHubItemUrl + "/issues)</font> \n\n" + "<font color='#708090' size=2>Hippo Subscribers Count:[%d](" + gitHubItemUrl + "/watchers)</font> \n\n" + " --- \n\n  " + "<font color='#708090' size=2>友情提示:5 分钟内 Star Fork 有变更则通知(可配置)</font> \n\n" + "<font color='#708090' size=2>OWNER:Long-Tai</font> \n\n" + " --- \n\n  " + "**播报时间:%s**", itemInfo.getName().toUpperCase(), gitHubItemUrl, itemInfo.getStar_add() == null ? "-" : itemInfo.getStar_add().toString() + "+", itemInfo.getFork_add() == null ? "-" : itemInfo.getFork_add().toString() + "+", itemInfo.getStargazers_count(), itemInfo.getForks_count(), itemInfo.getOpen_issues_count(), itemInfo.getSubscribers_count(), DateUtil.now());
    OpenChangeNotifyBootstrapProperties.NotifyConfig notifyConfig = bootstrapProperties.getNotifys().get(0);
    String serverUrl = notifyConfig.getUrl() + notifyConfig.getToken();
    DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl);
    OapiRobotSendRequest request = new OapiRobotSendRequest();
    request.setMsgtype("markdown");
    OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
    markdown.setTitle("GitHub Star Fork 变更通知");
    markdown.setText(markdownText);
    request.setMarkdown(markdown);
    try {
        dingTalkClient.execute(request);
    } catch (ApiException ex) {
        log.error("Ding failed to send message.", ex.getMessage());
    }
}
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)

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