| | |
| | | * @throws Exception |
| | | */ |
| | | public static void inform(String user, String content, File imageFile) throws Exception { |
| | | WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); |
| | | // 注册的企业信息 |
| | | config.setCorpId(CORP_ID); |
| | | config.setCorpSecret(CORP_SECRET); |
| | | |
| | | WxCpServiceImpl wxCpService = new WxCpServiceImpl(); |
| | | wxCpService.setWxCpConfigStorage(config); |
| | | |
| | | WxCpMessage message; |
| | | WxCpMessageSendResult result; |
| | | // 发送文本 |
| | | if (StringUtils.isNotBlank(content)) { |
| | | message = WxCpMessage.TEXT() |
| | | .content(content) |
| | | // 用户id |
| | | .toUser(user) |
| | | // 应用id |
| | | .agentId(AGENT_ID) |
| | | .build(); |
| | | result = wxCpService.getMessageService().send(message); |
| | | } |
| | | // 发送文件 |
| | | if (imageFile != null) { |
| | | // 获取文件id |
| | | WxMediaUploadResult imgResult = wxCpService.getMediaService().upload("file", imageFile); |
| | | message = WxCpMessage.IMAGE() |
| | | .mediaId(imgResult.getMediaId()) |
| | | // 用户id |
| | | .toUser(user) |
| | | // 应用id |
| | | .agentId(AGENT_ID) |
| | | .build(); |
| | | result = wxCpService.getMessageService().send(message); |
| | | } |
| | | return; |
| | | // WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); |
| | | // // 注册的企业信息 |
| | | // config.setCorpId(CORP_ID); |
| | | // config.setCorpSecret(CORP_SECRET); |
| | | // |
| | | // WxCpServiceImpl wxCpService = new WxCpServiceImpl(); |
| | | // wxCpService.setWxCpConfigStorage(config); |
| | | // |
| | | // WxCpMessage message; |
| | | // WxCpMessageSendResult result; |
| | | // // 发送文本 |
| | | // if (StringUtils.isNotBlank(content)) { |
| | | // message = WxCpMessage.TEXT() |
| | | // .content(content) |
| | | // // 用户id |
| | | // .toUser(user) |
| | | // // 应用id |
| | | // .agentId(AGENT_ID) |
| | | // .build(); |
| | | // result = wxCpService.getMessageService().send(message); |
| | | // } |
| | | // // 发送文件 |
| | | // if (imageFile != null) { |
| | | // // 获取文件id |
| | | // WxMediaUploadResult imgResult = wxCpService.getMediaService().upload("file", imageFile); |
| | | // message = WxCpMessage.IMAGE() |
| | | // .mediaId(imgResult.getMediaId()) |
| | | // // 用户id |
| | | // .toUser(user) |
| | | // // 应用id |
| | | // .agentId(AGENT_ID) |
| | | // .build(); |
| | | // result = wxCpService.getMessageService().send(message); |
| | | // } |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws Exception |
| | | */ |
| | | public static void informWebHook(List<String> webHookList, File file) throws Exception { |
| | | JSONObject json = new JSONObject(); |
| | | json.putOpt("msgtype", "image"); |
| | | JSONObject image = new JSONObject(); |
| | | image.putOpt("base64", getBase64(file)); |
| | | image.putOpt("md5", getMd5(file)); |
| | | json.putOpt("image", image); |
| | | |
| | | webHookList.stream().forEach(webHook -> { |
| | | String result = HttpRequest.post(webHook) |
| | | .header("Content-Type", "application/json") |
| | | .body(json.toString()) |
| | | .execute().body(); |
| | | }); |
| | | return; |
| | | // JSONObject json = new JSONObject(); |
| | | // json.putOpt("msgtype", "image"); |
| | | // JSONObject image = new JSONObject(); |
| | | // image.putOpt("base64", getBase64(file)); |
| | | // image.putOpt("md5", getMd5(file)); |
| | | // json.putOpt("image", image); |
| | | // |
| | | // webHookList.stream().forEach(webHook -> { |
| | | // String result = HttpRequest.post(webHook) |
| | | // .header("Content-Type", "application/json") |
| | | // .body(json.toString()) |
| | | // .execute().body(); |
| | | // }); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws Exception |
| | | */ |
| | | public static String informWebHook(String webHook, String content) { |
| | | JSONObject jsonObject = new JSONObject() |
| | | .accumulate("msgtype", "text") |
| | | .accumulate("text", new JSONObject() |
| | | .accumulate("content", content)); |
| | | return HttpRequest.post(webHook).header("Content-Type", "application/json").body(jsonObject.toString()).execute().body(); |
| | | return ""; |
| | | // return informWebHook(webHook, content,"text"); |
| | | } |
| | | |
| | | /** |
| | | * 推送群消息 |
| | | * |
| | | * @param webHook 企业微信机器人地址 |
| | | * @param content 数据 |
| | | * @param msgType 消息类型 |
| | | * @throws Exception |
| | | */ |
| | | public static String informWebHook(String webHook, String content,String msgType) { |
| | | return ""; |
| | | // JSONObject jsonObject = new JSONObject() |
| | | // .accumulate("msgtype", msgType) |
| | | // .accumulate(msgType, new JSONObject() |
| | | // .accumulate("content", content)); |
| | | // return HttpRequest.post(webHook).header("Content-Type", "application/json").body(jsonObject.toString()).execute().body(); |
| | | } |
| | | |
| | | public static String getMd5(File file) { |
| | |
| | | return base64Str; |
| | | } |
| | | |
| | | } |
| | | } |