@Override
public WxResponse robotMsgSend(String missionId, RobotMsg robotMsg) {
try {
log.info("机器人发送群聊消息开始,missionId:{},robotMsg:{}", missionId, robotMsg);
List<String> memberList = robotMsg.getMemberList();
String jsonData = "";
String mobileList = "";
String strMember = "";
if (robotMsg.getMsgType().equals(AppMsgType.TEXT.value())) {
if (!Strings.isNullOrEmpty(robotMsg.getMobileList())) {
mobileList = robotMsg.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\": \"" + robotMsg.getMsgType() + "\",\n" +
" \"text\": {\n" +
" \"content\": \"" + robotMsg.getContent() + "\",\n" +
" \"mentioned_list\":[" + strMember + "],\n" +
" \"mentioned_mobile_list\":[\"" + mobileList + "\"]\n" +
" }\n" +
"}";
} else if (robotMsg.getMsgType().equals(AppMsgType.IMAGE.value())) {
//图片base64加密的值
robotMsg.setImageBase64Value(ImageUtil.getImageStr(robotMsg.getSavePath()));
//图片md5加密的值
robotMsg.setImageMd5Value(DigestUtils.md5Hex(new FileInputStream(robotMsg.getSavePath())));
jsonData = "{\n" +
" \"msgtype\": \"" + robotMsg.getMsgType() + "\",\n" +
" \"image\": {\n" +
" \"base64\": \"" + robotMsg.getImageBase64Value() + "\",\n" +
" \"md5\": \"" + robotMsg.getImageMd5Value() + "\"\n" +
" }\n" +
"}";
} else if (robotMsg.getMsgType().equals(AppMsgType.NEWS.value())) {
//图文消息
robotMsg.setTitle(!Strings.isNullOrEmpty(robotMsg.getTitle()) ? robotMsg.getTitle() : "");
jsonData = "{\n" +
" \"msgtype\": \"" + robotMsg.getMsgType() + "\",\n" +
" \"news\": {\n" +
" \"articles\" : [\n" +
" {\n" +
" \"title\" : \"" + robotMsg.getTitle() + "\",\n" +
" \"description\" : \"" + robotMsg.getDescription() + "\",\n" +
" \"url\" : \"" + robotMsg.getUrl() + "\",\n" +
" \"picurl\" : \"" + robotMsg.getImageUrl() + "\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
}
log.info("missionId:{},企业微信发送应用消息请求报文:{},Webhook:{}", missionId, jsonData, robotMsg.getWebhookAddress());
String res = HttpUtil.postJson(robotMsg.getWebhookAddress(), jsonData);
log.info("missionId:{},企业微信发送应用消息返回报文:{}", res);
WxResponse wxResponse = JSON.parseObject(res, WxResponse.class);
return wxResponse;
} catch (Exception e) {
log.info("发送机器人消息失败:{}", e.getMessage());
return new WxResponse(-1, e.getMessage());
}