@GetMapping(value = "testRobot")
public void testRobot() throws IOException {
//VO这段可以写在业务逻辑层
RobotVo vo = new RobotVo();
//机器人地址
vo.setWebhookAddress("刚才复制的地址");
//1.第一种情况:发送文本消息
// vo.setContent("我发送的消息是:文本消息");
// List memberList = new ArrayList<>();
// memberList.add("@all");
// vo.setMemberList(memberList);
// vo.setMsgType("text");
// 2.第二种情况,发送图片消息
// vo.setMsgType("image");
// vo.setSavePath("C:/Users/Administrator/Desktop/吴彦祖.jpg");
//3.第三种情况,发送机器人消息
vo.setMsgType("news");
vo.setTitle("test");
vo.setDescription("test");
vo.setUrl("url");
// vo.setImageUrl("url")括号中的url是上传oss返回的路径
vo.setImageUrl("url");
service.run(vo);
}
4.VO
@Data
public class RobotVo {
private String robotId;
private String robotName;
private String webhookAddress;
private String msgType;
private String content;
private List memberList;
private String mobileList;
private String imageUrl;
private String imagebase64Value;
private String imageMd5Value;
private String title;
private String description;
private List imageUrlList;
private String url;
private List contentList;
private String savePath;
}
5.Service接口
void run(RobotVo vo) throws IOException;
6.Service业务逻辑
@Override
public void run(RobotVo vo) throws IOException {
List memberList = vo.getMemberList();
String jsonData = "";
String mobileList = "";
String strMember = "";
if (vo.getMsgType().equals("text")) {
if (!Strings.isNullOrEmpty(vo.getMobileList())) {
mobileList = vo.getMobileList();
} else {
mobileList = "";
}
for (int i = 0; i < memberList.size(); i++) {
if (i == memberList.size() - 1) {
strMember += """ + memberList.get(i) + """;
} else {
strMember += """ + memberList.get(i) + """ + ",";
}
}
String[] members = new String[memberList.size()];
for (int i = 0; i < memberList.size(); i++) {
members[i] = memberList.get(i);
}
jsonData = "{n" +
"t"msgtype": "" + vo.getMsgType() + "",n" +
" "text": {n" +
" "content": "" + vo.getContent() + "",n" +
" "mentioned_list":[" + strMember + "],n" +
" "mentioned_mobile_list":["" + mobileList + ""]n" +
" }n" +
"}";
} else if (vo.getMsgType().equals("image")) {
//图片base64加密的值
vo.setImagebase64Value(getImageStr(vo.getSavePath()));
//图片md5加密的值
vo.setImageMd5Value(DigestUtils.md5Hex(new FileInputStream(vo.getSavePath())));
jsonData = "{n" +
" "msgtype": "" + vo.getMsgType() + "",n" +
" "image": {n" +
" "base64": "" + vo.getImagebase64Value() + "",n" +
" "md5": "" + vo.getImageMd5Value() + ""n" +
" }n" +
"}";
} else if (vo.getMsgType().equals("news")) {
//图片+文字消息
vo.setTitle(!Strings.isNullOrEmpty(vo.getTitle()) ? vo.getTitle() : "");
jsonData = "{n" +
" "msgtype": "" + vo.getMsgType() + "",n" +
" "news": {n" +
" "articles" : [n" +
" {n" +
" "title" : "" + vo.getTitle() + "",n" +
" "description" : "" + vo.getDescription() + "",n" +
" "url" : "" + vo.getUrl() + "",n" +
" "picurl" : "" + vo.getImageUrl() + ""n" +
" }n" +
" ]n" +
" }n" +
"}";
}
send(vo.getWebhookAddress(), jsonData);
}
7.将图片地址转化成base64编码
public static String getImageStr(String imgFile) {
//要想发送图片内容需要将图片加密转化成base64编码
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 图片加密
base64.Encoder er= base64.getEncoder();
return er.encodeToString(data);
}
8.send方法
public static JSONObject send(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
JSONObject jsonObject = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流(设置请求编码为UTF-8)
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 获取请求返回数据(设置返回数据编码为UTF-8)
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
jsonObject = JSONObject.parseObject(result);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return jsonObject;
}
9.效果图
end ~



