当前位置:   article > 正文

企业微信、飞书、钉钉机器人消息发送工具类_钉钉发送信息工具类

钉钉发送信息工具类

1、实例化WebClient对象

其实你也可以使用RestTemplate,我这里主要是用到了webflux框架,所以需要实例化客户端请求对象

@Bean
public WebClient webClient(){
    HttpClient httpClient = getHttpClient();
    return WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(httpClient)).build();
}

private HttpClient getHttpClient() {
    ConnectionProvider provider = ConnectionProvider.builder("你爱咋咋的,一般用你项目名即可")
            .maxConnections(500)
            .maxIdleTime(Duration.ofSeconds(10))
            .maxLifeTime(Duration.ofSeconds(20))
            .pendingAcquireTimeout(Duration.ofSeconds(30))
            .pendingAcquireTimer((r, d) -> {
                Timeout t = wheel.newTimeout(timeout -> r.run(), d.toMillis(), TimeUnit.MILLISECONDS);
                return () -> t.cancel();
            })
            .fifo()
            .build();

    HttpClient httpClient = HttpClient.create(provider);
    return httpClient;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2、发送及有效性测试工具类


import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import static com.paratera.console.notice.utils.Constants.*;

/**
 * 机器人发送工具类(微信,飞书,钉钉)
 *
 * @author huxiang
 */
@Component
@Slf4j
public class RobotUtil {

    @Autowired
    private WebClient webClient;

    /**
     * 机器人发送消息(markdown格式)
     *
     * @param robotUrl   机器人地址
     * @param type       类型:1微信,2飞书,3钉钉
     * @param context    markdown文本内容
     * @param signSecret 签名校验(飞书,钉钉使用,可为空,具体要根据用户是否启用签名)
     */
    public void sendMsg(String robotUrl, Integer type, String context, String signSecret) {
        JSONObject msgObj = JSONUtil.createObj();
        JSONObject mdObj = JSONUtil.createObj();
        switch (type) {
            case ROBOT_TYPE_WX:
                msgObj.set("msgtype", "markdown");
                mdObj.set("content", context);
                msgObj.set("markdown", mdObj);
                break;
            case ROBOT_TYPE_FS:
                if (StringUtils.isNotEmpty(signSecret)) {
                    msgObj = getFsSignObj(signSecret);
                }
                msgObj.set("msg_type", "interactive");
                mdObj.set("tag", "lark_md");
                mdObj.set("content", context);
                JSONArray elements = JSONUtil.createArray();
                JSONObject wrapObj = JSONUtil.createObj();
                wrapObj.set("tag", "div");
                wrapObj.set("text", mdObj);
                elements.put(wrapObj);
                JSONObject cardObj = JSONUtil.createObj();
                cardObj.set("elements", elements);
                msgObj.set("card", cardObj);
                break;
            case ROBOT_TYPE_DD:
                if (StringUtils.isNotEmpty(signSecret)) {
                    robotUrl = getDdRobotURL(robotUrl, signSecret);
                }
                msgObj.set("msgtype", "markdown");
                mdObj.set("title", "通知");
                mdObj.set("text", context);
                msgObj.set("markdown", mdObj);
                break;
        }
        webClient.post()
                .uri(robotUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(msgObj))
                .retrieve().bodyToMono(String.class).subscribe(
                        result -> {
                            log.info("机器人通知发送结果:" + result);
                        }
                );
    }

    /**
     * 机器人有效性测试 0表示成功,其他表示失败
     *
     * @param robotUrl   机器人地址
     * @param type       类型:1微信,2飞书,3钉钉
     * @param signSecret 签名校验(飞书,钉钉使用,可为空,具体要根据用户是否启用签名)
     * @return
     */
    public Integer sendTestMsg(String robotUrl, Integer type, String signSecret) {
        JSONObject msgObj = JSONUtil.createObj();
        JSONObject mdObj = JSONUtil.createObj();
        ObjectMapper mapper = new ObjectMapper();
        switch (type) {
            case ROBOT_TYPE_WX:
                msgObj.set("msgtype", "markdown");
                mdObj.set("content", "机器人有效性测试!");
                msgObj.set("markdown", mdObj);
                break;
            case ROBOT_TYPE_FS:
                if (StringUtils.isNotEmpty(signSecret)) {
                    msgObj = getFsSignObj(signSecret);
                }
                msgObj.set("msg_type", "interactive");
                mdObj.set("tag", "lark_md");
                mdObj.set("content", "机器人有效性测试!");
                JSONArray elements = JSONUtil.createArray();
                JSONObject wrapObj = JSONUtil.createObj();
                wrapObj.set("tag", "div");
                wrapObj.set("text", mdObj);
                elements.put(wrapObj);
                JSONObject cardObj = JSONUtil.createObj();
                cardObj.set("elements", elements);
                msgObj.set("card", cardObj);
                break;
            case ROBOT_TYPE_DD:
                if (StringUtils.isNotEmpty(signSecret)) {
                    robotUrl = getDdRobotURL(robotUrl, signSecret);
                }
                msgObj.set("msgtype", "markdown");
                mdObj.set("title", "通知");
                mdObj.set("text", "机器人有效性测试!");
                msgObj.set("markdown", mdObj);
                break;

        }
        Mono<String> mono = webClient.post()
                .uri(robotUrl)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(msgObj))
                .retrieve().bodyToMono(String.class);
        String result = mono.block();
        try {
            Map res = mapper.readValue(result, Map.class);
            return (Integer) (res.containsKey("errcode") ? res.get("errcode") : res.get("code"));
        } catch (JsonProcessingException e) {
            log.error("类型转换异常-RobotUtil.sendTestMsg:" + e);
        }
        return -1;
    }

    /**
     * 飞书获取带签名的消息体
     *
     * @return
     */
    public JSONObject getFsSignObj(String signSecret) {
        JSONObject signObj = JSONUtil.createObj();
        Long timestamp = DateUtil.currentSeconds();
        String sign = null;
        try {
            //把timestamp+"\n"+密钥当做签名字符串
            String stringToSign = timestamp + "\n" + signSecret;
            //使用HmacSHA256算法计算签名
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
            byte[] signData = mac.doFinal(new byte[]{});
            sign = Base64.encodeBase64String(signData);
        } catch (Exception e) {
            log.error("飞书获取签名失败:" + e);
        }
        if (StringUtils.isEmpty(sign)) {
            return null;
        }
        signObj.set("timestamp", timestamp);
        signObj.set("sign", sign);
        return signObj;
    }


    /**
     * 钉钉获取带签名的机器人推送URL
     *
     * @return
     */

    public String getDdRobotURL(String robotUrl, String signSecret) {
        Long timestamp = System.currentTimeMillis();
        String stringToSign = timestamp + "\n" + signSecret;
        String sign = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(signSecret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            sign = URLEncoder.encode(Base64.encodeBase64String(signData), "UTF-8");
        } catch (Exception e) {
            log.error("钉钉获取签名失败:" + e);
        }
        if (StringUtils.isEmpty(sign)) {
            return null;
        }
        return robotUrl + "&timestamp=" + timestamp + "&sign=" + sign;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/965306
推荐阅读
相关标签
  

闽ICP备14008679号