6.2.2 方法封装

发送客服消息需要POST特定格式的JSON数据包,因此,有必要为每种类型的消息定义一个组装JSON数据包的方法,代码如下:

  1. /**
  2. * 组装文本客服消息
  3. *
  4. * @param openId 消息发送对象
  5. * @param content 文本消息内容
  6. * @return
  7. */
  8. public static String makeTextCustomMessage(String openId, String content) {
  9. // 对消息内容中的双引号进行转义
  10. content = content.replace("\"", "\\\"");
  11. String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"text\",
  12. \"text\":{\"content\":\"%s\"}}";
  13. return String.format(jsonMsg, openId, content);
  14. }
  15.  
  16. /**
  17. * 组装图片客服消息
  18. *
  19. * @param openId 消息发送对象
  20. * @param mediaId 媒体文件ID
  21. * @return
  22. */
  23. public static String makeImageCustomMessage(String openId, String mediaId) {
  24. String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"image\",
  25. \"image\ ":{\"media_id\":\"%s\"}}";
  26. return String.format(jsonMsg, openId, mediaId);
  27. }
  28.  
  29. /**
  30. * 组装语音客服消息
  31. *
  32. * @param openId 消息发送对象
  33. * @param mediaId 媒体文件ID
  34. * @return
  35. */
  36. public static String makeVoiceCustomMessage(String openId, String mediaId) {
  37. String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"voice\",
  38. \"voice\ ":{\"media_id\":\"%s\"}}";
  39. return String.format(jsonMsg, openId, mediaId);
  40. }
  41.  
  42. /**
  43. * 组装视频客服消息
  44. *
  45. * @param openId 消息发送对象
  46. * @param mediaId 媒体文件ID
  47. * @param thumbMediaId 视频消息缩略图的媒体ID
  48. * @return
  49. */
  50. public static String makeVideoCustomMessage(String openId, String mediaId, String
  51. thumbMediaId) {
  52. String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"video\",\"video\":
  53. {\"media_id\":\"%s\",\"thumb_media_id\":\"%s\"}}";
  54. return String.format(jsonMsg, openId, mediaId, thumbMediaId);
  55. }
  56.  
  57. /**
  58. * 组装音乐客服消息
  59. *
  60. * @param openId 消息发送对象
  61. * @param music 音乐对象
  62. * @return
  63. */
  64. public static String makeMusicCustomMessage(String openId, Music music) {
  65. String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"music\",\"music\":%s}";
  66. jsonMsg = String.format(jsonMsg, openId, JSONObject.fromObject(music).toString());
  67. // 将jsonMsg中的thumbmediaid替换为thumb_media_id
  68. jsonMsg = jsonMsg.replace("thumbmediaid", "thumb_media_id");
  69. return jsonMsg;
  70. }
  71.  
  72. /**
  73. * 组装图文客服消息
  74. *
  75. * @param openId 消息发送对象
  76. * @param articleList 图文消息列表
  77. * @return
  78. */
  79. public static String makeNewsCustomMessage(String openId, List<Article>
  80. articleList) {
  81. String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"news\",
  82. \"news\":{\" articles\":%s}}";
  83. jsonMsg = String.format(jsonMsg, openId, JSONArray.fromObject(articleList).
  84.               toString().replaceAll("\"", "\\\""));
  85. // 将jsonMsg中的picUrl替换为picurl
  86. jsonMsg = jsonMsg.replace("picUrl", "picurl");
  87. return jsonMsg;
  88. }

调用上面的方法就能够得到发送客服消息所需的JSON数据包。代码中的JSONObject和JSONArray都是JSON-lib工具中的类,我们在第5章学习过它的用法;代码中的Music、Article是我们在4.2.3节中定义的类。

笔者将发送客服消息的操作封装成sendCustomMessage()方法,该方法的实现如下:

  1. /**
  2. * 发送客服消息
  3. *
  4. * @param accessToken 接口访问凭证
  5. * @param jsonMsg json格式的客服消息(包括touser、msgtype和消息内容)
  6. * @return true | false
  7. */
  8. public static boolean sendCustomMessage(String accessToken, String jsonMsg) {
  9. log.info("消息内容:{}", jsonMsg);
  10. boolean result = false;
  11. // 拼接请求地址
  12. String requestUrl = "https:// api.weixin.qq.com/cgi-bin/message/
  13. custom/send?access_token=ACCESS_TOKEN";
  14. requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
  15. // 发送客服消息
  16. JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonMsg);
  17.  
  18. if (null != jsonObject) {
  19. int errorCode = jsonObject.getInt("errcode");
  20. String errorMsg = jsonObject.getString("errmsg");
  21. if (0 == errorCode) {
  22. result = true;
  23. log.info("客服消息发送成功 errcode:{} errmsg:{}", errorCode, errorMsg);
  24. } else {
  25. log.error("客服消息发送失败 errcode:{} errmsg:{}", errorCode, errorMsg);
  26. }
  27. }
  28.  
  29. return result;
  30. }

通过sendCustomMessage()方法就能向指定用户发送客服消息,调用该方法需要传入参数accessToken和jsonMsg,分别表示接口访问凭证和JSON数据包。sendCustomMessage()方法中的CommonUtil类是我们在5.6.3节中封装的通用工具类,该类中的httpsRequest()和getToken()方法分别用于发送HTTPS请求和获取接口访问凭证,本章后面的接口调用还会用到这两个方法。