1. 程式人生 > >Java微信公眾平臺開發(五)--文字及圖文訊息回覆的實現

Java微信公眾平臺開發(五)--文字及圖文訊息回覆的實現

轉自:http://www.cuiyongzhi.com/post/43.html

上篇我們說到回覆訊息可以根據是否需要上傳檔案到微信伺服器可劃分為【普通訊息】和【多媒體訊息】,這裡我們來講述普通訊息的回覆實現,在訊息回覆中存在一個關鍵欄位【openid】,它是微信使用者對於公眾號的唯一標識,這裡不做過多解釋後面將給出時間專門來講解微信生態中的關鍵字!

(一)回覆文字訊息

在前面我們已經完成了對訊息的分類和回覆訊息實體的建立,這裡回覆文字訊息需要用到的就是我們的TextMessage,我們把回覆文字訊息在【文字訊息】型別中給出回覆!在我們做訊息回覆的時候需要設定訊息的接收人ToUserName(openid)、訊息的傳送方FromUserName、訊息型別MsgType、建立時間CreateTime以及訊息體Content,由於我們我們的訊息回覆格式是需要為xml,所以最終我們需要將其裝換成xml再做返回輸出!

首先我們在工具類MessageUtil的程式碼做出部分修改和新增,實現最後版本為:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 package  com.cuiyongzhi.wechat.util;   import  java.io.InputStream; import  java.io.Writer; import  java.util.HashMap;
import  java.util.List; import  java.util.Map;   import  javax.servlet.http.HttpServletRequest;   import  org.dom4j.Document; import  org.dom4j.Element; import  org.dom4j.io.SAXReader;   import  com.cuiyongzhi.wechat.message.resp.Article; import  com.cuiyongzhi.wechat.message.resp.ImageMessage; import  com.cuiyongzhi.wechat.message.resp.MusicMessage; import  com.cuiyongzhi.wechat.message.resp.NewsMessage; import  com.cuiyongzhi.wechat.message.resp.TextMessage; import  com.cuiyongzhi.wechat.message.resp.VideoMessage; import  com.cuiyongzhi.wechat.message.resp.VoiceMessage; import  com.thoughtworks.xstream.XStream; import  com.thoughtworks.xstream.core.util.QuickWriter; import  com.thoughtworks.xstream.io.HierarchicalStreamWriter; import  com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import  com.thoughtworks.xstream.io.xml.XppDriver;   /**   * ClassName: MessageUtil     * @Description: 訊息工具類   * @author dapengniao   * @date 2016年3月7日 上午10:05:04   */ public  class  MessageUtil {        /**       * 返回訊息型別:文字       */      public  static  final  String RESP_MESSAGE_TYPE_TEXT =  "text" ;        /**       * 返回訊息型別:音樂       */      public  static  final  String RESP_MESSAGE_TYPE_MUSIC =  "music" ;        /**       * 返回訊息型別:圖文       */      public  static  final  String RESP_MESSAGE_TYPE_NEWS =  "news" ;        /**       * 返回訊息型別:圖片       */      public  static  final  String RESP_MESSAGE_TYPE_Image =  "image" ;        /**       * 返回訊息型別:語音       */      public  static  final  String RESP_MESSAGE_TYPE_Voice =  "voice" ;        /**       * 返回訊息型別:視訊       */      public  static  final  String RESP_MESSAGE_TYPE_Video =  "video" ;        /**       * 請求訊息型別:文字       */      public  static  final  String REQ_MESSAGE_TYPE_TEXT =  "text" ;        /**       * 請求訊息型別:圖片       */      public  static  final  String REQ_MESSAGE_TYPE_IMAGE =  "image" ;        /**       * 請求訊息型別:連結       */      public  static  final  String REQ_MESSAGE_TYPE_LINK =  "link" ;        /**       * 請求訊息型別:地理位置       */      public  static  final  String REQ_MESSAGE_TYPE_LOCATION =  "location" ;        /**       * 請求訊息型別:音訊       */      public  static  final  String REQ_MESSAGE_TYPE_VOICE =  "voice" ;        /**       * 請求訊息型別:視訊       */      public  static  final  String REQ_MESSAGE_TYPE_VIDEO =  "video" ;        /**       * 請求訊息型別:推送       */      public  static  final  String REQ_MESSAGE_TYPE_EVENT =  "event" ;        /**       * 事件型別:subscribe(訂閱)       */      public  static  final  String EVENT_TYPE_SUBSCRIBE =  "subscribe" ;        /**       * 事件型別:unsubscribe(取消訂閱)       */      public  static  final  String EVENT_TYPE_UNSUBSCRIBE =  "unsubscribe" ;        /**       * 事件型別:CLICK(自定義選單點選事件)       */      public  static  final  String EVENT_TYPE_CLICK =  "CLICK" ;        /**       * 事件型別:VIEW(自定義選單URl檢視)       */      public  static  final  String EVENT_TYPE_VIEW =  "VIEW" ;        /**       * 事件型別:LOCATION(上報地理位置事件)       */      public  static  final  String EVENT_TYPE_LOCATION =  "LOCATION" ;        /**       * 事件型別:LOCATION(上報地理位置事件)       */      public  static  final  String EVENT_TYPE_SCAN =  "SCAN" ;        /**       * @Description: 解析微信發來的請求(XML)       * @param @param request       * @param @return       * @param @throws Exception       * @author dapengniao       * @date 2016年3月7日 上午10:04:02       */      @SuppressWarnings ( "unchecked" )      public  static  Map<String, String> parseXml(HttpServletRequest request)              throws  Exception {          // 將解析結果儲存在HashMap中          Map<String, String> map =  new  HashMap<String, String>();          // 從request中取得輸入流          InputStream inputStream = request.getInputStream();          // 讀取輸入流          SAXReader reader =  new  SAXReader();          Document document = reader.read(inputStream);          // 得到xml根元素          Element root = document.getRootElement();          // 得到根元素的所有子節點          List<Element> elementList = root.elements();            // 遍歷所有子節點          for  (Element e : elementList)              map.put(e.getName(), e.getText());            // 釋放資源          inputStream.close();          inputStream =  null ;            return  map;      }        /**       * @Description: 文字訊息物件轉換成xml       * @param @param textMessage       * @param @return       * @author dapengniao       * @date 2016年3月8日 下午4:13:22       */      public  static  String textMessageToXml(TextMessage textMessage) {          xstream.alias( "xml" , textMessage.getClass());          return  xstream.toXML(textMessage);      }        /**       * @Description: 圖文訊息物件轉換成xml       * @param @param newsMessage       * @param @return       * @author dapengniao       * @date 2016年3月8日 下午4:14:09       */      public  static  String newsMessageToXml(NewsMessage newsMessage) {          xstream.alias( "xml" , newsMessage.getClass());          xstream.alias( "item" new  Article().getClass());          return  xstream.toXML(newsMessage);      }        /**       * @Description: 圖片訊息物件轉換成xml       * @param @param imageMessage       * @param @return       * @author dapengniao       * @date 2016年3月9日 上午9:25:51       */      public  static  String imageMessageToXml(ImageMessage imageMessage) {          xstream.alias( "xml" , imageMessage.getClass());          return  xstream.toXML(imageMessage);      }        /**       * @Description: 語音訊息物件轉換成xml