微信开发(三)消息的自动回复和关注后自动回复
1.前置与微信接口对接成功后。
3.创建一个实体类消息类。里面设置的变量和属性参照微信开发者文档,https://mp.weixin.qq.com/wiki。
2.修改Servlet中dopost方法。
3.在消息操作类中增加方法,添加主菜单的方法,拼接字符串的方法,回复弹出信息的方法(1-5)。
参考代码:消息操作类中的代码
1 package com.util; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.util.Date; 5 import java.util.HashMap; 6 import java.util.Map; 7 8 9 import javax.servlet.http.HttpServletRequest; 10 import org.dom4j.DocumentException; 11 import org.dom4j.Element; 12 import org.dom4j.io.SAXReader; 13 14 import com.po.TextMessage; 15 import com.thoughtworks.xstream.XStream; 16 17 /* 18 * 创建一个方法 进行xml->map集合的转换 19 * 20 */ 21 public class MessagesUitl { 22 //消息类型的常量 23 public static final String MESSAGE_TEXT="text"; 24 public static final String MESSAGE_IMAGE="image"; 25 public static final String MESSAGE_VOICE="voice"; 26 public static final String MESSAGE_VIDEO="video"; 27 public static final String MESSAGE_LINK="link"; 28 public static final String MESSAGE_LOCATION="location"; 29 public static final String MESSAGE_EVENT="event"; 30 public static final String MESSAGE_SUBSCRIBE="subscribe"; 31 public static final String MESSAGE_UNSUBSCRIBE="unsubscribe"; 32 public static final String MESSAGE_CLICK="CLICK"; 33 public static final String MESSAGE_VIEW="VIEW"; 34 public static Map<String,String>xmlToMap(HttpServletRequest request) throws IOException, DocumentException{ 35 //创建一个集合 36 Map<String, String> map=new HashMap<String, String>(); 37 SAXReader reader=new SAXReader(); 38 39 InputStream ins=request.getInputStream(); 40 org.dom4j.Document doc=reader.read(ins); 41 42 Element root=doc.getRootElement(); 43 44 java.util.List<Element> list=root.elements(); 45 for (Element e:list) { 46 map.put(e.getName(), e.getText()); 47 } 48 ins.close(); 49 return map; 50 51 52 } 53 //将文本消息转化为xml文件 54 public static String textMessageToXml(TextMessage textMessage){ 55 XStream xstream=new XStream(); 56 xstream.alias("xml", textMessage.getClass()) ; 57 return xstream.toXML(textMessage); 58 } 59 /* 60 * 主菜单 61 */ 62 public static String menuText(){ 63 StringBuffer sb=new StringBuffer(); 64 sb.append(" 欢迎您的关注!\n"); 65 sb.append(" 请按照菜单提示进行操作\n\n"); 66 sb.append(" 1.账号介绍 \n"); 67 sb.append(" 2.美剧推荐 \n"); 68 sb.append(" 3.超级英雄 \n"); 69 sb.append(" 4.美剧时间表 \n"); 70 sb.append(" ?.回复?调出此菜单\n\n"); 71 sb.append(" 更多功能尚在开发中 \n"); 72 return sb.toString(); 73 } 74 /* 75 * 拼接文本消息 76 */ 77 public static String initText(String toUserName,String fromUserName,String content ){ 78 TextMessage text=new TextMessage(); 79 text.setFromUserName(toUserName); 80 text.setToUserName(fromUserName); 81 text.setMsgType(MessagesUitl.MESSAGE_TEXT); 82 text.setCreateTime(new Date().getTime()); 83 text.setContent(content); 84 return textMessageToXml(text); 85 } 86 /* 87 * 回复“1”弹出信息 88 */ 89 public static String firstMenu(){ 90 StringBuffer sb=new StringBuffer(); 91 sb.append("本账号属于测试账号,正在开发中,主要发布与美剧相关信息!"); 92 93 return sb.toString(); 94 } 95 /* 96 * 回复“2”弹出信息 97 */ 98 public static String secondMenu(){ 99 StringBuffer sb=new StringBuffer(); 100 sb.append("美剧推荐功能正在开发中!"); 101 102 return sb.toString(); 103 } 104 /* 105 * 回复“3”弹出信息 106 */ 107 public static String thirdMenu(){ 108 StringBuffer sb=new StringBuffer(); 109 sb.append("超级英雄功能正在开发中!"); 110 111 return sb.toString(); 112 } 113 /* 114 * 回复“1”弹出信息 115 */ 116 public static String fourtMenu(){ 117 StringBuffer sb=new StringBuffer(); 118 sb.append("美剧时间表功能正在开发中!"); 119 120 return sb.toString(); 121 } 122 public static String wenMenu(){ 123 StringBuffer sb=new StringBuffer(); 124 sb.append("美剧时间表功能正在开发中!"); 125 126 return sb.toString(); 127 } 128 }
参考代码:Servlet
1 package com.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Date; 6 import java.util.Map; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.dom4j.DocumentException; 14 15 import com.po.TextMessage; 16 import com.util.MessagesUitl; 17 18 import sun.misc.MessageUtils; 19 20 21 22 public class WeixinServlet extends HttpServlet{ 23 @Override 24 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 25 throws ServletException, IOException { 26 String signature=req.getParameter("signature"); 27 String timestamp=req.getParameter("timestamp"); 28 String nonce=req.getParameter("nonce"); 29 String echostr=req.getParameter("echostr"); 30 PrintWriter out=resp.getWriter(); 31 if (com.util.CheckUtil.checkSignature(signature, timestamp, nonce)) { 32 System.out.println(); 33 out.print(echostr); 34 }else{ 35 System.out.println("不匹配"); 36 } 37 } 38 @Override 39 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 40 throws ServletException, IOException { 41 resp.setCharacterEncoding("utf-8"); 42 req.setCharacterEncoding("utf-8"); 43 // TODO Auto-generated method stub 44 PrintWriter out=resp.getWriter(); 45 try { 46 47 Map<String, String>map=MessagesUitl.xmlToMap(req); 48 String fromUserName=map.get("FromUserName"); 49 String toUserName=map.get("ToUserName"); 50 //String createTime=map.get("CreateTime"); 51 String msgType=map.get("MsgType"); 52 String content=map.get("Content"); 53 //String msgId=map.get("MsgId"); 54 String message=null; 55 if (MessagesUitl.MESSAGE_TEXT.equals(msgType)) { 56 if ("1".equals(content)) { 57 message=MessagesUitl.initText(toUserName, fromUserName, MessagesUitl.firstMenu()); 58 }else if ("2".equals(content)) { 59 message=MessagesUitl.initText(toUserName, fromUserName, MessagesUitl.secondMenu()); 60 }else if ("3".equals(content)) { 61 message=MessagesUitl.initText(toUserName, fromUserName, MessagesUitl.thirdMenu()); 62 }else if ("4".equals(content)) { 63 message=MessagesUitl.initText(toUserName, fromUserName, MessagesUitl.fourtMenu()); 64 }else if ("?".equals(content)||"?".equals(content)) { 65 message=MessagesUitl.initText(toUserName, fromUserName, MessagesUitl.menuText()); 66 } 67 //测试代码 无用 68 // TextMessage text=new TextMessage(); 69 // text.setFromUserName(toUserName); 70 // text.setToUserName(fromUserName); 71 // text.setMsgType("text"); 72 // text.setCreateTime(new Date().getTime()); 73 // text.setContent("您发送的消息是:"+content); 74 // message=MessagesUitl.textMessageToXml(text); 75 }else if(MessagesUitl.MESSAGE_EVENT.equals(msgType)){//事件推送逻辑 76 String evenType=map.get("Event");//从集合中获取Event具体获取是哪一推送事件 77 //获取关注后的事件 78 if (MessagesUitl.MESSAGE_SUBSCRIBE.equals(evenType)) { 79 message=MessagesUitl.initText(toUserName, fromUserName, MessagesUitl.menuText()); 80 } 81 82 83 } 84 out.print(message); 85 } catch (DocumentException e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 }finally{ 89 out.close(); 90 } 91 92 93 } 94 }
参考代码:消息实体类
1 package com.po; 2 3 public class TextMessage { 4 5 6 private String ToUserName; //开发者微信号 7 private String FromUserName; //发送方帐号(一个OpenID) 8 private long CreateTime; //消息创建时间 (整型) 9 private String MsgType ;//text 10 private String Content;//文本消息内容 11 private String MsgId;//消息id,64位整型 12 public String getToUserName() { 13 return ToUserName; 14 } 15 public void setToUserName(String toUserName) { 16 ToUserName = toUserName; 17 } 18 public String getFromUserName() { 19 return FromUserName; 20 } 21 public void setFromUserName(String fromUserName) { 22 FromUserName = fromUserName; 23 } 24 public long getCreateTime() { 25 return CreateTime; 26 } 27 public void setCreateTime(long createTime) { 28 CreateTime = createTime; 29 } 30 public String getMsgType() { 31 return MsgType; 32 } 33 public void setMsgType(String msgType) { 34 MsgType = msgType; 35 } 36 public String getContent() { 37 return Content; 38 } 39 public void setContent(String content) { 40 Content = content; 41 } 42 public String getMsgId() { 43 return MsgId; 44 } 45 public void setMsgId(String msgId) { 46 MsgId = msgId; 47 } 48 }