ホームページ >WeChat アプレット >WeChatの開発 >Java版WeChatロボットの実装方法を共有する

Java版WeChatロボットの実装方法を共有する

Y2J
Y2Jオリジナル
2017-04-28 10:49:436440ブラウズ

   
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
   
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
   
import com.eiyoung.wechat.web.utils.Message;
import com.eiyoung.wechat.web.utils.ReplyMessage;
import org.apache.commons.io.IOUtils;
   
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
   
   
public class WeChat extends HttpServlet {
    private static final long serialVersionUID = 1L;
   
   
    public WeChat() {
        super();
    }
   
    /**
     * 验证
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        String echo = request.getParameter("echostr");
        System.out.print(echo);
        echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8");
        pw.println(echo);
    }
   
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8");
        Message textMsg = null;
        try {
            textMsg = getMessage(wxMsgXml);
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuffer replyMsg = new StringBuffer();
        if(textMsg != null){
            //增加你所需要的处理逻辑,这里只是简单重复消息
            replyMsg.append("您给我的消息是:");
            replyMsg.append(textMsg.getContent());
        }
        else{
            replyMsg.append(":)不是文本的消息,我暂时看不懂");
        }
        String returnXml = getReplyMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
        System.out.print(textMsg.toString());
        System.out.print(returnXml.toString());
        pw.println(returnXml);
    }
   
    private Message getMessage(String xml){
        XStream xstream = new XStream(new DomDriver());
        xstream.alias("xml", Message.class);
        xstream.aliasField("ToUserName", Message.class, "toUserName");
        xstream.aliasField("FromUserName", Message.class, "fromUserName");
        xstream.aliasField("CreateTime", Message.class, "createTime");
        xstream.aliasField("MsgType", Message.class, "messageType");
        xstream.aliasField("Content", Message.class, "content");
        xstream.aliasField("MsgId", Message.class, "msgId");
        Message Message = (Message)xstream.fromXML(xml);
        return Message;
    }
   
    private String getReplyMessage(String replyMsg,String toUserName,String fromUserName){
        ReplyMessage we = new ReplyMessage();
        we.setMessageType("text");
        we.setFuncFlag("0");
        we.setCreateTime(new Long(new Date().getTime()).toString());
        we.setContent(replyMsg);
        we.setToUserName(toUserName);
        we.setFromUserName(fromUserName);
        XStream xstream = new XStream(new DomDriver());
        xstream.alias("xml", ReplyMessage.class);
        xstream.aliasField("ToUserName", ReplyMessage.class, "toUserName");
        xstream.aliasField("FromUserName", ReplyMessage.class, "fromUserName");
        xstream.aliasField("CreateTime", ReplyMessage.class, "createTime");
        xstream.aliasField("MsgType", ReplyMessage.class, "messageType");
        xstream.aliasField("Content", ReplyMessage.class, "content");
        xstream.aliasField("FuncFlag", ReplyMessage.class, "funcFlag");
        String xml =xstream.toXML(we);
        return xml;
    }
   
   
}
   
===========================================================================
package com.eiyoung.wechat.web.utils;
   
/**
 * Created with IntelliJ IDEA.
 * User: sb
 * Date: 8/1/13
 * Time: 10:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class ReplyMessage {
   
    private String FuncFlag;//消息编号
    private String fromUserName;//发送人
    private String toUserName;//接收人
    private String content;//内容
    private String messageType;//消息类型
    private String createTime;//创建日期
   
   
    public String getContent() {
        return content;
    }
   
    public void setContent(String content) {
        this.content = content;
    }
   
    public String getMessageType() {
        return messageType;
    }
   
    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }
   
    public String getFromUserName() {
        return fromUserName;
    }
   
    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }
   
    public String getToUserName() {
        return toUserName;
    }
   
    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }
   
    public String getCreateTime() {
        return createTime;
    }
   
    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
   
    public String getFuncFlag() {
        return FuncFlag;
    }
   
    public void setFuncFlag(String funcFlag) {
        FuncFlag = funcFlag;
    }
   
    public String toString(){
        return "createTime:"+getCreateTime()+"\\ntoUserName:"+getToUserName()+"\\n FromUserName:"+getFromUserName()+"\\nmessageType:"+getMessageType()+"\\ncontent:"+getContent();
    }
}
   
===============================================================================
package com.eiyoung.wechat.web.utils;
   
import java.util.Date;
   
/**
 * Created with IntelliJ IDEA.
 * User: sb
 * Date: 8/1/13
 * Time: 10:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class Message {
   
    private String msgId;//消息编号
    private String fromUserName;//发送人
    private String toUserName;//接收人
    private String content;//内容
    private String messageType;//消息类型
    private String createTime;//创建日期
   
   
    public String getContent() {
        return content;
    }
   
    public void setContent(String content) {
        this.content = content;
    }
   
    public String getMessageType() {
        return messageType;
    }
   
    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }
   
    public String getFromUserName() {
        return fromUserName;
    }
   
    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }
   
    public String getToUserName() {
        return toUserName;
    }
   
    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }
   
    public String getCreateTime() {
        return createTime;
    }
   
    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
   
    public String getMsgId() {
        return msgId;
    }
   
    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }
   
    public String toString(){
        return "msgId:"+getMsgId()+"\\ncreateTime:"+getCreateTime()+"\\ntoUserName:"+getToUserName()+"\\n FromUserName:"+getFromUserName()+"\\nmessageType:"+getMessageType()+"\\ncontent:"+getContent();
    }
}
==============================================================================
maven
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>3.0-alpha-1</version>
    </dependency>
      <dependency>
          <groupId>joda-time</groupId>
          <artifactId>joda-time</artifactId>
          <version>2.2</version>
      </dependency>
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-io</artifactId>
          <version>1.3.2</version>
      </dependency>
      <dependency>
          <groupId>com.thoughtworks.xstream</groupId>
          <artifactId>xstream</artifactId>
          <version>1.4.3</version>
      </dependency>

以上がJava版WeChatロボットの実装方法を共有するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。