/** * 获取所有客服帐号信息 * * @return 所有客服帐号信息对象 */ public GetCustomAccountsResponse getCustomAccountList() { LOG.debug("获取所有客服帐号信息...."); GetCustomAccountsResponse response; String url = BASE_API_URL + "cgi-bin/customservice/getkflist?access_token=#"; BaseResponse r = executeGet(url); String resultJson = isSuccess(r.getErrcode()) ? r.getErrmsg() : r.toJsonString(); response = JSONUtil.toBean(resultJson, GetCustomAccountsResponse.class); return response; }
/** * 上传媒体文件 * * @param file 媒体文件 * @param type 媒体文件类型 * @return 上传结果 */ public UploadMediaResponse upload(MediaType type, File file) { if (type == MediaType.NEWS) { LOG.debug("企业号媒体素材不包含新闻列表"); return null; } UploadMediaResponse response; String url = BASE_API_URL + "cgi-bin/media/upload?access_token=#&type=" + type.toString(); BaseResponse r = executePost(url, null, file); String resultJson = isSuccess(r.getErrcode()) ? r.getErrmsg() : r.toJsonString(); response = JSONUtil.toBean(resultJson, UploadMediaResponse.class); return response; }
/** * 修改客服帐号信息 * * @param customAccount 客服帐号信息 * @return 修改结果 */ public ResultType updateCustomAccount(CustomAccount customAccount) { LOG.debug("修改客服帐号信息......"); BeanUtil.requireNonNull(customAccount.getAccountName(), "帐号必填"); BeanUtil.requireNonNull(customAccount.getNickName(), "昵称必填"); String url = BASE_API_URL + "customservice/kfaccount/update?access_token=#"; Map<String, String> params = new HashMap<String, String>(); params.put("kf_account", customAccount.getAccountName()); params.put("nickname", customAccount.getNickName()); if (StrUtil.isNotBlank(customAccount.getPassword())) { params.put("password", customAccount.getPassword()); } BaseResponse response = executePost(url, JSONUtil.toJson(params)); return ResultType.get(response.getErrcode()); }
/** * 下载媒体文件 * * @param mediaId 媒体ID * @return 下载结果 */ public DownloadMediaResponse download(String mediaId) { DownloadMediaResponse response = new DownloadMediaResponse(); String url = BASE_API_URL + "cgi-bin/media/get?access_token=" + config.getAccessToken() + "&media_id=" + mediaId; RequestConfig config = RequestConfig.custom() .setConnectionRequestTimeout(NetWorkCenter.CONNECT_TIMEOUT) .setConnectTimeout(NetWorkCenter.CONNECT_TIMEOUT) .setSocketTimeout(NetWorkCenter.CONNECT_TIMEOUT) .build(); CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); HttpGet get = new HttpGet(url); try { CloseableHttpResponse r = client.execute(get); if (HttpStatus.SC_OK == r.getStatusLine().getStatusCode()) { InputStream inputStream = r.getEntity().getContent(); Header[] headers = r.getHeaders("Content-disposition"); if (null != headers && 0 != headers.length) { Header length = r.getHeaders("Content-Length")[0]; response.setContent(inputStream, Integer.valueOf(length.getValue())); response.setFileName( headers[0].getElements()[0].getParameterByName("filename").getValue()); } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamUtil.copy(inputStream, out); String json = out.toString(); response = JSONUtil.toBean(json, DownloadMediaResponse.class); } } } catch (Exception e) { LOG.error("异常", e); } finally { try { client.close(); } catch (IOException e) { LOG.error("异常", e); } } return response; }
/** * 发布客服消息 * * @param openid 关注者ID * @param message 消息对象,支持各种消息类型 * @return 调用结果 */ public ResultType sendCustomMessage(String openid, BaseMsg message) { BeanUtil.requireNonNull(openid, "openid is null"); BeanUtil.requireNonNull(message, "message is null"); LOG.debug("发布客服消息......"); String url = BASE_API_URL + "cgi-bin/message/custom/send?access_token=#"; final Map<String, Object> params = new HashMap<String, Object>(); params.put("touser", openid); if (message instanceof TextMsg) { TextMsg msg = (TextMsg) message; params.put("msgtype", "text"); Map<String, String> text = new HashMap<String, String>(); text.put("content", msg.getContent()); params.put("text", text); } else if (message instanceof ImageMsg) { ImageMsg msg = (ImageMsg) message; params.put("msgtype", "image"); Map<String, String> image = new HashMap<String, String>(); image.put("media_id", msg.getMediaId()); params.put("image", image); } else if (message instanceof VoiceMsg) { VoiceMsg msg = (VoiceMsg) message; params.put("msgtype", "voice"); Map<String, String> voice = new HashMap<String, String>(); voice.put("media_id", msg.getMediaId()); params.put("voice", voice); } else if (message instanceof VideoMsg) { VideoMsg msg = (VideoMsg) message; params.put("msgtype", "video"); Map<String, String> video = new HashMap<String, String>(); video.put("media_id", msg.getMediaId()); video.put("thumb_media_id", msg.getMediaId()); video.put("title", msg.getTitle()); video.put("description", msg.getDescription()); params.put("video", video); } else if (message instanceof MusicMsg) { MusicMsg msg = (MusicMsg) message; params.put("msgtype", "music"); Map<String, String> music = new HashMap<String, String>(); music.put("thumb_media_id", msg.getThumbMediaId()); music.put("title", msg.getTitle()); music.put("description", msg.getDescription()); music.put("musicurl", msg.getMusicUrl()); music.put("hqmusicurl", msg.getHqMusicUrl()); params.put("music", music); } else if (message instanceof NewsMsg) { NewsMsg msg = (NewsMsg) message; params.put("msgtype", "news"); Map<String, Object> news = new HashMap<String, Object>(); List<Object> articles = new ArrayList<Object>(); List<Article> list = msg.getArticles(); for (Article article : list) { Map<String, String> map = new HashMap<String, String>(); map.put("title", article.getTitle()); map.put("description", article.getDescription()); map.put("url", article.getUrl()); map.put("picurl", article.getPicUrl()); articles.add(map); } news.put("articles", articles); params.put("news", news); } else if (message instanceof MpNewsMsg) { MpNewsMsg msg = (MpNewsMsg) message; params.put("msgtype", "mpnews"); Map<String, String> news = new HashMap<String, String>(); news.put("media_id", msg.getMediaId()); params.put("mpnews", news); } BaseResponse response = executePost(url, JSONUtil.toJson(params)); return ResultType.get(response.getErrcode()); }