/src/org/sword/wechat4j/message/CustomerMsg.java

https://github.com/sword-org/wechat4j · Java · 335 lines · 149 code · 42 blank · 144 comment · 4 complexity · 796b7e4d60f7954493ad184b98518841 MD5 · raw file

  1. /**
  2. * http请求方式: POST
  3. * https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
  4. */
  5. package org.sword.wechat4j.message;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.log4j.Logger;
  10. import org.sword.lang.HttpUtils;
  11. import org.sword.wechat4j.event.MsgType;
  12. import org.sword.wechat4j.response.ArticleResponse;
  13. import org.sword.wechat4j.response.MusicResponse;
  14. import org.sword.wechat4j.response.VideoResponse;
  15. import org.sword.wechat4j.token.AccessToken;
  16. import org.sword.wechat4j.token.TokenProxy;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. /**
  20. * 发送客服消息
  21. * @author ChengNing
  22. * @date 2014年12月11日
  23. */
  24. public class CustomerMsg {
  25. private static Logger logger = Logger.getLogger(CustomerMsg.class);
  26. private static final String MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
  27. // private String accessToken;
  28. private String toUserOpenId;
  29. private String msgType; //msgtype
  30. private String msgBody; //发送的消息post数据
  31. /**
  32. * 需要主动去刷新access_token,不建议使用
  33. * 建议自己去获取access_token保存,并定时刷新。
  34. * 然后使用SendMsg(String toUserOpenId,String accessToken)来替代本方法
  35. * @param toUserOpenId
  36. */
  37. public CustomerMsg(String toUserOpenId){
  38. this.toUserOpenId = toUserOpenId;
  39. }
  40. //
  41. // public String getMsgBody() {
  42. // return msgBody;
  43. // }
  44. /**
  45. * 发送客服消息
  46. * @param msgBody
  47. */
  48. private void send(){
  49. String accessToken = TokenProxy.accessToken();
  50. if(StringUtils.isBlank(this.toUserOpenId))
  51. return;
  52. //token不存在则重新刷新token
  53. if(StringUtils.isBlank(accessToken)){
  54. logger.error("发送失败,无法得到accessToken");
  55. return;
  56. }
  57. //需要判断一下,防止上面刷新token失败
  58. if(StringUtils.isNotBlank(accessToken)){
  59. String url = MSG_URL + accessToken;
  60. HttpUtils.post(url, msgBody);
  61. }
  62. }
  63. /**
  64. * {
  65. "touser":"OPENID",
  66. "msgtype":"text",
  67. "text":
  68. {
  69. "content":"Hello World"
  70. }
  71. }
  72. * @param content
  73. */
  74. public void sendText(String content){
  75. this.msgType = MsgType.text.name();
  76. JSONObject jsonMsg = new JSONObject();
  77. jsonMsg.put("content", content);
  78. JSONObject json = new JSONObject();
  79. json.put("touser", this.toUserOpenId);
  80. json.put("msgtype", this.msgType);
  81. json.put("text", jsonMsg);
  82. this.msgBody = json.toJSONString();
  83. send();
  84. }
  85. /**
  86. * 发送图片消息
  87. * {
  88. "touser":"OPENID",
  89. "msgtype":"image",
  90. "image":
  91. {
  92. "media_id":"MEDIA_ID"
  93. }
  94. }
  95. * @param mediaId
  96. */
  97. public void sendImage(String mediaId){
  98. this.msgType = MsgType.image.name();
  99. JSONObject jsonMsg = new JSONObject();
  100. jsonMsg.put("media_id", mediaId);
  101. JSONObject json = new JSONObject();
  102. json.put("touser", this.toUserOpenId);
  103. json.put("msgtype", this.msgType);
  104. json.put("image", jsonMsg);
  105. this.msgBody = json.toJSONString();
  106. send();
  107. }
  108. /**
  109. * 发送语音消息
  110. *
  111. * {
  112. "touser":"OPENID",
  113. "msgtype":"voice",
  114. "voice":
  115. {
  116. "media_id":"MEDIA_ID"
  117. }
  118. }
  119. */
  120. public void sendVoice(String mediaId){
  121. this.msgType = MsgType.voice.name();
  122. JSONObject jsonMsg = new JSONObject();
  123. jsonMsg.put("media_id", mediaId);
  124. JSONObject json = new JSONObject();
  125. json.put("touser", this.toUserOpenId);
  126. json.put("msgtype", this.msgType);
  127. json.put("voice", jsonMsg);
  128. this.msgBody = json.toJSONString();
  129. send();
  130. }
  131. /**
  132. * 发送视频消息
  133. *
  134. *
  135. * @param title
  136. * @param description
  137. * @param mediaId
  138. * @param thumbMediaId
  139. */
  140. public void sendVideo(String title,String description,String mediaId,String thumbMediaId){
  141. VideoResponse video = new VideoResponse();
  142. video.setTitle(title);
  143. video.setDescription(description);
  144. video.setMediaId(thumbMediaId);
  145. video.setThumbMediaId(thumbMediaId);
  146. sendVideo(video);
  147. }
  148. /**
  149. * 发送视频消息
  150. * {
  151. "touser":"OPENID",
  152. "msgtype":"video",
  153. "video":
  154. {
  155. "media_id":"MEDIA_ID",
  156. "thumb_media_id":"MEDIA_ID",
  157. "title":"TITLE",
  158. "description":"DESCRIPTION"
  159. }
  160. }
  161. * @param video
  162. */
  163. public void sendVideo(VideoResponse video){
  164. this.msgType = MsgType.video.name();
  165. JSONObject jsonMsg = new JSONObject();
  166. jsonMsg.put("media_id", video.getMediaId());
  167. jsonMsg.put("thumb_media_id", video.getThumbMediaId());
  168. jsonMsg.put("title", video.getTitle());
  169. jsonMsg.put("description", video.getDescription());
  170. JSONObject json = new JSONObject();
  171. json.put("touser", this.toUserOpenId);
  172. json.put("msgtype", this.msgType);
  173. json.put("video", jsonMsg);
  174. this.msgBody = json.toJSONString();
  175. send();
  176. }
  177. /**
  178. * 发送音乐消息
  179. * @param title
  180. * @param description
  181. * @param musicURL
  182. * @param hQMusicUrl
  183. * @param thumbMediaId
  184. */
  185. public void sendMusic(String title,String description,String musicURL,String hQMusicUrl,String thumbMediaId){
  186. MusicResponse music = new MusicResponse();
  187. music.setTitle(title);
  188. music.setDescription(description);
  189. music.setMusicURL(musicURL);
  190. music.setHQMusicUrl(hQMusicUrl);
  191. music.setThumbMediaId(thumbMediaId);
  192. sendMusic(music);
  193. }
  194. /**
  195. * 发送音乐消息
  196. * {
  197. "touser":"OPENID",
  198. "msgtype":"music",
  199. "music":
  200. {
  201. "title":"MUSIC_TITLE",
  202. "description":"MUSIC_DESCRIPTION",
  203. "musicurl":"MUSIC_URL",
  204. "hqmusicurl":"HQ_MUSIC_URL",
  205. "thumb_media_id":"THUMB_MEDIA_ID"
  206. }
  207. }
  208. * @param music 音乐消息
  209. */
  210. public void sendMusic(MusicResponse music){
  211. this.msgType = MsgType.music.name();
  212. JSONObject jsonMsg = new JSONObject();
  213. jsonMsg.put("title", music.getTitle());
  214. jsonMsg.put("description", music.getDescription());
  215. jsonMsg.put("musicurl", music.getMusicURL());
  216. jsonMsg.put("hqmusicurl", music.getHQMusicUrl());
  217. jsonMsg.put("thumb_media_id", music.getThumbMediaId());
  218. JSONObject json = new JSONObject();
  219. json.put("touser", this.toUserOpenId);
  220. json.put("msgtype", this.msgType);
  221. json.put("music", jsonMsg);
  222. this.msgBody = json.toJSONString();
  223. send();
  224. }
  225. /**
  226. * 发送图文消息,单条图文消息
  227. * @param Title 图文消息标题
  228. * @param Description 图文消息描述
  229. * @param PicUrl 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
  230. * @param Url 点击图文消息跳转链接
  231. */
  232. public void sendNew(String title,String description,String picUrl,String url){
  233. ArticleResponse item = new ArticleResponse();
  234. item.setTitle(title);
  235. item.setDescription(description);
  236. item.setPicUrl(picUrl);
  237. item.setUrl(url);
  238. sendNews(item);
  239. }
  240. /**
  241. * 发送图文消息,单条图文消息
  242. * @param item
  243. */
  244. public void sendNews(ArticleResponse item){
  245. List<ArticleResponse> items = new ArrayList<ArticleResponse>();
  246. items.add(item);
  247. sendNews(items);
  248. }
  249. /**
  250. * 发送图文消息
  251. * {
  252. "touser":"OPENID",
  253. "msgtype":"news",
  254. "news":{
  255. "articles": [
  256. {
  257. "title":"Happy Day",
  258. "description":"Is Really A Happy Day",
  259. "url":"URL",
  260. "picurl":"PIC_URL"
  261. },
  262. {
  263. "title":"Happy Day",
  264. "description":"Is Really A Happy Day",
  265. "url":"URL",
  266. "picurl":"PIC_URL"
  267. }
  268. ]
  269. }
  270. }
  271. * @param items
  272. */
  273. public void sendNews(List<ArticleResponse> items){
  274. this.msgType = MsgType.news.name();
  275. JSONArray jsonArray = new JSONArray();
  276. for (ArticleResponse item : items) {
  277. JSONObject jsonItem = new JSONObject();
  278. jsonItem.put("title", item.getTitle());
  279. jsonItem.put("description", item.getDescription());
  280. jsonItem.put("url", item.getUrl());
  281. jsonItem.put("picurl", item.getPicUrl());
  282. jsonArray.add(jsonItem);
  283. }
  284. JSONObject jsonMsg = new JSONObject();
  285. jsonMsg.put("articles", jsonArray);
  286. JSONObject json = new JSONObject();
  287. json.put("touser", this.toUserOpenId);
  288. json.put("msgtype", this.msgType);
  289. json.put("news", jsonMsg);
  290. this.msgBody = json.toJSONString();
  291. send();
  292. }
  293. }