【JAVA】java 企业微信信息推送
前言
JAVA中 将信息 推送到企业微信
// 企微消息推送message
private String getMessage(String name, String problemType, String pushResults, Long orderId,
java.util.Date submitTime, java.util.Date payTime) {
String message = "对接方:+<font color=\"info\">" + name + "</font>" + "\n" + ">问题类型:+<font color=\"info\">"
+ problemType + "</font>" + "\n" + ">推送结果:+<font color=\"info\">" + pushResults + "</font>" + "\n"
+ ">订单号:+<font color=\"info\">" + orderId + "</font>" + "\n" + ">订单产生时间:+<font color=\"info\">"
+ submitTime + "</font>" + "\n" + ">订单支付时间:+<font color=\"info\">" + payTime + "</font>" + "\n"
+ ">最后重推时间:+<font color=\"info\">" + new Date() + "</font>";
return message;
}
业务逻辑实现
LoggerFactory.getLogger(CtgExtendController.class).info("企业微信消息发送开始");
String message = getMessage(log);
String url = ConfigDao.getAppConfig().getConfig("fail_order_push_robot","https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=49308bf4-3e4c-47d8-95a9-1dbfd7984b66");
ExceptionMessageModel exceptionMessageModel = new ExceptionMessageModel();
exceptionMessageModel.setMessage(message);
exceptionMessageModel.setRobotUrl(url);
exceptionMessageModel.setTemplateCode(WxEtpRobotMsgEnum.MSG_ORDER_PUSH_H5.msgCode);
exceptionMessageModel.setJsonParam(JSONUtility.objectToNotNullJson(resVo));
//发送消息实现
if (StringUtils.isNotBlank(exceptionMessageModel.getTemplateCode()) && StringUtils.isNotBlank(exceptionMessageModel.getJsonParam())) {
//优先取配置项中的模板配置
String msg = WxEtpRobotMsgEnum.covertMsg(exceptionMessageModel.getTemplateCode(), exceptionMessageModel.getJsonParam());
if(StringUtils.isNotBlank(msg)){
exceptionMessageModel.setMessage(msg);
}
}
LoggerFactory.getLogger(CtgExtendController.class).info("异常信息推送埋点进入,入参:"+ exceptionMessageModel.toString());
WxRobotUtil.sendMsg(exceptionMessageModel.getRobotUrl(), exceptionMessageModel.getMessage());
LoggerFactory.getLogger(CtgExtendController.class).info("企业微信消息发送成功");
public class WxRobotUtil {
private static final Logger logger= LoggerFactory.getLogger(WxRobotUtil.class);
public static void sendMsg(String url,String msg){
new CommonThread("","WxRobotUtil"){
@Override
public void run() {
try {
//拼装发送消息
Map<String, Object> map = new HashMap<>();
Map<String, String> param = new HashMap<>();
map.put("msgtype", "markdown");
param.put("content","时间:"+new Date().getDateTimeStr()+"\n"+msg);
map.put("markdown", param);
String result = HttpUtility.doPost(url, JSONUtility.objectToJson(map), ContentType.APPLICATION_JSON.getMimeType());
logger.info("微信机器人消息:"+url+"---"+msg);
}
catch (Exception e){
logger.error("异常:"+url+"---"+msg,e);
}
}
}.start();
}
}
public class WxRobotUtil {
private static final Logger logger= LoggerFactory.getLogger(WxRobotUtil.class);
public static void sendMsg(String url,String msg){
new CommonThread("","WxRobotUtil"){
@Override
public void run() {
try {
//拼装发送消息
Map<String, Object> map = new HashMap<>();
Map<String, String> param = new HashMap<>();
map.put("msgtype", "markdown");
param.put("content","时间:"+new Date().getDateTimeStr()+"\n"+msg);
map.put("markdown", param);
String result = HttpUtility.doPost(url, JSONUtility.objectToJson(map), ContentType.APPLICATION_JSON.getMimeType());
logger.info("微信机器人消息:"+url+"---"+msg);
}
catch (Exception e){
logger.error("异常:"+url+"---"+msg,e);
}
}
}.start();
}
}