Notification
1. 概述
tio-boot 内置了消息通知组件,用户发送消息到 微信企业群 飞书企业群 等等
本文档提供了一个通知组件的使用指南,介绍通过不同的方式发送通知,比如发送文本消息或者发送预警信息。它支持自定义通知方式,用户可以通过实现特定的接口来集成不同的通知服务。
添加依赖 Notification 依赖 okhttp 发送数据,请手动添加 okhttp 库
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
2. 核心组件介绍
- INotification: 这是一个接口,定义了发送通知的基本方法。任何通知实现类都应该实现这个接口。
- INotificationFactory: 这是一个工厂接口,用于生成
INotification
的实例。 - NotificationUtils: 这是一个工具类,提供了静态方法来发送不同类型的通知。它使用
INotificationFactory
来获取INotification
的实例。
3. 使用说明
发送消息到企业微信群
配置 WebHook URL
在app.properties
文件中配置 WebHook URL:
notification.webhook.url=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=get key from wecom
发送文本消息
要发送文本消息,可以使用NotificationUtils.sendTextMsg
方法:
NotificationUtils.sendTextMsg("您的消息内容");
发送预警信息
要发送预警信息,可以使用NotificationUtils.sendWarm
方法:
package com.imaginix.kimi.service.monitoring;
import java.io.IOException;
import java.util.Date;
import org.junit.Test;
import com.litongjava.tio.utils.environment.EnvUtils;
import com.litongjava.tio.utils.notification.NotifactionWarmModel;
import com.litongjava.tio.utils.notification.NotificationUtils;
import lombok.Cleanup;
import okhttp3.Response;
public class MonitoringNotificationTest {
@Test
public void test() {
EnvUtils.load("app.properties");
String appGroupName = "imaginix";
String appName = "kimi-service-monitoring";
String warningName = "警告名称";
String level = "警告级别";
String deviceName = "设备名称";
String content = "警告内容";
Date time = new Date();
NotifactionWarmModel notifactionWarmModel = new NotifactionWarmModel(appGroupName, appName, warningName, level,
deviceName, content, time);
try {
@Cleanup
Response response = NotificationUtils.sendWarm(notifactionWarmModel);
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
发送消息到飞书群
获取推送地址
Invite custom robots into the group.
- Enter the target group, click the More button in the upper right corner of the group, and click Settings.
- On the Settings interface on the right, click Bots.
- On the Bots interface, click Add Bot.
- In the Add bot dialog box, find and click Custom bot.
- Set the avatar, name and description of the custom robot, and click Add.
Obtain the webhook address of the custom bot and click Finish.
- The Webhook URL format corresponding to the robot is as follows
https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxx
Please keep this webhook address properly and do not publish it on publicly accessible websites such as Gitlab and blogs to avoid being maliciously called to send spam messages after the address is leaked.
Later, you can click the robot picture to the right of the group name to enter the custom robot details page and manage the configuration information of the custom robot.
Test calling the webhook address of the custom robot to send a message to the group it belongs to.
Initiate an HTTP POST request to the webhook address in any way.
You need to have a certain server-side development foundation, and call the webhook address through the server-side HTTP POST request. Taking the curl command as an example, the request example is as follows.
POST /open-apis/bot/v2/hook/xxxx HTTP/1.1
Host: open.larksuite.com
Content-Type: application/json
Accept: */*
Host: open.larksuite.com
Connection: keep-alive
{
"msg_type": "text",
"content": {
"text": "request example"
}
}
发送消息到 Larksuite 群
添加依赖
- okhttp
- tio-utils
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>tio-utils</artifactId>
<version>3.7.3.v20240801-RELEASE</version>
</dependency>
添加配置
notification.webhook.url=https://open.larksuite.com/open-apis/bot/v2/hook/xxx
发送消息测试类
import java.io.IOException;
import java.util.Date;
import org.junit.Test;
import com.litongjava.tio.utils.environment.EnvUtils;
import com.litongjava.tio.utils.notification.LarksuiteNotificationFectory;
import com.litongjava.tio.utils.notification.NotifactionWarmModel;
import com.litongjava.tio.utils.notification.NotificationUtils;
import okhttp3.Response;
public class MonitoringNotificationTest {
@Test
public void test() {
// 加载配置文件
EnvUtils.load();
// 设置为LarksuiteNotificationFectory
NotificationUtils.setFactory(new LarksuiteNotificationFectory());
// 发送文本信息
try (Response response = NotificationUtils.sendTextMsg("this is just a test quesiton")) {
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
// 发送报警消息
String appGroupName = "imaginix";
String appName = "kimi-service-monitoring";
String warningName = "警告名称";
String level = "警告级别";
String deviceName = "设备名称";
String content = "测试内容";
Date time = new Date();
NotifactionWarmModel notifactionWarmModel = new NotifactionWarmModel(appGroupName, appName, warningName, level,
deviceName, content, time);
try (Response response = NotificationUtils.sendWarm(notifactionWarmModel);) {
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
5. 自定义通知工厂
实现 INotification 接口: 创建一个新类实现
INotification
接口,并实现其方法,以支持发送通知的具体实现。public class CustomNotification implements INotification { // 实现接口中的方法 }
实现 INotificationFactory 接口: 创建一个工厂类来生成自定义通知的实例。
public class CustomNotificationFactory implements INotificationFactory { @Override public INotification getNotifaction() { return new CustomNotification(); } }
配置工厂: 在程序开始时,配置
NotificationUtils
使用自定义的工厂。NotificationUtils.setFactory(new CustomNotificationFactory());
发送通知: 使用
NotificationUtils
发送通知,它会使用您配置的自定义工厂来获取通知实例,并发送通知。// 使用自定义通知发送消息 NotificationUtils.sendTextMsg("您的消息内容"); // 或者发送预警信息 NotificationUtils.sendWarm(warningName, level, deviceName, content);