iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot中集成企业微信机器人实现运维报警的示例
  • 127
分享到

SpringBoot中集成企业微信机器人实现运维报警的示例

2024-04-02 19:04:59 127人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录1、注册企业微信2、添加群机器人3、引入 forest 依赖4、请求方法5、发送消息6、测试在企业运营中,为了实现工作效率和执行效率的提升,往往会选择在社群中使用群聊机器人进行协

在企业运营中,为了实现工作效率和执行效率的提升,往往会选择在社群中使用群聊机器人进行协助管理。机器人可以定时或者按照一定的规则给群里发信息并@群成员等。群聊机器人可以活跃气氛,关怀员工比如根据天气情况提醒员工注意天气变化,发送节日、生日祝福等。它也可以进行工作提醒,帮助员工更好的做系统化的回报总结,机器人可以依托业务系统,每天定时发送工作总结给对应负责人,帮助员工更好地复盘工作。

1、注册企业微信

注册地址:https://work.weixin.qq.com/wework_admin/reGISter_wx?from=myhome

这里的注册企业微信,不一定需要你有企业信息,可以任意填写,不需要审核

2、添加群机器人

加入企业微信后,会有一个该企业的全员群,我们可以在群内添加机器人:

在这里插入图片描述

填写机器人名称

在这里插入图片描述

在这里插入图片描述

添加成功后,得到机器人的 WEBhook 地址,我们需要用到它,特别特别要注意:一定要保护好机器人的 webhook 地址,避免泄漏!不要分享到 GitHub、博客等可被公开查阅的地方,否则坏人就可以用你的机器人来发垃圾消息了。

3、引入 forest 依赖

<!-- Http请求工具 -->
<dependency>
    <groupId>com.dtflys.forest</groupId>
    <artifactId>forest-spring-boot-starter</artifactId>
    <version>1.5.14</version>
</dependency>

forest 是声明式 HTTP 客户端 api 框架,让 Java 发送 HTTP/HTTPS 请求不再难。它比 OkHttp 和 HttpClient 更高层,是封装调用第三方 restful api client 接口的好帮手,是 retrofit 和 feign 之外另一个选择。通过在接口上声明注解的方式配置 HTTP 请求接口,gitee 地址:https://gitee.com/dromara/forest

文档地址:https://forest.dtflyx.com/

相关配置:

## 轻量级HTTP客户端框架forest
forest:
  # 配置底层API为 okhttp3
  backend: okhttp3
  # 连接池最大连接数,默认值为500
  max-connections: 1000
  # 每个路由的最大连接数,默认值为500
  max-route-connections: 500
  # 请求超时时间,单位为毫秒, 默认值为3000
  timeout: 3000
   # 连接超时时间,单位为毫秒, 默认值为2000
  connect-timeout: 3000
   # 请求失败后重试次数,默认为0次不重试
  retry-count: 1
   # 单向验证的HTTPS的默认SSL协议,默认为SSLv3
  ssl-protocol: SSLv3
   # 打开或关闭日志,默认为true
  logEnabled: true
   # 打开/关闭Forest请求日志(默认为 true)
  log-request: true
   # 打开/关闭Forest响应状态日志(默认为 true)
  log-response-status: true
   # 打开/关闭Forest响应内容日志(默认为 false)
  log-response-content: true

4、请求方法

import com.dtflys.forest.annotation.JSONBody;
import com.dtflys.forest.annotation.Post;
import com.dtflys.forest.annotation.Var;

import java.util.Map;

public interface WechatClient {

    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}",
            headers = {
                    "Accept-Charset: utf-8",
                    "Content-Type: application/json"
            },
            dataType = "json")
    void sendWechatMsg(@Var("key") String key, @JSONBody Map<String, Object> body);
}

使用 forest 做 http 请求非常方便,只需要使用注解的方式轻松完成请求

5、发送消息

注入请求接口:

@Resource
private WechatClient wechatClient;

1、发送文本消息


public void sendTextMsg() {
    Map<String, Object> map = new HashMap<>();
    map.put("msgtype", "text");
    Map<String, String> content = new HashMap<>();
    content.put("content", "hello world!");
    map.put("text", content);
    wechatClient.sendWechatMsg("xxxxxxxxxxxxxxxxxx", map);
}

