自动开服,钉钉通知

master
duhui 2021-08-25 17:07:09 +08:00
parent 92a97c63d3
commit 0fb25f4f85
4 changed files with 149 additions and 6 deletions

View File

@ -52,6 +52,14 @@
<optional>true</optional>
</dependency>
<!--糊涂工具-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.12</version>
</dependency>
<!--数据库访问支持JPAJava Persistence API包括spring-data-jpa、spring-orm、Hibernate-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,126 @@
package com.jmfy.handler;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.jmfy.model.ServerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author hj
* @Date 2021/8/25 16:11:31
* @Description:
* @Version 1.0
*/
@Component
public class DingTalkLogic {
/**
*
*/
private static final Logger LOGGER = LoggerFactory.getLogger(DingTalkLogic.class);
/**
*
*/
private static DingTalkLogic instance = new DingTalkLogic();
public static DingTalkLogic getInstance(){
return instance;
}
/**
* webhook
*/
@Value("${services.dingurl}")
private String dingurl;
/**
*
*/
private static final int timeout = 10000;
/**
*
* @param serverInfo
* @param debug
*/
public void sendDingTalk(ServerInfo serverInfo, boolean debug){
try {
//是否通知所有人
boolean isAtAll = false;
//通知具体人的手机号码列表
List<String> mobileList = new ArrayList<>();
//钉钉机器人消息内容
String content = "开服提醒";
if (debug){
content = "开服提醒, hello world!";
}else {
content = "开服提醒," +
"\n自动开服报错服务器id" + serverInfo.getServer_id() +
"\n服务器名字" + serverInfo.getName() +
"\n服务器当前状态" + serverInfo.getStatus() +
"\n" +
"\n服务器全部信息" + serverInfo.toString();
}
//组装请求内容
String reqStr = buildReqStr(content, isAtAll, mobileList);
//推送消息http请求
String result = postJson(dingurl, reqStr);
System.out.println("result == " + result);
}catch (Exception e){
LOGGER.error("发送钉钉信息报错=========={}",e.getMessage());
e.printStackTrace();
}
}
/**
*
* @param url
* @param reqStr
* @return
*/
public static String postJson(String url, String reqStr) {
String body = null;
try {
body = HttpRequest.post(url).body(reqStr).timeout(timeout).execute().body();
} catch (Exception e) {
e.printStackTrace();
}
return body;
}
/**
*
* @param content
* @return
*/
private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
//消息内容
Map<String, String> contentMap = new HashMap<>();
contentMap.put("content", content);
//通知人
Map<String, Object> atMap = new HashMap<>();
//1.是否通知所有人
atMap.put("isAtAll", isAtAll);
//2.通知具体人的手机号码列表
atMap.put("atMobiles", mobileList);
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("msgtype", "text");
reqMap.put("text", contentMap);
reqMap.put("at", atMap);
return JSON.toJSONString(reqMap);
}
}

View File

@ -66,6 +66,12 @@ public class ServerInfo implements Comparable {
@Field(value = "coreName")
private String coreName;
@Field(value = "server_version")
private String server_version;
@Field(value = "isWhite")
private String isWhite;
public ServerInfo(int _id, String name) {
this._id = _id;
this.server_id = String.valueOf(_id);
@ -197,6 +203,8 @@ public class ServerInfo implements Comparable {
", open_type='" + open_type + '\'' +
", register_state='" + register_state + '\'' +
", coreName='" + coreName + '\'' +
", server_version='" + server_version + '\'' +
", isWhite='" + isWhite + '\'' +
'}';
}

View File

@ -1,6 +1,7 @@
package com.jmfy.utils;
import com.jmfy.dao.ServerInfoDao;
import com.jmfy.handler.DingTalkLogic;
import com.jmfy.model.AutoServerSetting;
import com.jmfy.model.CServerOpenTime;
import com.jmfy.model.ServerInfo;
@ -45,6 +46,8 @@ public class AutoServerManager {
@Resource
private ServerInfoDao serverInfoDao;
@Resource
private DingTalkLogic dingTalkLogic;
/**
*
@ -115,12 +118,10 @@ public class AutoServerManager {
// 验证新服状态
CServerOpenTime cServerOpenTime = serverInfoDao.getOpenServerTime(newServer.getServer_id());
if (null == cServerOpenTime) {
LOGGER.error("新服自动开服失败1================={}",newServer.toString());
return;
}
if (cServerOpenTime.getOpenTime() == null || "".equals(cServerOpenTime.getOpenTime()) || cServerOpenTime.getVersion() <= 0){
LOGGER.error("新服自动开服失败2================={}",newServer.toString());
if (null == cServerOpenTime || cServerOpenTime.getOpenTime() == null || "".equals(cServerOpenTime.getOpenTime()) || cServerOpenTime.getVersion() <= 0){
LOGGER.error("新服自动开服失败================={}",newServer.toString());
// 通知钉钉
dingTalkLogic.sendDingTalk(newServer,false);
return;
}
// 旧服务器状态更新