企业微信应用PUSH @author itning @since 2021/9/22 14:41
| 26 | * @since 2021/9/22 14:41 |
| 27 | */ |
| 28 | @Slf4j |
| 29 | public class WeComAppPush extends AbstractPush { |
| 30 | |
| 31 | private static final int WE_COM_APP_MESSAGE_MAX_LENGTH = 1000; |
| 32 | |
| 33 | @Override |
| 34 | protected String generatePushUrl(PushMetaInfo metaInfo) { |
| 35 | |
| 36 | JsonObject jsonObject = HttpUtils.doGet(ApiList.WECOM_APP_PUSH_GET_TOKEN + "?corpid=" + metaInfo.getToken() + "&corpsecret=" + metaInfo.getSecret(), new JsonObject(), requestConfig); |
| 37 | if (null == jsonObject) { |
| 38 | throw new RuntimeException("获取企业微信凭证失败,JsonObject Is Null"); |
| 39 | } |
| 40 | JsonElement element = jsonObject.get("access_token"); |
| 41 | if (null == element) { |
| 42 | throw new RuntimeException("获取企业微信凭证失败,access_token Is Null"); |
| 43 | } |
| 44 | String accessToken = element.getAsString(); |
| 45 | if (StringUtils.isBlank(accessToken)) { |
| 46 | throw new RuntimeException("获取企业微信凭证失败,access_token Is Blank"); |
| 47 | } |
| 48 | |
| 49 | return ApiList.WECOM_APP_PUSH + "?access_token=" + accessToken; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected String generatePushBody(PushMetaInfo metaInfo, String content) { |
| 54 | content = content.replaceAll("\r","").replaceAll("\n\n","\n"); |
| 55 | WeComMessageSendRequest request = new WeComMessageSendRequest(); |
| 56 | request.setToUser(metaInfo.getToUser()); |
| 57 | request.setAgentId(metaInfo.getAgentId()); |
| 58 | //System.out.println(StringUtils.isBlank(metaInfo.getMediaid())); |
| 59 | if (StringUtils.isBlank(metaInfo.getMediaid())) { |
| 60 | request.setMsgType("text"); |
| 61 | WeComMessageSendRequest.Text text = new WeComMessageSendRequest.Text(); |
| 62 | text.setContent(content); |
| 63 | request.setText(text); |
| 64 | } else { |
| 65 | request.setMsgType("mpnews"); |
| 66 | WeComMessageSendRequest.Articles Articles = new WeComMessageSendRequest.Articles(); |
| 67 | Articles.setAuthor("海尔破小助手"); |
| 68 | Articles.setTitle("BILIBILI-HELPER任务简报"); |
| 69 | Articles.setDigest(content); |
| 70 | Articles.setContent(content.replaceAll("\n","<br>")); |
| 71 | Articles.setThumb_media_id(metaInfo.getMediaid()); |
| 72 | WeComMessageSendRequest.Mpnews Mpnews = new WeComMessageSendRequest.Mpnews(); |
| 73 | Mpnews.setArticles(Collections.singletonList(Articles)); |
| 74 | request.setMpnews(Mpnews); |
| 75 | } |
| 76 | return GsonUtils.toJson(request); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected List<String> segmentation(PushMetaInfo metaInfo, String pushBody) { |
| 81 | if (StringUtils.isBlank(pushBody)) { |
| 82 | return Collections.emptyList(); |
| 83 | } |
| 84 | if (pushBody.length() > WE_COM_APP_MESSAGE_MAX_LENGTH && StringUtils.isBlank(metaInfo.getMediaid())) { |
| 85 | log.info("推送内容长度[{}]大于最大长度[{}]进行分割处理", pushBody.length(), WE_COM_APP_MESSAGE_MAX_LENGTH); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…