feat: aos-robot 优化

This commit is contained in:
王永亮
2022-11-10 18:38:15 +08:00
parent 4b2a08e1c9
commit acfb2e830a
14 changed files with 123 additions and 51 deletions
+6 -4
View File
@@ -35,10 +35,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.aos</groupId>
<artifactId>aos-web</artifactId>
@@ -49,6 +45,12 @@
<artifactId>aos-robot</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.9</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -1,2 +1,2 @@
asura.robot.wx.ids.test=dc62c56e-c194-4a22-87ff-d9d0e30f56b4
aos.robots.wx.id=dc62c56e-c194-4a22-87ff-d9d0e30f56b4
spring.profiles.active=dev
@@ -18,6 +18,6 @@ public class ApplicationTest {
@Test
public void test() throws InterruptedException {
wxAlarm.alarm("test", "qwe");
wxAlarm.alarm("wx", "qwe");
}
}
@@ -1,6 +1,8 @@
package com.aos.test;
import cn.hutool.json.JSONUtil;
import com.aos.robot.WXAlarm;
import lombok.Data;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -8,6 +10,10 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
@RunWith(SpringRunner.class)
@SpringBootTest(args = "--eureka.client.register-with-eureka=false", webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseTest.Application.class)
public class BaseTest {
@@ -22,6 +28,22 @@ public class BaseTest {
@Test
public void robotTest() {
wxAlarm.alarm("test", "123");
tmp tmp = new tmp();
tmp.setAge(1);
tmp.setName("wyl");
String test = JSONUtil.toJsonStr(tmp);
String format = MessageFormat.format("测试 {0},{1}", test, test);
System.out.println(format);
// wxAlarm.alarm("test", "123");
}
@Data
public class tmp {
String name;
Integer age;
LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.now();
}
}
@@ -1,7 +1,7 @@
package com.aos.robot;
import com.aos.robot.model.MegTypeEnum;
import com.aos.robot.model.RobotParam;
import com.aos.robot.model.WxRobotParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -47,9 +47,9 @@ public class WXAlarm {
}
private void makeMarkdownMessage(String robotName, String message) {
RobotParam robotParam = new RobotParam();
WxRobotParam robotParam = new WxRobotParam();
robotParam.setMsgtype(MegTypeEnum.MARKDOWN);
RobotParam.Markdown markdown = new RobotParam.Markdown();
WxRobotParam.Markdown markdown = new WxRobotParam.Markdown();
message = "## 当前时间 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "\n" + message;
markdown.setContent(message);
robotParam.setMarkdown(markdown);
@@ -59,8 +59,8 @@ public class WXAlarm {
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();
WxRobotParam robotParam = new WxRobotParam();
WxRobotParam.Text text = new WxRobotParam.Text();
text.setContent(message);
robotParam.setText(text);
this.send(robotName, robotParam);
@@ -75,7 +75,7 @@ public class WXAlarm {
* @Date 2022/8/5
* @Author wangyl
*/
private void send(String robotName, RobotParam robotParam) {
private void send(String robotName, WxRobotParam robotParam) {
robot.sendMessage(robotName, robotParam);
}
@@ -1,9 +1,12 @@
package com.aos.robot;
import com.aos.robot.model.RobotConfig;
import com.aos.robot.model.RobotParam;
import com.aos.robot.model.RobotRes;
import com.aos.robot.model.RobotInfo;
import com.aos.robot.model.RobotPatam;
import com.aos.robot.model.WxRobotRes;
import com.aos.robot.service.Robot;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@@ -14,10 +17,11 @@ import javax.annotation.Resource;
@Slf4j
@Component
public class WxRobot {
public class WxRobot implements Robot {
@Resource(name = "robotRestTemplate")
RestTemplate restTemplate;
@Resource
@Autowired
RobotConfig robotConfig;
/**
@@ -29,17 +33,18 @@ public class WxRobot {
* @Date 2022/8/5
* @Author wangyl
*/
public void sendMessage(String robotName, RobotParam robotParam) {
String key = robotConfig.getIds().get(robotName);
if (StringUtils.isEmpty(key)) {
@Override
public void sendMessage(String robotName, RobotPatam robotParam) {
RobotInfo robotInfo = robotConfig.getRobots().get(robotName);
if (StringUtils.isEmpty(robotInfo)) {
throw new RuntimeException("获取机器人名称:" + robotName + "对应的id失败,请检查配置asura.robot.wx.ids." + robotName + "是否存在");
}
String url = RobotConfig.URL + key;
ResponseEntity<RobotRes> stringResponseEntity = restTemplate.postForEntity(url, robotParam, RobotRes.class);
String url = robotInfo.getUrl() + robotInfo.getId();
ResponseEntity<WxRobotRes> stringResponseEntity = restTemplate.postForEntity(url, robotParam, WxRobotRes.class);
if (stringResponseEntity.getStatusCode() != HttpStatus.OK) {
throw new RuntimeException("消息发送异常,网络代码【" + stringResponseEntity.getStatusCodeValue() + "");
}
RobotRes body = stringResponseEntity.getBody();
WxRobotRes body = stringResponseEntity.getBody();
if (!body.succeed()) {
log.error("消息发送异常【{}】,错误码【{}】", body.getErrmsg(), body.getErrcode());
throw new RuntimeException("消息发送异常,【" + body.getErrmsg() + "");
@@ -36,7 +36,7 @@ public class AutoRobotConfig {
}
@Bean
@ConfigurationProperties(prefix = "asura.robot.wx")
@ConfigurationProperties(prefix = "aos")
public RobotConfig readDruidDataSource() {
return new RobotConfig();
}
@@ -8,7 +8,5 @@ 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=";
private Map<String, RobotInfo> robots = new HashMap<>();
}
@@ -0,0 +1,10 @@
package com.aos.robot.model;
import lombok.Data;
@Data
public class RobotInfo {
private String id;
private String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
}
@@ -0,0 +1,4 @@
package com.aos.robot.model;
public interface RobotPatam {
}
@@ -1,32 +1,14 @@
package com.aos.robot.model;
import lombok.Data;
/**
* @author: wangyl
* @date: 2022/8/5
* @description: 企业微信机器人返回信息
*/
@Data
public class RobotRes {
/**
* 错误码
*/
Integer errcode;
/**
* 错误信息
*/
String errmsg;
public interface RobotRes {
String getErrcode();
/**
* 判断是否成功
*
* @param
* @return java.lang.Boolean
* @Date 2022/8/5
* @Author wangyl
*/
public Boolean succeed() {
return errcode == 0;
}
String getErrmsg();
Boolean succeed();
}
@@ -5,7 +5,7 @@ import lombok.Data;
import java.util.List;
@Data
public class RobotParam {
public class WxRobotParam implements RobotPatam {
MegTypeEnum msgtype = MegTypeEnum.TEXT;
Text text;
Markdown markdown;
@@ -0,0 +1,33 @@
package com.aos.robot.model;
import lombok.Data;
/**
* @author: wangyl
* @date: 2022/8/5
* @description: 企业微信机器人返回信息
*/
@Data
public class WxRobotRes implements RobotRes {
/**
* 错误码
*/
String errcode;
/**
* 错误信息
*/
String errmsg;
/**
* 判断是否成功
*
* @param
* @return java.lang.Boolean
* @Date 2022/8/5
* @Author wangyl
*/
@Override
public Boolean succeed() {
return "0".equals(errcode);
}
}
@@ -0,0 +1,16 @@
package com.aos.robot.service;
import com.aos.robot.model.RobotPatam;
public interface Robot {
/**
* 发送消息
*
* @param robotName
* @param robotParam
* @return void
* @Date 2022/11/10
* @Author wangyl
*/
void sendMessage(String robotName, RobotPatam robotParam);
}