使用Webhook向钉钉机器人发送通知

先创建一个自定义钉钉机器人。

在头像里面点击设置

然后找到机器人管理,前往设置

 

打开后自定义一个机器人并设置好安全设置,我这里采用指定ip

设置好之后拷贝Webhook地址到下面代码中替换就行了

public static void main(String[] args) throws Exception {
        File[] disks = File.listRoots();
        long totalSpace = 0;
        long freeSpace = 0;
        for (File file : disks) {
            totalSpace += file.getTotalSpace();
            freeSpace += file.getFreeSpace();
        }
        double a = totalSpace / (1024 * 1024) / 1024;
        double b = freeSpace / (1024 * 1024) / 1024;
        double c = (b / a);
        if (c < 1) {
            // 钉钉的webhook
            String dingDingToken = "替换成你自己的地址";
            Map<String, Object> json = new HashMap();
            Map<String, Object> text = new HashMap();
            json.put("msgtype", "text");
            text.put("content", "磁盘的空间大小为:" + a + "G\n" + "磁盘的剩余空间大小为:" + b + "G\n剩余空间不足" + String.format("%.2f", c * 100) + "%");
            json.put("text", text);
            System.out.println(JSON.toJSONString(json));
            HttpUtil.sendJSON(dingDingToken, JSON.toJSONString(json));
        }
    }

 这是发送的代码

maven

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.5</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.76</version>
    </dependency>

 Java代码

    public static void main(String[] args) throws Exception {
        File[] disks = File.listRoots();
        long totalSpace = 0;
        long freeSpace = 0;
        for (File file : disks) {
            totalSpace += file.getTotalSpace();
            freeSpace += file.getFreeSpace();
        }
        double a = totalSpace / (1024 * 1024) / 1024;
        double b = freeSpace / (1024 * 1024) / 1024;
        double c = (b / a);
        if (c < 1) {
            // 钉钉的webhook
            String dingDingToken = "替换成你自己的地址";
            Map<String, Object> json = new HashMap();
            Map<String, Object> text = new HashMap();
            json.put("msgtype", "text");
            text.put("content", "磁盘的空间大小为:" + a + "G\n" + "磁盘的剩余空间大小为:" + b + "G\n剩余空间不足" + String.format("%.2f", c * 100) + "%");
            json.put("text", text);
            System.out.println(JSON.toJSONString(json));
            sendJSON(dingDingToken, JSON.toJSONString(json));
        }
    }
    public static String sendJSON(String url, String json) throws Exception {
        String res = "";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost request = new HttpPost(url);
        StringEntity params = new StringEntity(json, HTTP.UTF_8);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        CloseableHttpResponse response = httpClient.execute(request);
        res = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        httpClient.close();
        return res;
    }

最后就大功告成了

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱钓鱼的程序员小郭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值