其中:xxxxxxxxxxxxxx 为你的机器人的 Webhook 地址的 key

如果我们想 @某人时,我们可以在 content 中这样写:

content.put("content", "hello world!<@zhangsan>");

这样就可以 @zhangsan 了,仅支持 text、markdown 类型的消息

2、发送 MD 消息


public void sendMarkdownMsg() {
    Map<String, Object> map = new HashMap<>();
    map.put("msgtype", "markdown");
    Map<String, String> content = new HashMap<>();
    content.put("content", "实时新增用户反馈<font color=\\\"warning\\\">132例</font>,请相关同事注意。\\n\n" +
            "         >类型:<font color=\\\"comment\\\">用户反馈</font>\n" +
            "         >普通用户反馈:<font color=\\\"comment\\\">117例</font>\n" +
            "         >VIP用户反馈:<font color=\\\"comment\\\">15例</font>");
    map.put("markdown", content);
    wechatClient.sendWechatMsg("xxxxxxxxxxxxxxxxxx", map);
}

markdown 语法教程见:https://mp.weixin.qq.com/s/uvxdj4tdWePkGbdD5I9iLQ

3、发送图片消息


public void sendImageMsg() {
    String url = "C:\\Users\\admin\\Desktop\\test.png";
    Map<String, Object> map = new HashMap<>();
    map.put("msgtype", "image");
    Map<String, String> content = new HashMap<>();
    content.put("md5", getMd5(url));
    content.put("base64", getBase64(url));
    map.put("image", content);
    wechatClient.sendWechatMsg("xxxxxxxxxxxxxxxxxx", map);
}

我们需要图片的 base64 编码和 MD5 值,方法如下:


public static String getBase64(String imgFile) {
    InputStream in = null;
    byte[] data = null;
    //  读取图片字节数组
    try {
        in = new FileInputStream(imgFile);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 对字节数组Base64编码
    BASE64Encoder encoder = new BASE64Encoder();
    // 返回Base64编码过的字节数组字符串
    return encoder.encode(data);
}


public static String getMd5(String path) {
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream(path);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            md5.update(buffer, 0, len);
        }
        fis.close();
        byte[] byteArray = md5.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : byteArray) {
            sb.append(String.fORMat("%02x", b));
        }
        return sb.toString();
    } catch (IOException | NoSuchAlGorithmException e) {
        e.printStackTrace();
    }
    return null;
}

这里会有一个坑,发送请求后,返回的请求码是 200,但是收不到消息,会看到提示信息:

{"errcode":301019,"errmsg":"media md5 not match, hint: [1651825628340383128465893], from ip: 218.201.194.160, more info at https://open.work.weixin.qq.com/devtool/query?e=301019"}

提示我们:媒体md5不匹配

其实,并不是 MD5 的问题,是 base64 的问题,转化出来的 base64 编码 存在 \r\n,我们需要将其替换掉,这样写:

content.put("base64", getBase64(url).replaceAll("\r|\n", ""));

4、发送图文消息


public void sendNewsMsg() {
    Map<String, Object> map = new HashMap<>();
    map.put("msgtype", "news");
    Map<String, Object> content = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> obj = new HashMap<>();
    obj.put("title", "中秋节礼品领取");
    obj.put("description", "今年中秋节公司有豪礼相送");
    obj.put("url", "www.qq.com");
    obj.put("picurl", "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png");
    list.add(obj);
    content.put("articles", list);
    map.put("news", content);
    wechatClient.sendWechatMsg("xxxxxxxxxxxxxxxxxx", map);
}

6、测试

1、发送文本消息

在这里插入图片描述

2、发送 MD 消息

在这里插入图片描述

3、发送图片消息

在这里插入图片描述

4、发送图文消息

在这里插入图片描述

5、发送文本消息并@群员

在这里插入图片描述

到此这篇关于SpringBoot中集成企业微信机器人实现运维报警的示例的文章就介绍到这了,更多相关SpringBoot运维报警内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot中集成企业微信机器人实现运维报警的示例

本文链接: https://www.lsjlt.com/news/148210.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作