java代码结合钉钉机器人自动报警

一、钉钉群组添加机器人

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、java代码结合钉钉机器人

1、添加依赖:

		<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
            <scope>compile</scope>
        </dependency>
         <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.51</version>
        </dependency>

2、编写钉钉发送消息工具类

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.kongzhong.aw.autogift.param.BaseParam;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * @Description : 钉钉工具类
 * @Author : caoxiya
 * @Date: 2020-05-22 19:16
 */
public class DingUtil {

    /**
     * 给钉钉群发送消息方法
     *
     * @param content 消息内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException
     */
    public static String sendMsg(String content) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        try {
            //群机器人复制到的秘钥secret
            //String secret = "SECc986f2d199370a58870e17d86f976e12f6dc31bb6e3d71e44efas0a538bb5278";
            String secret = "你的秘钥secret";
            //获取系统时间戳
            long timestamp = Instant.now().toEpochMilli();
            //拼接
            String stringToSign = timestamp + "\n" + secret;
            //使用HmacSHA256算法计算签名
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            //进行Base64 encode 得到最后的sign,可以拼接进url里
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
            //钉钉机器人地址(配置机器人的webhook)
            String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=a99413dtd5524b651f289357ab84196fb2bbf13d2c0c925c1f1d2d1df89b74b3&timestamp=" + timestamp + "&sign=" + sign;

            String result = HttpUtil.post(dingUrl, content);
            return result;
        } catch (Exception e) {
           // log.error("钉钉推送消息出现异常");
            return null;
        }

    }

    /**
     * @param content    内容
     * @param isAtAll    是否@所有人 如果写true mobileList失效
     * @param mobileList @人的手机号
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     */
    public static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        //消息内容
        Map<String, String> contentMap = Maps.newHashMap();
        contentMap.put("content", content);
        //通知人
        Map<String, Object> atMap = Maps.newHashMap();
        //1.是否通知所有人
        atMap.put("isAtAll", isAtAll);
        //2.通知具体人的手机号码列表
        atMap.put("atMobiles", mobileList);
        Map<String, Object> reqMap = Maps.newHashMap();
        reqMap.put("msgtype", "text");
        reqMap.put("text", contentMap);
        reqMap.put("at", atMap);
        String contens = JSON.toJSONString(reqMap);

        String result = DingUtil.sendMsg(contens);
        return result;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        try {
            Integer i = null;
            if (i < 1) {

            }
            }catch(Exception e){
                DingUtil.buildReqStr("异常====>"+e.getMessage(), true, BaseParam.PHONELIST);
            }
    }
}

3、添加通知人员

import java.util.Arrays;
import java.util.List;

/**
 * 项目名: trip
 * 文件名: BaseParam
 * 创建者: LCK
 * 创建时间:2022/4/7 16:33
 * 描述: TODO 指定钉钉报警接收信息人员
 */
public class BaseParam {
    public static List<String> PHONELIST = Arrays.asList(new String[]{"131****0000"});
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值