feat:添加钉钉和企业微信消息通知

V0.5.x
荭琪枫 2023-05-08 23:33:32 +08:00
parent 8a33c66254
commit 2b29d500cf
20 changed files with 160 additions and 65 deletions

View File

@ -1,9 +0,0 @@
package cc.iotkit.message.model;
/**
* author:
* date: 2023-05-08 15:58
* description:
**/
public class DingTalkMessage extends Message{
}

View File

@ -1,9 +0,0 @@
package cc.iotkit.message.model;
/**
* author:
* date: 2023-05-08 15:58
* description:
**/
public class QyWechatMessage extends Message{
}

View File

@ -1,16 +0,0 @@
package cc.iotkit.message.notify;
import cc.iotkit.message.model.Message;
/**
* author:
* date: 2023-05-08 15:09
* description:
**/
public class DingTalkEventListener implements EventListener{
@Override
public void doEvent(Message message) {
}
}

View File

@ -1,16 +0,0 @@
package cc.iotkit.message.notify;
import cc.iotkit.message.model.Message;
/**
* author:
* date: 2023-05-08 15:09
* description:
**/
public class QyWechatEventListener implements EventListener{
@Override
public void doEvent(Message message) {
}
}

View File

@ -19,7 +19,6 @@
<module>iot-message-core</module>
<module>iot-vertx-event-bus</module>
<module>iot-message-rocketmq</module>
<module>iot-message-notify</module>
</modules>
<dependencies>

View File

@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>iot-message-bus</artifactId>
<artifactId>iot-common</artifactId>
<groupId>cc.iotkit</groupId>
<version>${revision}</version>
</parent>
@ -13,21 +13,25 @@
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-model</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-message-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,37 @@
package cc.iotkit.message.model;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
/**
* author:
* date: 2023-05-08 15:58
* description:
**/
@Data
@Builder
public class DingTalkMessage implements Serializable {
/**
*
*/
private String msgtype;
/**
*
*/
private MessageContent text;
@Data
@Builder
public static class MessageContent {
/**
*
*/
private String content;
}
}

View File

@ -1,9 +1,11 @@
package cc.iotkit.message.model;
import java.io.Serializable;
/**
* author:
* date: 2023-05-08 15:58
* description:
**/
public class EmailMessage extends Message {
public class EmailMessage implements Serializable {
}

View File

@ -1,10 +1,14 @@
package cc.iotkit.message.model;
import lombok.Data;
/**
* author:
* date: 2023-05-08 15:15
* description:
**/
public abstract class Message {
@Data
public class Message {
private String key;
private String content;
}

View File

@ -0,0 +1,26 @@
package cc.iotkit.message.model;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
/**
* author:
* date: 2023-05-08 15:58
* description:
**/
@Data
@Builder
public class QyWechatMessage implements Serializable {
private String msgtype;
private MessageContent text;
@Data
@Builder
public static class MessageContent{
private String content;
}
}

View File

@ -0,0 +1,34 @@
package cc.iotkit.message.notify;
import cc.iotkit.message.model.DingTalkMessage;
import cc.iotkit.message.model.Message;
import io.vertx.core.Vertx;
import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j;
/**
* author:
* date: 2023-05-08 15:09
* description:
**/
@Slf4j
public class DingTalkEventListener implements EventListener{
private String baseUrl = "https://oapi.dingtalk.com/robot/send?access_token=%s";
@Override
public void doEvent(Message message) {
WebClient client = WebClient.create(Vertx.vertx());
String url = String.format(baseUrl, message.getKey());
DingTalkMessage qyWechatMessage = DingTalkMessage.builder()
.msgtype("text")
.text(DingTalkMessage.MessageContent.builder().content(message.getContent()).build())
.build();
client.post(url).sendJson(qyWechatMessage, rs -> {
if (rs.succeeded()) {
log.info("发送成功.");
}
if (rs.failed()) {
log.info("发送失败.");
}
});
}
}

View File

@ -0,0 +1,38 @@
package cc.iotkit.message.notify;
import cc.iotkit.message.model.Message;
import cc.iotkit.message.model.QyWechatMessage;
import io.vertx.core.Vertx;
import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* author:
* date: 2023-05-08 15:09
* description:
**/
@Slf4j
@Component
public class QyWechatEventListener implements EventListener {
private String baseUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s";
@Override
public void doEvent(Message message) {
WebClient client = WebClient.create(Vertx.vertx());
String url = String.format(baseUrl, message.getKey());
QyWechatMessage qyWechatMessage = QyWechatMessage.builder()
.msgtype("text")
.text(QyWechatMessage.MessageContent.builder().content(message.getContent()).build())
.build();
client.post(url).sendJson(qyWechatMessage, rs -> {
if (rs.succeeded()) {
log.info("发送成功.");
}
if (rs.failed()) {
log.info("发送失败.");
}
});
}
}

View File

@ -21,6 +21,7 @@
<module>iot-common-core</module>
<module>iot-script-engine</module>
<module>iot-message-bus</module>
<module>iot-message-notify</module>
</modules>