当前位置: 移动技术网 > IT编程>开发语言>Java > Java微信公众平台开发(5) 文本及图文消息回复的实现

Java微信公众平台开发(5) 文本及图文消息回复的实现

2019年07月22日  | 移动技术网IT编程  | 我要评论

上篇我们说到回复消息可以根据是否需要上传文件到微信服务器可划分为【普通消息】和【多媒体消息】,这里我们来讲述普通消息的回复实现,在消息回复中存在一个关键字段【openid】,它是微信用户对于公众号的唯一标识,这里不做过多解释后面将给出时间专门来讲解微信生态中的关键字!

(一)回复文本消息

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

首先我们在工具类messageutil的代码做出部分修改和添加,实现最后版本为:

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: 解析微信发来的请求(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,在消息分类为【文本消息】中加入如下代码:

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);
}

启动项目,当我们发送任何文本消息后我们可以看到我们的回复内容,如图:

(二)图文消息回复

图文消息的回复和文本消息的实现模式是一样的,只不过对应消息体的字段有所区别而已,这里为了和文本消息能有所区分我在【图片消息】实现图文消息的回复,修改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);  //这里发送的是单图文,如果需要发送多图文则在这里list中加入多个article即可!
 newmsg.setarticlecount(list.size());
 newmsg.setarticles(list);
 return messageutil.newsmessagetoxml(newmsg);
}  

实现结果如下图所示:

在整个的谱图消息发送的过程中没有任何项目结构的变化,只是对文件内容作了简单代码增加和修改,下一篇将讲述【微信开发中的token生成】以方便后面多媒体消息发送的讲解,感谢你的查阅,如有疑问获需源码可留言!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网