feat: aos-robot 初步开发
This commit is contained in:
@@ -44,6 +44,11 @@
|
||||
<artifactId>aos-web</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aos</groupId>
|
||||
<artifactId>aos-robot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
asura.robot.wx.ids.test=dc62c56e-c194-4a22-87ff-d9d0e30f56b4
|
||||
spring.profiles.active=dev
|
||||
@@ -1,2 +0,0 @@
|
||||
spring.application.name=aos-gateway
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
spring.profiles.active=dev
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.aos.test;
|
||||
|
||||
import com.aos.robot.WXAlarm;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -10,8 +12,12 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@Slf4j
|
||||
public class ApplicationTest {
|
||||
|
||||
@Autowired
|
||||
WXAlarm wxAlarm;
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
Thread.sleep(1000000L);
|
||||
wxAlarm.alarm("test", "qwe");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.aos.test;
|
||||
|
||||
import com.aos.robot.WXAlarm;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(args = "--eureka.client.register-with-eureka=false", webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseTest.Application.class)
|
||||
public class BaseTest {
|
||||
|
||||
@Autowired
|
||||
WXAlarm wxAlarm;
|
||||
|
||||
@ComponentScan(basePackages = {"com.aos.test"})
|
||||
public static class Application {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void robotTest() {
|
||||
wxAlarm.alarm("test", "123");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>aos-dependencies</artifactId>
|
||||
<groupId>com.aos</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../aos-dependencies/pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>aos-robot</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.2.15.RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.aos.robot;
|
||||
|
||||
import com.aos.robot.model.MegTypeEnum;
|
||||
import com.aos.robot.model.RobotParam;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.exception.ExceptionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
* @date: 2022/6/25
|
||||
* @description:
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WXAlarm {
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String env;
|
||||
@Autowired
|
||||
WxRobot robot;
|
||||
|
||||
public void alarm(String robootName, String message, Throwable error) {
|
||||
String stackTrace = ExceptionUtils.getStackTrace(error);
|
||||
message = message + "\n" + "错误信息:" + error.getMessage();
|
||||
message = message + "\n" + stackTrace;
|
||||
this.makeTextMessage(robootName, message.substring(0, 2000));
|
||||
}
|
||||
|
||||
public void alarm(String robootName, String message, Object... value) {
|
||||
String format = message;
|
||||
if (value.length != 0) {
|
||||
format = MessageFormat.format(message, value);
|
||||
}
|
||||
this.makeTextMessage(robootName, format);
|
||||
}
|
||||
|
||||
public void markdown(String robotName, String message, Object... value) {
|
||||
String format = MessageFormat.format(message, value);
|
||||
this.makeMarkdownMessage(robotName, format);
|
||||
}
|
||||
|
||||
private void makeMarkdownMessage(String robotName, String message) {
|
||||
RobotParam robotParam = new RobotParam();
|
||||
robotParam.setMsgtype(MegTypeEnum.MARKDOWN);
|
||||
RobotParam.Markdown markdown = new RobotParam.Markdown();
|
||||
message = "## 当前时间 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "\n" + message;
|
||||
markdown.setContent(message);
|
||||
robotParam.setMarkdown(markdown);
|
||||
this.send(robotName, robotParam);
|
||||
}
|
||||
|
||||
|
||||
private void makeTextMessage(String robotName, String message) {
|
||||
message = "当前环境:" + env + "\n" + "当前时间:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "\n" + message;
|
||||
RobotParam robotParam = new RobotParam();
|
||||
RobotParam.Text text = new RobotParam.Text();
|
||||
text.setContent(message);
|
||||
robotParam.setText(text);
|
||||
this.send(robotName, robotParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本格式消息
|
||||
*
|
||||
* @param robotName
|
||||
* @param robotParam
|
||||
* @return void
|
||||
* @Date 2022/8/5
|
||||
* @Author wangyl
|
||||
*/
|
||||
private void send(String robotName, RobotParam robotParam) {
|
||||
robot.sendMessage(robotName, robotParam);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.aos.robot;
|
||||
|
||||
import com.aos.robot.model.RobotConfig;
|
||||
import com.aos.robot.model.RobotParam;
|
||||
import com.aos.robot.model.RobotRes;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WxRobot {
|
||||
@Resource(name = "robotRestTemplate")
|
||||
RestTemplate restTemplate;
|
||||
@Resource
|
||||
RobotConfig robotConfig;
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param robotName 机器人名称
|
||||
* @param robotParam 机器人参数
|
||||
* @return void
|
||||
* @Date 2022/8/5
|
||||
* @Author wangyl
|
||||
*/
|
||||
public void sendMessage(String robotName, RobotParam robotParam) {
|
||||
String key = robotConfig.getIds().get(robotName);
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
throw new RuntimeException("获取机器人名称:" + robotName + "对应的id失败,请检查配置asura.robot.wx.ids." + robotName + "是否存在");
|
||||
}
|
||||
String url = RobotConfig.URL + key;
|
||||
ResponseEntity<RobotRes> stringResponseEntity = restTemplate.postForEntity(url, robotParam, RobotRes.class);
|
||||
if (stringResponseEntity.getStatusCode() != HttpStatus.OK) {
|
||||
throw new RuntimeException("消息发送异常,网络代码【" + stringResponseEntity.getStatusCodeValue() + "】");
|
||||
}
|
||||
RobotRes body = stringResponseEntity.getBody();
|
||||
if (!body.succeed()) {
|
||||
log.error("消息发送异常【{}】,错误码【{}】", body.getErrmsg(), body.getErrcode());
|
||||
throw new RuntimeException("消息发送异常,【" + body.getErrmsg() + "】");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.aos.robot.conf;
|
||||
|
||||
import com.aos.robot.model.RobotConfig;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.aos.robot"})
|
||||
public class AutoRobotConfig {
|
||||
|
||||
@Bean("robotRestTemplate")
|
||||
public RestTemplate restTemplate() {
|
||||
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用OkHttpClient作为底层客户端
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private ClientHttpRequestFactory getClientHttpRequestFactory() {
|
||||
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS)
|
||||
.writeTimeout(5, TimeUnit.SECONDS)
|
||||
.readTimeout(5, TimeUnit.SECONDS)
|
||||
.build();
|
||||
return new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "asura.robot.wx")
|
||||
public RobotConfig readDruidDataSource() {
|
||||
return new RobotConfig();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.aos.robot.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: zouyang
|
||||
* @date: 2022/6/1
|
||||
* @description: 活动需求状态
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum MegTypeEnum {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
TEXT(0, "text"),
|
||||
MARKDOWN(1, "markdown"),
|
||||
;
|
||||
private static final Map<Integer, MegTypeEnum> megTypeMAP = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (MegTypeEnum type : MegTypeEnum.values()) {
|
||||
megTypeMAP.put(type.getValue(), type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际值
|
||||
*/
|
||||
private final Integer value;
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
@JsonValue
|
||||
private final String desc;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.aos.robot.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Data
|
||||
public class RobotConfig {
|
||||
private Map<String, String> ids = new HashMap<>();
|
||||
final static public String URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.aos.robot.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RobotParam {
|
||||
MegTypeEnum msgtype = MegTypeEnum.TEXT;
|
||||
Text text;
|
||||
Markdown markdown;
|
||||
|
||||
@Data
|
||||
public static class Text {
|
||||
String content;
|
||||
List<String> mentioned_list;
|
||||
List<String> mentioned_mobile_list;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Markdown {
|
||||
String content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.aos.robot.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
* @date: 2022/8/5
|
||||
* @description: 企业微信机器人返回信息
|
||||
*/
|
||||
@Data
|
||||
public class RobotRes {
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
Integer errcode;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
String errmsg;
|
||||
|
||||
/**
|
||||
* 判断是否成功
|
||||
*
|
||||
* @param
|
||||
* @return java.lang.Boolean
|
||||
* @Date 2022/8/5
|
||||
* @Author wangyl
|
||||
*/
|
||||
public Boolean succeed() {
|
||||
return errcode == 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.aos.robot.conf.AutoRobotConfig
|
||||
@@ -47,5 +47,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
Reference in New Issue
Block a user