1.打开企业微信电脑版点击红框位置添加机器人
2.复制地址
3.代码环节Controller
@GetMapping(value = "testRobot")
public void testRobot() throws IOException {
//VO这段可以写在业务逻辑层
RobotVo vo = new RobotVo();
//机器人地址
vo.setWebhookAddress("刚才复制的地址");
//1.第一种情况:发送文本消息
// vo.setContent("我发送的消息是:文本消息");
// List<String> 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 {
/**
* 机器人id
*/
private String robotId;
/**
* 机器人名字
*/
private String robotName;
/**
* 当前机器人的webhook地址
*/
private String webhookAddress;
/**
* 消息类型
*/
private String msgType;
/**
* 富文本框里面的内容
*/
private String content;
/**
* 涉及发送的人员
*/
private List<String> memberList;
/**
* 电话
*/
private String mobileList;
/**
* 图片地址
*/
private String imageUrl;
/**
* base64编码后的值
*/
private String imageBase64Value;
/**
* 图片md5加密后的值
*/
private String imageMd5Value;
/**
* 发送消息的标题
*/
private String title;
/**
* 发送图文消息的描述信息
*/
private String description;
/**
* 图片url地址集合
*/
private List<String> imageUrlList;
/**
* 图片打开的地址
*/
private String url;
/**
* 消息内容集合
*/
private List<String> contentList;
/**
* 图片路径
*/
private String savePath;
}
5.Service接口
/**
* 机器人
* @param vo
* @throws IOException
*/
void run(RobotVo vo) throws IOException;
6.Service业务逻辑
@Override
public void run(RobotVo vo) throws IOException {
List<String> 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编码
/**
* 根据图片地址转换为base64编码字符串
* @param imgFile
* @return
*/
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 ~