微信公众平台开发--文本及图文消息回复的实现

 根据是否需要上传文件到微信服务器分为【普通消息】和【多媒体消息】;而普通消息的回复实现,在消息回复中存在一个关键字段【openid】,它是微信用户对于公众号的唯一标识。

(一)回复文本消息

在前面已完成了对消息的分类和回复消息实体的建立,这里回复文本消息需要用到的是TextMessage,我们把回复文本消息在【文本消息】类型中给出回复!在我们做消息回复的时候需要设置消息的接收人ToUserNameopenid)、消息的发送方 FromUserName、消息类型 MsgType、创建时间 CreateTime以及消息体 Content,由于我们我们的消息回复格式是需要为 xml,所以最终我们需要将其装换成 xml 再做返回输出!

工具类MessageUtil :

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
* @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: 视频消息对象转换成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: 音乐消息对象转换成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节点的转换都增加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);
}
}
};
}
});
}

我们回复文本消息的简单实现:修改MsgDispatcher,在消息分类为【文本消息】中加入如下代码:


测试结果如下:


(二)图文消息回复

图文消息的回复和文本消息的实现模式是一样的,只不过对应消息体的字段有所区别,这里为了和文本消息能有所区分我在【图片消息】实现图文消息的回复,修改MsgDispatcher




  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值