6.2.2 方法封装
发送客服消息需要POST特定格式的JSON数据包,因此,有必要为每种类型的消息定义一个组装JSON数据包的方法,代码如下:
- /**
- * 组装文本客服消息
- *
- * @param openId 消息发送对象
- * @param content 文本消息内容
- * @return
- */
- public static String makeTextCustomMessage(String openId, String content) {
- // 对消息内容中的双引号进行转义
- content = content.replace("\"", "\\\"");
- String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"text\",
- \"text\":{\"content\":\"%s\"}}";
- return String.format(jsonMsg, openId, content);
- }
- /**
- * 组装图片客服消息
- *
- * @param openId 消息发送对象
- * @param mediaId 媒体文件ID
- * @return
- */
- public static String makeImageCustomMessage(String openId, String mediaId) {
- String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"image\",
- \"image\ ":{\"media_id\":\"%s\"}}";
- return String.format(jsonMsg, openId, mediaId);
- }
- /**
- * 组装语音客服消息
- *
- * @param openId 消息发送对象
- * @param mediaId 媒体文件ID
- * @return
- */
- public static String makeVoiceCustomMessage(String openId, String mediaId) {
- String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"voice\",
- \"voice\ ":{\"media_id\":\"%s\"}}";
- return String.format(jsonMsg, openId, mediaId);
- }
- /**
- * 组装视频客服消息
- *
- * @param openId 消息发送对象
- * @param mediaId 媒体文件ID
- * @param thumbMediaId 视频消息缩略图的媒体ID
- * @return
- */
- public static String makeVideoCustomMessage(String openId, String mediaId, String
- thumbMediaId) {
- String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"video\",\"video\":
- {\"media_id\":\"%s\",\"thumb_media_id\":\"%s\"}}";
- return String.format(jsonMsg, openId, mediaId, thumbMediaId);
- }
- /**
- * 组装音乐客服消息
- *
- * @param openId 消息发送对象
- * @param music 音乐对象
- * @return
- */
- public static String makeMusicCustomMessage(String openId, Music music) {
- String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"music\",\"music\":%s}";
- jsonMsg = String.format(jsonMsg, openId, JSONObject.fromObject(music).toString());
- // 将jsonMsg中的thumbmediaid替换为thumb_media_id
- jsonMsg = jsonMsg.replace("thumbmediaid", "thumb_media_id");
- return jsonMsg;
- }
- /**
- * 组装图文客服消息
- *
- * @param openId 消息发送对象
- * @param articleList 图文消息列表
- * @return
- */
- public static String makeNewsCustomMessage(String openId, List<Article>
- articleList) {
- String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"news\",
- \"news\":{\" articles\":%s}}";
- jsonMsg = String.format(jsonMsg, openId, JSONArray.fromObject(articleList).
- toString().replaceAll("\"", "\\\""));
- // 将jsonMsg中的picUrl替换为picurl
- jsonMsg = jsonMsg.replace("picUrl", "picurl");
- return jsonMsg;
- }
调用上面的方法就能够得到发送客服消息所需的JSON数据包。代码中的JSONObject和JSONArray都是JSON-lib工具中的类,我们在第5章学习过它的用法;代码中的Music、Article是我们在4.2.3节中定义的类。
笔者将发送客服消息的操作封装成sendCustomMessage()方法,该方法的实现如下:
- /**
- * 发送客服消息
- *
- * @param accessToken 接口访问凭证
- * @param jsonMsg json格式的客服消息(包括touser、msgtype和消息内容)
- * @return true | false
- */
- public static boolean sendCustomMessage(String accessToken, String jsonMsg) {
- log.info("消息内容:{}", jsonMsg);
- boolean result = false;
- // 拼接请求地址
- String requestUrl = "https:// api.weixin.qq.com/cgi-bin/message/
- custom/send?access_token=ACCESS_TOKEN";
- requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
- // 发送客服消息
- JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonMsg);
- if (null != jsonObject) {
- int errorCode = jsonObject.getInt("errcode");
- String errorMsg = jsonObject.getString("errmsg");
- if (0 == errorCode) {
- result = true;
- log.info("客服消息发送成功 errcode:{} errmsg:{}", errorCode, errorMsg);
- } else {
- log.error("客服消息发送失败 errcode:{} errmsg:{}", errorCode, errorMsg);
- }
- }
- return result;
- }
通过sendCustomMessage()方法就能向指定用户发送客服消息,调用该方法需要传入参数accessToken和jsonMsg,分别表示接口访问凭证和JSON数据包。sendCustomMessage()方法中的CommonUtil类是我们在5.6.3节中封装的通用工具类,该类中的httpsRequest()和getToken()方法分别用于发送HTTPS请求和获取接口访问凭证,本章后面的接口调用还会用到这两个方法。