Git Product home page Git Product logo

weixin-robot-java's Introduction

Weixin robot Java

微信公共平台自动回复机器人的Java SDK,你可以使用SDK简单快速构建微信机器人。 微信Robot Java借鉴了url rewrite的**,url rewrite是根据url进行路由,而微信Robot则是根据消息类型和内容进行路由。 整理的流程如下:

  • WexinRobotServlet负责认证和微信消息接收
  • 接收后进行XML解析,构建出 WeixinMessage对象,并注入到request的attribute中,名字为wxMsg
  • 根据配置的路由参数,对微信消息进行匹配
  • 根据匹配的结果进行URL路由转发
  • 最后由处理的组件完成xml格式反馈的输出

如何使用

首先我们要在pom.xml中添加:

 <dependency>
    <groupId>org.mvnsearch.wx</groupId>
    <artifactId>wx-robot-sdk</artifactId>
    <version>1.0.0</version>
 </dependency>

接下来就是修改web.xml添加Servlet负责接收微信公共平台发过来的消息:

    <servlet>
        <servlet-name>WexinRobotServlet</servlet-name>
        <servlet-class>org.mvnsearch.wx.servlet.WexinRobotServlet</servlet-class>
        <init-param>
            <param-name>token</param-name>
            <param-value>yourTokenHere</param-value>
        </init-param>
        <init-param>
            <param-name>confFile</param-name>
            <param-value>/WEB-INF/weixin-router.xml</param-value>
        </init-param>
    </servlet>

    <!-- weixing -->
    <servlet-mapping>
        <servlet-name>WexinRobotServlet</servlet-name>
        <url-pattern>/servlet/weixinrobot</url-pattern>
    </servlet-mapping>

其中token是指你在微信公共平台上的token,这里和其一致。接下来我们还需要设置servlet mapping,添加url-pattern, 然后将最终的url,如上述配置的 http://www.foobar.com/servlet/weixinrobot 作为微信公共平台接口配置中的URL。 接下来是在WEB-INF目录下创建一个weixin-router.xml的文件,完成信息的路由。

接收到的消息如何路由到指定的组件处理,如Struts的Controller等,这里我们需要创建一个router文件,这里还是根据 消息的类型和内容进行转发,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<weixinrobot>

    <rule>
        <type>text</type>
        <content>[\d]{11}</content>
        <forward>/weixin2.wx</forward>
    </rule>

    <rule>
        <type>text</type>
        <forward>/weixin.wx</forward>
    </rule>

</weixinrobot>

上述的例子就是如果内容是手机号,交与/weixin2.wx这个URL处理。当然我们设置了一个默认的选项,如果text类型的消息没有任何 匹配,则由/weixin.wx处理。

收到微信消息后,要给出消息反馈。微信公共平台的消息反馈是一个xml格式的数据,在Java的系统中,我们使用jspx输出接口,样例如下:

 <?xml version="1.0" encoding="UTF-8"?>
 <xml>
     <ToUserName>${wxMsg.sender}</ToUserName>
     <FromUserName>${wxMsg.receiver}</FromUserName>
     <CreateTime>${wxMsg.createdTime}</CreateTime>
     <MsgType>text</MsgType>
     <Content>${content}</Content>
     <FuncFlag>0</FuncFlag>
 </xml>

编码

  • 在代码中获取解析好的微信消息 WeixinMessage wxMsg = (WeixinMessage)request.getAttribute("wxMsg");

FAQ

  • 如何调试: 由于微信服务器只能通知互联网IP和80端口,开发时你可以在你家中的ADSL拨号路由器上设置一下80转发, 然后微信服务器的消息通知就可以路由到你的笔记本上,方便你测试和开发。查看你的互联网Ip请访问 http://ip.mvnsearch.org
  • 参考样例清访问robot-demo Maven module

weixin-robot-java's People

Contributors

linux-china avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.