Commit 2691b976 authored by zhouwei's avatar zhouwei

add alarm service

parent 18300239
package com.nanyan.securitylink.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
@Slf4j
@RefreshScope
@Configuration
public class AppConfig {
@Value("${alert.host}")
String alertHost;
public String getAlertHost() {
return alertHost;
}
}
package com.nanyan.securitylink.entity;
import com.nanyan.securitylink.common.MsgCode;
public class Response<T> {
String msg;
int code;
T data;
public static Response getResponse(MsgCode msgCode){
Response response = new Response();
response.setCode(msgCode.getCode());
response.setMsg(msgCode.getMsg());
return response;
}
public static Response getResponse(int code, String msg){
Response response = new Response();
response.setCode(code);
response.setMsg(msg);
return response;
}
public static<T> Response<T> SUCCESS(T data){
Response response = new Response<T>();
response.setCode(MsgCode.SUCCESS.getCode());
response.setMsg(MsgCode.SUCCESS.getMsg());
response.setData(data);
return response;
}
public static<T> Response<T> FAILED(T data){
Response response = new Response();
response.setCode(MsgCode.FAILED.getCode());
response.setMsg(MsgCode.FAILED.getMsg());
response.setData(data);
return response;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
package com.nanyan.securitylink.entity;
import lombok.Data;
@Data
public class WeChatMsg {
String msgtype;
MsgText text;
MsgText markdown;
public static class MsgText{
public String content;
}
}
package com.nanyan.securitylink.service;
import com.alibaba.fastjson.JSONObject;
import com.nanyan.securitylink.config.AppConfig;
import com.nanyan.securitylink.entity.WeChatMsg;
import com.nanyan.securitylink.vo.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
@Slf4j
@Service
public class AlertService {
@Autowired
RestTemplate restTemplate;
@Autowired
AppConfig appConfig;
public boolean sendAlertMsg(String msg){
WeChatMsg weChatMsg = buildAlertMsg(msg);
log.info("发送企业微信msg:{}", JSONObject.toJSONString(weChatMsg));
HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(weChatMsg),getHeader().getHeaders());
ResponseEntity<Response> response = restTemplate.postForEntity(String.format("%s/api/v1/msg", appConfig.getAlertHost()), entity, Response.class);
log.info("发送企业微信返回:{}", JSONObject.toJSONString(response));
return true;
}
private WeChatMsg buildAlertMsg(String content) {
if(content.length()>4000){
content = content.substring(0,4000);
}
WeChatMsg weChatMsg = new WeChatMsg();
weChatMsg.setMsgtype("markdown");
WeChatMsg.MsgText msgText = new WeChatMsg.MsgText();
String builder = "## dify异常\n" +
">内容:<font color=\"comment\"> " + content + "</font>\n";
msgText.content = builder;
weChatMsg.setMarkdown(msgText);
return weChatMsg;
}
private HttpEntity getHeader(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type","application/json");
return new HttpEntity<>(httpHeaders);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment