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

网友投稿 1402 2022-10-07

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

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

目录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 依赖

com.dtflys.forest

forest-spring-boot-starter

1.5.14

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?khttp://ey={key}",

headers = {

"Accept-Charset: utf-8",

"Content-Type: application/json"

},

dataType = "json")

void sendWechatMsg(@Var("key") String key, @JSONBody Map body);

}

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

5、发送消息

注入请求接口:

@Resource

private WechatClient wechatClient;

1、发送文本消息

/**

* 发送文本消息

*/

public void sendTextMsg() {

Map map = new HashMap<>();

map.put("msgtype", "text");

Map 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 消息

/**

* 发送md消息

*/

public void sendMarkdownMsg() {

Map map = new HashMap<>();

map.put("msgtype", "markdown");

Map content = new HashMap<>();

content.put("content", "实时新增用户反馈132例,请相关同事注意。\\n\n" +

" >类型:用户反馈\n" +

" >普通用户反馈:117例\n" +

" >VIP用户反馈:15例");

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 map = new HashMap<>();

map.put("msgtype", "image");

Map content = new HashMap<>();

content.put("md5", getMd5(url));

content.put("base64", getBase64(url));

map.put("image", content);

wechatClient.sendWechatMsg("xxxxxxxxxxxxxxxxxx", map);

}

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

/**

* 图片转为base64编码

*/

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);

}

/**

* 获取文件的MD5值

*

* @param path

* @return

*/

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 map = new HashMap<>();

map.put("msgtype", "news");

Map content = new HashMap<>();

List> list = new ArrayList<>();

Map obj = new HashMap<>();

obj.put("title", "中秋节礼品领取");

obj.put("description", "今年中秋节公司有豪礼相送");

objUEMIwpIs.put("url", "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、发送文本消息并@群员

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序开发怎样创建页面(制作小程序页面)
下一篇:微信小程序开发怎样创建项目(怎么新建微信小程序项目)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~