{"id":13471478,"url":"https://github.com/linux-china/weixin-robot-java","last_synced_at":"2025-08-20T18:33:58.142Z","repository":{"id":7871099,"uuid":"9243652","full_name":"linux-china/weixin-robot-java","owner":"linux-china","description":"微信公共平台机器人Java SDK","archived":false,"fork":false,"pushed_at":"2022-12-16T00:44:31.000Z","size":55,"stargazers_count":123,"open_issues_count":10,"forks_count":90,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-12-17T19:17:45.063Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linux-china.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-05T15:04:29.000Z","updated_at":"2024-09-13T09:05:16.000Z","dependencies_parsed_at":"2023-01-11T17:15:26.179Z","dependency_job_id":null,"html_url":"https://github.com/linux-china/weixin-robot-java","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fweixin-robot-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fweixin-robot-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fweixin-robot-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fweixin-robot-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linux-china","download_url":"https://codeload.github.com/linux-china/weixin-robot-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230445927,"owners_count":18227060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:00:45.646Z","updated_at":"2024-12-19T14:08:08.732Z","avatar_url":"https://github.com/linux-china.png","language":"Java","funding_links":[],"categories":["Java"],"sub_categories":[],"readme":"Weixin robot Java\n========================================\n微信公共平台自动回复机器人的Java SDK，你可以使用SDK简单快速构建微信机器人。\n微信Robot Java借鉴了url rewrite的思想，url rewrite是根据url进行路由，而微信Robot则是根据消息类型和内容进行路由。\n整理的流程如下：\n\n* WexinRobotServlet负责认证和微信消息接收\n* 接收后进行XML解析，构建出 WeixinMessage对象，并注入到request的attribute中，名字为wxMsg\n* 根据配置的路由参数，对微信消息进行匹配\n* 根据匹配的结果进行URL路由转发\n* 最后由处理的组件完成xml格式反馈的输出\n\n\n### 如何使用\n首先我们要在pom.xml中添加：\n\n     \u003cdependency\u003e\n        \u003cgroupId\u003eorg.mvnsearch.wx\u003c/groupId\u003e\n        \u003cartifactId\u003ewx-robot-sdk\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.0\u003c/version\u003e\n     \u003c/dependency\u003e\n\n接下来就是修改web.xml添加Servlet负责接收微信公共平台发过来的消息：\n\n        \u003cservlet\u003e\n            \u003cservlet-name\u003eWexinRobotServlet\u003c/servlet-name\u003e\n            \u003cservlet-class\u003eorg.mvnsearch.wx.servlet.WexinRobotServlet\u003c/servlet-class\u003e\n            \u003cinit-param\u003e\n                \u003cparam-name\u003etoken\u003c/param-name\u003e\n                \u003cparam-value\u003eyourTokenHere\u003c/param-value\u003e\n            \u003c/init-param\u003e\n            \u003cinit-param\u003e\n                \u003cparam-name\u003econfFile\u003c/param-name\u003e\n                \u003cparam-value\u003e/WEB-INF/weixin-router.xml\u003c/param-value\u003e\n            \u003c/init-param\u003e\n        \u003c/servlet\u003e\n\n        \u003c!-- weixing --\u003e\n        \u003cservlet-mapping\u003e\n            \u003cservlet-name\u003eWexinRobotServlet\u003c/servlet-name\u003e\n            \u003curl-pattern\u003e/servlet/weixinrobot\u003c/url-pattern\u003e\n        \u003c/servlet-mapping\u003e\n\n其中token是指你在微信公共平台上的token，这里和其一致。接下来我们还需要设置servlet mapping，添加url-pattern，\n然后将最终的url，如上述配置的 http://www.foobar.com/servlet/weixinrobot 作为微信公共平台接口配置中的URL。\n接下来是在WEB-INF目录下创建一个weixin-router.xml的文件，完成信息的路由。\n\n接收到的消息如何路由到指定的组件处理，如Struts的Controller等，这里我们需要创建一个router文件，这里还是根据\n消息的类型和内容进行转发，内容如下：\n\n    \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\n    \u003cweixinrobot\u003e\n\n        \u003crule\u003e\n            \u003ctype\u003etext\u003c/type\u003e\n            \u003ccontent\u003e[\\d]{11}\u003c/content\u003e\n            \u003cforward\u003e/weixin2.wx\u003c/forward\u003e\n        \u003c/rule\u003e\n\n        \u003crule\u003e\n            \u003ctype\u003etext\u003c/type\u003e\n            \u003cforward\u003e/weixin.wx\u003c/forward\u003e\n        \u003c/rule\u003e\n\n    \u003c/weixinrobot\u003e\n\n上述的例子就是如果内容是手机号，交与/weixin2.wx这个URL处理。当然我们设置了一个默认的选项，如果text类型的消息没有任何\n匹配，则由/weixin.wx处理。\n\n收到微信消息后，要给出消息反馈。微信公共平台的消息反馈是一个xml格式的数据，在Java的系统中，我们使用jspx输出接口，样例如下：\n\n     \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n     \u003cxml\u003e\n         \u003cToUserName\u003e${wxMsg.sender}\u003c/ToUserName\u003e\n         \u003cFromUserName\u003e${wxMsg.receiver}\u003c/FromUserName\u003e\n         \u003cCreateTime\u003e${wxMsg.createdTime}\u003c/CreateTime\u003e\n         \u003cMsgType\u003etext\u003c/MsgType\u003e\n         \u003cContent\u003e${content}\u003c/Content\u003e\n         \u003cFuncFlag\u003e0\u003c/FuncFlag\u003e\n     \u003c/xml\u003e\n\n### 编码\n\n* 在代码中获取解析好的微信消息  WeixinMessage wxMsg = (WeixinMessage)request.getAttribute(\"wxMsg\");\n\n### FAQ\n\n* 如何调试： 由于微信服务器只能通知互联网IP和80端口，开发时你可以在你家中的ADSL拨号路由器上设置一下80转发，\n然后微信服务器的消息通知就可以路由到你的笔记本上，方便你测试和开发。查看你的互联网Ip请访问 http://ip.mvnsearch.org\n* 参考样例清访问robot-demo Maven module\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fweixin-robot-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinux-china%2Fweixin-robot-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fweixin-robot-java/lists"}