一、钉钉机器人相关设置
1、首先要有一个钉钉群
2、群设置-》智能群助手-〉添加机器人-》自定义机器人
3、机器人设置
注意这里的小细节:
安全设置,
自定义关键词:
当发送的信息包含关键词时,机器人才会把消息发送出来。
点击完成
Webhook:
https://oapi.dingtalk.com/robot/send?access_token=abcdef
消息格式:
{
"text": {
"content":"我就是我, @XXX 是不一样的烟火"
},
"msgtype":"text"
}
然后发送请求就可以了
post请求,url 与 内容都有了,发送请求就可以正常使用了
------------------------------------------------------------------------------------------------------------------------------
二、在自动化框架内构建http请求来发送报警
1、http请求方法 HttpService
package com.example.autoapi.http.service;
import com.google.gson.Gson;
import okhttp3.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class HttpService {
public String doPostJson(String url,Object data){
return doPostJson(url,null,data);
}
public String doPostJson(String url, Map<String,String> headers,Object data){
OkHttpClient client = new OkHttpClient();
// 1、构建请求
MediaType mediaType = MediaType.parse("application/json");
// Gson是json工具类,将x转成json
Gson gson = new Gson();
RequestBody requestBody = RequestBody.create(gson.toJson(data), mediaType);
Request request = new Request.Builder()
.url(url)
.headers(Headers.of(headers==null?new HashMap<>():headers))
.post(requestBody)
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
String body = response.body().string();
return body;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
1、http请求用的okhttp3包里的方法,这里看个人喜好,包有很多
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
2、这里构造的是post请求,body 是json格式的
2、HttpFacade
package com.example.autoapi.http;
import com.example.autoapi.http.service.HttpService;
public class HttpFacade {
public static String doPostJson(String url,Object data){
return new HttpService().doPostJson(url, data);
}
}
3、AlarmService
最后在AlarmService里,构造http请求,发送钉钉报警
package com.example.autoapi.alarm.service;
import com.example.autoapi.annotation.CaseDesc;
import com.example.autoapi.annotation.CaseTitle;
import com.example.autoapi.annotation.CheckPoint;
import com.example.autoapi.http.HttpFacade;
import com.example.autoapi.model.FailureResult;
import com.example.autoapi.template.TemplateFacade;
import com.example.autoapi.util.ReflectUtils;
import com.google.common.base.Joiner;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
// 钉钉报警
public class AlarmService {
public void doAlarm(FailureResult failureResult,String token){
String className = failureResult.getClassName();
String methodName = failureResult.getMethodName();
Method method = ReflectUtils.getMethod(className,methodName);
String title = null;
String desc = null;
String owner = null;
List<String> cps = null;
String caseId = className+"#"+methodName;
String failureMsg = failureResult.getThrowable().getMessage();
if(method.isAnnotationPresent(CaseTitle.class)){
// 判断case的标题是否存在
title = method.getAnnotation(CaseTitle.class).value();
}
desc = method.getAnnotation(CaseDesc.class).desc();
owner = method.getAnnotation(CaseDesc.class).owner();
CheckPoint[] checkPoints = method.getAnnotationsByType(CheckPoint.class);
cps= Arrays.stream(checkPoints).map(checkPoint -> checkPoint.value()).collect(Collectors.toList());
Map<String,Object> map = new HashMap<>();
map.put("case_title",title);
map.put("case_id",caseId);
map.put("case_desc",desc);
map.put("case_owner",owner);
map.put("case_cps", Joiner.on(",").join(cps));
map.put("failure_msg",failureMsg);
String template = TemplateFacade.replaceTemplate("default_alarm_template", map);
// System.out.println("template = " + template);
/*
{
"text": {
"content":"我就是我, @XXX 是不一样的烟火"
},
"msgtype":"text"
}
*/
Map<String,String> text = new HashMap<>();
text.put("content",template);
Map<String,Object> data = new HashMap<>();
data.put("text",text);
data.put("msgtype","text");
String url = DING_ALARM_URL + token;
HttpFacade.doPostJson(url,data);
}
private final static String DING_ALARM_URL = "https://oapi.dingtalk.com/robot/send?access_token=";
}