首页 > 微信 > 详细

企业微信群聊机器人发送群聊消息

时间:2021-07-16 15:27:18      阅读:29      评论:0      收藏:0      [点我收藏+]
@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());
}

企业微信群聊机器人发送群聊消息

原文:https://www.cnblogs.com/Marlo/p/15019963.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!