Java版微信机器人代码

  1. package com.eiyoung.wechat.web.controller;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.Date;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import com.eiyoung.wechat.web.utils.Message;
  10. import com.eiyoung.wechat.web.utils.ReplyMessage;
  11. import org.apache.commons.io.IOUtils;
  12. import com.thoughtworks.xstream.XStream;
  13. import com.thoughtworks.xstream.io.xml.DomDriver;
  14. public class WeChat extends HttpServlet {
  15. private static final long serialVersionUID = 1L;
  16. public WeChat() {
  17. super();
  18. * 验证
  19. * @param request
  20. * @param response
  21. * @throws ServletException
  22. * @throws IOException
  23. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. response.setContentType("text/html;charset=UTF-8");
  25. PrintWriter pw = response.getWriter();
  26. String echo = request.getParameter("echostr");
  27. System.out.print(echo);
  28. echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8");
  29. pw.println(echo);
  30. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  31. response.setContentType("text/html;charset=UTF-8");
  32. PrintWriter pw = response.getWriter();
  33. String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8");
  34. Message textMsg = null;
  35. try {
  36. textMsg = getMessage(wxMsgXml);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. StringBuffer replyMsg = new StringBuffer();
  40. if(textMsg != null){
  41. //增加你所需要的处理逻辑,这里只是简单重复消息
  42. replyMsg.append("您给我的消息是:");
  43. replyMsg.append(textMsg.getContent());
  44. else{
  45. replyMsg.append(":)不是文本的消息,我暂时看不懂");
  46. String returnXml = getReplyMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
  47. System.out.print(textMsg.toString());
  48. System.out.print(returnXml.toString());
  49. pw.println(returnXml);
  50. private Message getMessage(String xml){
  51. XStream xstream = new XStream(new DomDriver());
  52. xstream.alias("xml", Message.class);
  53. xstream.aliasField("ToUserName", Message.class, "toUserName");
  54. xstream.aliasField("FromUserName", Message.class, "fromUserName");
  55. xstream.aliasField("CreateTime", Message.class, "createTime");
  56. xstream.aliasField("MsgType", Message.class, "messageType");
  57. xstream.aliasField("Content", Message.class, "content");
  58. xstream.aliasField("MsgId", Message.class, "msgId");
  59. Message Message = (Message)xstream.fromXML(xml);
  60. return Message;
  61. private String getReplyMessage(String replyMsg,String toUserName,String fromUserName){
  62. ReplyMessage we = new ReplyMessage();
  63. we.setMessageType("text");
  64. we.setFuncFlag("0");
  65. we.setCreateTime(new Long(new Date().getTime()).toString());
  66. we.setContent(replyMsg);
  67. we.setToUserName(toUserName);
  68. we.setFromUserName(fromUserName);
  69. XStream xstream = new XStream(new DomDriver());
  70. xstream.alias("xml", ReplyMessage.class);
  71. xstream.aliasField("ToUserName", ReplyMessage.class, "toUserName");
  72. xstream.aliasField("FromUserName", ReplyMessage.class, "fromUserName");
  73. xstream.aliasField("CreateTime", ReplyMessage.class, "createTime");
  74. xstream.aliasField("MsgType", ReplyMessage.class, "messageType");
  75. xstream.aliasField("Content", ReplyMessage.class, "content");
  76. xstream.aliasField("FuncFlag", ReplyMessage.class, "funcFlag");
  77. String xml =xstream.toXML(we);
  78. return xml;
  79. package com.eiyoung.wechat.web.utils;
  80. * Created with IntelliJ IDEA.
  81. * User: sb
  82. * Date: 8/1/13
  83. * Time: 10:37 PM
  84. * To change this template use File | Settings | File Templates.
  85. public class ReplyMessage {
  86. private String FuncFlag;//消息编号
  87. private String fromUserName;//发送人
  88. private String toUserName;//接收人
  89. private String content;//内容
  90. private String messageType;//消息类型
  91. private String createTime;//创建日期
  92. public String getContent() {
  93. return content;
  94. public void setContent(String content) {
  95. this.content = content;
  96. public String getMessageType() {
  97. return messageType;
  98. public void setMessageType(String messageType) {
  99. this.messageType = messageType;
  100. public String getFromUserName() {
  101. return fromUserName;
  102. public void setFromUserName(String fromUserName) {
  103. this.fromUserName = fromUserName;
  104. public String getToUserName() {
  105. return toUserName;
  106. public void setToUserName(String toUserName) {
  107. this.toUserName = toUserName;
  108. public String getCreateTime() {
  109. return createTime;
  110. public void setCreateTime(String createTime) {
  111. this.createTime = createTime;
  112. public String getFuncFlag() {
  113. return FuncFlag;
  114. public void setFuncFlag(String funcFlag) {
  115. FuncFlag = funcFlag;
  116. public String toString(){
  117. return "createTime:"+getCreateTime()+" toUserName:"+getToUserName()+" FromUserName:"+getFromUserName()+" messageType:"+getMessageType()+" content:"+getContent();http://www.huiyi8.com/tishiyin/ 提示音
  118. package com.eiyoung.wechat.web.utils;
  119. import java.util.Date;
  120. * Created with IntelliJ IDEA.
  121. * User: sb
  122. * Date: 8/1/13
  123. * Time: 10:37 PM
  124. * To change this template use File | Settings | File Templates.
  125. public class Message {
  126. private String msgId;//消息编号
  127. private String fromUserName;//发送人
  128. private String toUserName;//接收人
  129. private String content;//内容
  130. private String messageType;//消息类型
  131. private String createTime;//创建日期
  132. public String getContent() {
  133. return content;
  134. public void setContent(String content) {
  135. this.content = content;
  136. public String getMessageType() {
  137. return messageType;
  138. public void setMessageType(String messageType) {
  139. this.messageType = messageType;
  140. public String getFromUserName() {
  141. return fromUserName;
  142. public void setFromUserName(String fromUserName) {
  143. this.fromUserName = fromUserName;
  144. public String getToUserName() {
  145. return toUserName;
  146. public void setToUserName(String toUserName) {
  147. this.toUserName = toUserName;
  148. public String getCreateTime() {
  149. return createTime;
  150. public void setCreateTime(String createTime) {
  151. this.createTime = createTime;
  152. public String getMsgId() {
  153. return msgId;
  154. public void setMsgId(String msgId) {
  155. this.msgId = msgId;
  156. public String toString(){
  157. return "msgId:"+getMsgId()+" createTime:"+getCreateTime()+" toUserName:"+getToUserName()+" FromUserName:"+getFromUserName()+" messageType:"+getMessageType()+" content:"+getContent();
  158. maven
  159. junit
  160. junit
  161. 3.8.1
  162. test
  163. javax.servlet
  164. servlet-api
  165. 3.0-alpha-1
  166. joda-time
  167. joda-time
  168. 2.2
  169. org.apache.commons
  170. commons-io
  171. 1.3.2
  172. com.thoughtworks.xstream
  173. xstream
  174. 1.4.3
  175. 其实做为一个开发者,有一个学习的氛围跟一个交流圈子特别重要这里我推荐一个Java交流群664389243,不管你是小白还是大牛欢迎入驻,大家一起交流成长。

你可能感兴趣的:(Java版微信机器人代码)