云萌主云应用官方论坛

标题: java版微信机器人代码 [打印本页]

作者: 骑单车的小女孩    时间: 2016-12-30 00:47
标题: java版微信机器人代码
来源:http://www.open-open.com/code/view/1430907127288


  1.   
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.Date;
  5.   
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.   
  11. import com.eiyoung.wechat.web.utils.Message;
  12. import com.eiyoung.wechat.web.utils.ReplyMessage;
  13. import org.apache.commons.io.IOUtils;
  14.   
  15. import com.thoughtworks.xstream.XStream;
  16. import com.thoughtworks.xstream.io.xml.DomDriver;
  17.   
  18.   
  19. public class WeChat extends HttpServlet {
  20.     private static final long serialVersionUID = 1L;
  21.   
  22.   
  23.     public WeChat() {
  24.         super();
  25.     }
  26.   
  27.     /**
  28.      * 验证
  29.      * @param request
  30.      * @param response
  31.      * @throws ServletException
  32.      * @throws IOException
  33.      */
  34.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35.         response.setContentType("text/html;charset=UTF-8");
  36.         PrintWriter pw = response.getWriter();
  37.         String echo = request.getParameter("echostr");
  38.         System.out.print(echo);
  39.         echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8");
  40.         pw.println(echo);
  41.     }
  42.   
  43.   
  44.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  45.        response.setContentType("text/html;charset=UTF-8");
  46.         PrintWriter pw = response.getWriter();
  47.         String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8");
  48.         Message textMsg = null;
  49.         try {
  50.             textMsg = getMessage(wxMsgXml);
  51.         } catch (Exception e) {
  52.             e.printStackTrace();
  53.         }
  54.         StringBuffer replyMsg = new StringBuffer();
  55.         if(textMsg != null){
  56.             //增加你所需要的处理逻辑,这里只是简单重复消息
  57.             replyMsg.append("您给我的消息是:");
  58.             replyMsg.append(textMsg.getContent());
  59.         }
  60.         else{
  61.             replyMsg.append(":)不是文本的消息,我暂时看不懂");
  62.         }
  63.         String returnXml = getReplyMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
  64.         System.out.print(textMsg.toString());
  65.         System.out.print(returnXml.toString());
  66.         pw.println(returnXml);
  67.     }
  68.   
  69.     private Message getMessage(String xml){
  70.         XStream xstream = new XStream(new DomDriver());
  71.         xstream.alias("xml", Message.class);
  72.         xstream.aliasField("ToUserName", Message.class, "toUserName");
  73.         xstream.aliasField("FromUserName", Message.class, "fromUserName");
  74.         xstream.aliasField("CreateTime", Message.class, "createTime");
  75.         xstream.aliasField("MsgType", Message.class, "messageType");
  76.         xstream.aliasField("Content", Message.class, "content");
  77.         xstream.aliasField("MsgId", Message.class, "msgId");
  78.         Message Message = (Message)xstream.fromXML(xml);
  79.         return Message;
  80.     }
  81.   
  82.     private String getReplyMessage(String replyMsg,String toUserName,String fromUserName){
  83.         ReplyMessage we = new ReplyMessage();
  84.         we.setMessageType("text");
  85.         we.setFuncFlag("0");
  86.         we.setCreateTime(new Long(new Date().getTime()).toString());
  87.         we.setContent(replyMsg);
  88.         we.setToUserName(toUserName);
  89.         we.setFromUserName(fromUserName);
  90.         XStream xstream = new XStream(new DomDriver());
  91.         xstream.alias("xml", ReplyMessage.class);
  92.         xstream.aliasField("ToUserName", ReplyMessage.class, "toUserName");
  93.         xstream.aliasField("FromUserName", ReplyMessage.class, "fromUserName");
  94.         xstream.aliasField("CreateTime", ReplyMessage.class, "createTime");
  95.         xstream.aliasField("MsgType", ReplyMessage.class, "messageType");
  96.         xstream.aliasField("Content", ReplyMessage.class, "content");
  97.         xstream.aliasField("FuncFlag", ReplyMessage.class, "funcFlag");
  98.         String xml =xstream.toXML(we);
  99.         return xml;
  100.     }
  101.   
  102.   
  103. }
  104.   
  105. ===========================================================================
  106. package com.eiyoung.wechat.web.utils;
  107.   
  108. /**
  109. * Created with IntelliJ IDEA.
  110. * User: sb
  111. * Date: 8/1/13
  112. * Time: 10:37 PM
  113. * To change this template use File | Settings | File Templates.
  114. */
  115. public class ReplyMessage {
  116.   
  117.     private String FuncFlag;//消息编号
  118.     private String fromUserName;//发送人
  119.     private String toUserName;//接收人
  120.     private String content;//内容
  121.     private String messageType;//消息类型
  122.     private String createTime;//创建日期
  123.   
  124.   
  125.     public String getContent() {
  126.         return content;
  127.     }
  128.   
  129.     public void setContent(String content) {
  130.         this.content = content;
  131.     }
  132.   
  133.     public String getMessageType() {
  134.         return messageType;
  135.     }
  136.   
  137.     public void setMessageType(String messageType) {
  138.         this.messageType = messageType;
  139.     }
  140.   
  141.     public String getFromUserName() {
  142.         return fromUserName;
  143.     }
  144.   
  145.     public void setFromUserName(String fromUserName) {
  146.         this.fromUserName = fromUserName;
  147.     }
  148.   
  149.     public String getToUserName() {
  150.         return toUserName;
  151.     }
  152.   
  153.     public void setToUserName(String toUserName) {
  154.         this.toUserName = toUserName;
  155.     }
  156.   
  157.     public String getCreateTime() {
  158.         return createTime;
  159.     }
  160.   
  161.     public void setCreateTime(String createTime) {
  162.         this.createTime = createTime;
  163.     }
  164.   
  165.     public String getFuncFlag() {
  166.         return FuncFlag;
  167.     }
  168.   
  169.     public void setFuncFlag(String funcFlag) {
  170.         FuncFlag = funcFlag;
  171.     }
  172.   
  173.     public String toString(){
  174.         return "createTime:"+getCreateTime()+"\\ntoUserName:"+getToUserName()+"\\n FromUserName:"+getFromUserName()+"\\nmessageType:"+getMessageType()+"\\ncontent:"+getContent();
  175.     }
  176. }
  177.   
  178. ===============================================================================
  179. package com.eiyoung.wechat.web.utils;
  180.   
  181. import java.util.Date;
  182.   
  183. /**
  184. * Created with IntelliJ IDEA.
  185. * User: sb
  186. * Date: 8/1/13
  187. * Time: 10:37 PM
  188. * To change this template use File | Settings | File Templates.
  189. */
  190. public class Message {
  191.   
  192.     private String msgId;//消息编号
  193.     private String fromUserName;//发送人
  194.     private String toUserName;//接收人
  195.     private String content;//内容
  196.     private String messageType;//消息类型
  197.     private String createTime;//创建日期
  198.   
  199.   
  200.     public String getContent() {
  201.         return content;
  202.     }
  203.   
  204.     public void setContent(String content) {
  205.         this.content = content;
  206.     }
  207.   
  208.     public String getMessageType() {
  209.         return messageType;
  210.     }
  211.   
  212.     public void setMessageType(String messageType) {
  213.         this.messageType = messageType;
  214.     }
  215.   
  216.     public String getFromUserName() {
  217.         return fromUserName;
  218.     }
  219.   
  220.     public void setFromUserName(String fromUserName) {
  221.         this.fromUserName = fromUserName;
  222.     }
  223.   
  224.     public String getToUserName() {
  225.         return toUserName;
  226.     }
  227.   
  228.     public void setToUserName(String toUserName) {
  229.         this.toUserName = toUserName;
  230.     }
  231.   
  232.     public String getCreateTime() {
  233.         return createTime;
  234.     }
  235.   
  236.     public void setCreateTime(String createTime) {
  237.         this.createTime = createTime;
  238.     }
  239.   
  240.     public String getMsgId() {
  241.         return msgId;
  242.     }
  243.   
  244.     public void setMsgId(String msgId) {
  245.         this.msgId = msgId;
  246.     }
  247.   
  248.     public String toString(){
  249.         return "msgId:"+getMsgId()+"\\ncreateTime:"+getCreateTime()+"\\ntoUserName:"+getToUserName()+"\\n FromUserName:"+getFromUserName()+"\\nmessageType:"+getMessageType()+"\\ncontent:"+getContent();
  250.     }
  251. }
  252. ==============================================================================
  253. maven
  254. <dependencies>
  255.     <dependency>
  256.       <groupId>junit</groupId>
  257.       <artifactId>junit</artifactId>
  258.       <version>3.8.1</version>
  259.       <scope>test</scope>
  260.     </dependency>
  261.     <dependency>
  262.       <groupId>javax.servlet</groupId>
  263.       <artifactId>servlet-api</artifactId>
  264.       <version>3.0-alpha-1</version>
  265.     </dependency>
  266.       <dependency>
  267.           <groupId>joda-time</groupId>
  268.           <artifactId>joda-time</artifactId>
  269.           <version>2.2</version>
  270.       </dependency>
  271.       <dependency>
  272.           <groupId>org.apache.commons</groupId>
  273.           <artifactId>commons-io</artifactId>
  274.           <version>1.3.2</version>
  275.       </dependency>
  276.       <dependency>
  277.           <groupId>com.thoughtworks.xstream</groupId>
  278.           <artifactId>xstream</artifactId>
  279.           <version>1.4.3</version>
  280.       </dependency>
  281.   </dependencies>

复制代码







欢迎光临 云萌主云应用官方论坛 (https://yunmengzhu.com/) Powered by Discuz! X3.4