All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.webhook.starter.push.dingtalk.DingTalkNotifier Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.webhook.starter.push.dingtalk;

import com.spring.boxes.webhook.starter.meta.DingTalkConfig;
import com.spring.boxes.webhook.starter.push.Notifier;
import com.spring.boxes.webhook.starter.push.NotifyMessage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestOperations;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class DingTalkNotifier implements Notifier {

    private RestOperations restOperations;

    private DingTalkConfig dingTalkConfig;

    @Override
    public Object notify(NotifyMessage message) {
        MarkdownMessage markdownMessage = toMessage(message);
        return notify(markdownMessage);
    }

    public Object notify(MarkdownMessage markdownMessage) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(markdownMessage, httpHeaders);
        long timeStamp = System.currentTimeMillis();
        return restOperations.postForEntity(String.format("https://oapi.dingtalk.com/robot/send?access_token=%s×tamp=%s&sign=%s",
                dingTalkConfig.getAccessToken(), timeStamp, sign(timeStamp, dingTalkConfig.getSignKey())), entity, Object.class).getBody();
    }

    public MarkdownMessage toMessage(NotifyMessage message) {
        MarkdownMessage markdownMessage = new MarkdownMessage();
        StringBuilder sb = new StringBuilder();
        if (!ObjectUtils.isEmpty(message.getNotifies())) {
            List notifies = message.getNotifies();
            List atMobiles = new ArrayList<>();
            for (String notifier : notifies) {
                if (!ObjectUtils.isEmpty(notifier) && PHONE_PATTERN.matcher(notifier).matches()) {
                    sb.append("@").append(notifier);
                    atMobiles.add(notifier);
                }
            }
            if (sb.length() > 0) {
                sb.append("\n\n");
            }
            DingRobotAt at = new DingRobotAt();
            at.setAtMobiles(atMobiles);
            markdownMessage.setAt(at);
        }
        sb.append(message.getMessage());
        Markdown markdown = new Markdown(message.getTitle(), sb.toString());
        markdownMessage.setMarkdown(markdown);
        return markdownMessage;
    }

    /**
     * 钉钉接口签名
     *
     * @param timestamp 时间戳
     * @param secret    签名密钥
     * @return 签名
     */
    public static String sign(long timestamp, String secret) {
        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));
            return URLEncoder.encode(new String(Base64.getEncoder().encode(signData)), StandardCharsets.UTF_8.name());
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) {
            throw new UnsupportedOperationException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy