Java微信公眾平臺開發(fā)(5) 文本及圖文消息回復(fù)的實現(xiàn)
上篇我們說到回復(fù)消息可以根據(jù)是否需要上傳文件到微信服務(wù)器可劃分為【普通消息】和【多媒體消息】,這里我們來講述普通消息的回復(fù)實現(xiàn),在消息回復(fù)中存在一個關(guān)鍵字段【openid】,它是微信用戶對于公眾號的唯一標識,這里不做過多解釋后面將給出時間專門來講解微信生態(tài)中的關(guān)鍵字!
(一)回復(fù)文本消息
在前面我們已經(jīng)完成了對消息的分類和回復(fù)消息實體的建立,這里回復(fù)文本消息需要用到的就是我們的TextMessage,我們把回復(fù)文本消息在【文本消息】類型中給出回復(fù)!在我們做消息回復(fù)的時候需要設(shè)置消息的接收人ToUserName(openid)、消息的發(fā)送方FromUserName、消息類型MsgType、創(chuàng)建時間CreateTime以及消息體Content,由于我們我們的消息回復(fù)格式是需要為xml,所以最終我們需要將其裝換成xml再做返回輸出!
首先我們在工具類MessageUtil的代碼做出部分修改和添加,實現(xiàn)最后版本為:
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.D ocument;
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: 解析微信發(fā)來的請求(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 {
// 將解析結(jié)果存儲在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();
// 得到根元素的所有子節(jié)點
List<Element> elementList = root.elements();
// 遍歷所有子節(jié)點
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 釋放資源
inputStream.close();
inputStream = null;
return map;
}
/**
* @Description: 文本消息對象轉(zhuǎn)換成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: 圖文消息對象轉(zhuǎn)換成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: 圖片消息對象轉(zhuǎn)換成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: 語音消息對象轉(zhuǎn)換成xml
* @param @param voiceMessage
* @param @return
* @author dapengniao
* @date 2016年3月9日 上午9:27:26
*/
public static String voiceMessageToXml(VoiceMessage voiceMessage) {
xstream.alias("xml", voiceMessage.getClass());
return xstream.toXML(voiceMessage);
}
/**
* @Description: 視頻消息對象轉(zhuǎn)換成xml
* @param @param videoMessage
* @param @return
* @author dapengniao
* @date 2016年3月9日 上午9:31:09
*/
public static String videoMessageToXml(VideoMessage videoMessage) {
xstream.alias("xml", videoMessage.getClass());
return xstream.toXML(videoMessage);
}
/**
* @Description: 音樂消息對象轉(zhuǎn)換成xml
* @param @param musicMessage
* @param @return
* @author dapengniao
* @date 2016年3月8日 下午4:13:36
*/
public static String musicMessageToXml(MusicMessage musicMessage) {
xstream.alias("xml", musicMessage.getClass());
return xstream.toXML(musicMessage);
}
/**
* 對象到xml的處理
*/
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 對所有xml節(jié)點的轉(zhuǎn)換都增加CDATA標記
boolean cdata = true;
@SuppressWarnings("rawtypes")
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
}
我們回復(fù)文本消息的簡單實現(xiàn):修改MsgDispatcher,在消息分類為【文本消息】中加入如下代碼:
String openid=map.get("FromUserName"); //用戶openid
String mpid=map.get("ToUserName"); //公眾號原始ID
//普通文本消息
TextMessage txtmsg=new TextMessage();
txtmsg.setToUserName(openid);
txtmsg.setFromUserName(mpid);
txtmsg.setCreateTime(new Date().getTime());
txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { // 文本消息
txtmsg.setContent("你好,這里是崔用志個人賬號!");
return MessageUtil.textMessageToXml(txtmsg);
}
啟動項目,當我們發(fā)送任何文本消息后我們可以看到我們的回復(fù)內(nèi)容,如圖:

(二)圖文消息回復(fù)
圖文消息的回復(fù)和文本消息的實現(xiàn)模式是一樣的,只不過對應(yīng)消息體的字段有所區(qū)別而已,這里為了和文本消息能有所區(qū)分我在【圖片消息】實現(xiàn)圖文消息的回復(fù),修改MsgDispatcher:
//對圖文消息
NewsMessage newmsg=new NewsMessage();
newmsg.setToUserName(openid);
newmsg.setFromUserName(mpid);
newmsg.setCreateTime(new Date().getTime());
newmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) { // 圖片消息
System.out.println("==============這是圖片消息!");
Article article=new Article();
article.setDescription("這是圖文消息1"); //圖文消息的描述
article.setPicUrl("http://res.cuiyongzhi.com/2016/03/201603086749_6850.png"); //圖文消息圖片地址
article.setTitle("圖文消息1"); //圖文消息標題
article.setUrl("http://www.cuiyongzhi.com"); //圖文url鏈接
List<Article> list=new ArrayList<Article>();
list.add(article); //這里發(fā)送的是單圖文,如果需要發(fā)送多圖文則在這里list中加入多個Article即可!
newmsg.setArticleCount(list.size());
newmsg.setArticles(list);
return MessageUtil.newsMessageToXml(newmsg);
}
實現(xiàn)結(jié)果如下圖所示:

在整個的譜圖消息發(fā)送的過程中沒有任何項目結(jié)構(gòu)的變化,只是對文件內(nèi)容作了簡單代碼增加和修改,下一篇將講述【微信開發(fā)中的token生成】以方便后面多媒體消息發(fā)送的講解,感謝你的查閱,如有疑問獲需源碼可留言!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java微信公眾平臺開發(fā)(15) 微信JSSDK的使用
- Java微信公眾平臺開發(fā)(14) 微信web開發(fā)者工具使用
- Java微信公眾平臺開發(fā)(12) 微信用戶信息的獲取
- Java微信公眾平臺開發(fā)(10) 微信自定義菜單的創(chuàng)建實現(xiàn)
- Java微信公眾平臺開發(fā)(9) 關(guān)鍵字回復(fù)以及客服接口實現(xiàn)
- Java微信公眾平臺開發(fā)(8) 多媒體消息回復(fù)
- Java微信公眾平臺開發(fā)(7) 公眾平臺測試帳號的申請
- Java微信公眾平臺之素材管理
- Java微信公眾平臺之獲取地理位置
- Java微信公眾平臺之群發(fā)接口(高級群發(fā))
相關(guān)文章
Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用
這篇文章主要介紹了Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用,文中有非常詳細的圖文示例及代碼,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05

