• Stars
    star
    119
  • Rank 297,930 (Top 6 %)
  • Language
    Java
  • Created over 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

微信公共平台机器人Java SDK

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

More Repositories

1

jenv

Java enVironment Manager
Shell
603
star
2

chatgpt-spring-boot-starter

Spring Boot ChatGPT Starter
Java
481
star
3

spring-boot-dubbo

Spring Boot with Dubbo support
Java
278
star
4

dubbo3

Dubbo3: distributed RPC framework from Alibaba Dubbo2
Java
255
star
5

ddd-base

DDD(Domain Driven Design) base package for java
Java
255
star
6

awesome-rsocket

Awesome RSocket
190
star
7

mybatis-r2dbc

MyBatis R2DBC Adapter
Java
131
star
8

plantuml-gist

PlantUML with Github Gist and Gitlab Support
Java
96
star
9

dx

A tool and task runner for writing better scripts with Deno
TypeScript
93
star
10

zookeeper-intellij

IntelliJ IDEA ZooKeeper Plugin
Java
62
star
11

spring-boot-starter-redisson

Spring Boot starter for Redisson
Java
58
star
12

h2-functions-4-mysql

H2 functions for MySQL
Java
57
star
13

spring-boot-starter-okhttp3

Spring Boot Starter for OkHttp3
Java
55
star
14

spring-boot-starter-httpclient

Spring Boot Starter for HttpClient
Java
54
star
15

vitest-jetbrains-plugin

Vitest JetBrains plugin
Kotlin
52
star
16

spring-boot-rsocket-demo

Spring Boot RSocket Demo with RPC style
Java
42
star
17

spring-boot-thymeleaf3-bootstrap4-angular4

Spring Boot Kotlin Application Template for Thymeleaf3, BootStrap4 and Angularjs 4
TypeScript
42
star
18

toolchains-maven-plugin

Toolchains Maven Plugin with JDK auto download
Java
36
star
19

xtermjs-spring-boot-starter

Web terminal to manage your Spring Boot app
Java
30
star
20

spring-cloud-function-demo

Spring cloud function with RSocket
Java
24
star
21

spring-boot-starter-wechatmp

Spring Boot with Wechat Support
Java
23
star
22

ngrok-spring-boot-starter

Easy to expose a local Spring Boot Application to the internet by ngrok
Java
23
star
23

smarty4j

Smarty Template Engine for Java
Java
22
star
24

spring-boot-starter-oss

Spring Boot Start with Aliyun OSS
Java
19
star
25

reactive-demo

Reactive Demo with Reactor, RxJava and reactive-stream
Java
19
star
26

jetbrains-just-plugin

JetBrains Just Command Runner Plugin
Kotlin
19
star
27

cli-completion

CLI completion for bash, zsh, fish and powershell
Rust
18
star
28

spring-boot-microservices

Spring Boot MicroServices
Java
18
star
29

spring-boot-starter-shell

Spring Shell with Spring Boot Driven
Java
17
star
30

chrome_js_api_stub

Chrome JavaScript API Stub to assistant extension development in IDE
JavaScript
16
star
31

rsocket-load-balancing

RSocket load balancing based on Spring Cloud Service Registry
Java
15
star
32

kotlin-wasm-demo

Kotlin WebAssembly Demo
Kotlin
15
star
33

spring-boot-kotlin-microservices

Spring Boot MicroServices with Kotlin
Kotlin
14
star
34

task-keeper

A cli to manage tasks from different task runners or package managers
Rust
13
star
35

open2internet-spring-boot-starter

Expose local Spring Boot Application to internet for demonstration, testing and debuging
Java
13
star
36

ali-oss-shell

Aliyun OSS Shell
Java
12
star
37

easy-random-junit5-extension

JUnit 5 extension for easy-random
Java
12
star
38

rocker-template-demo

Rocker template demo with Spring Boot
Java
12
star
39

mail-catcher

MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface
Java
12
star
40

kotlin-platform-bom

Kotlin Platform BOM
Just
11
star
41

axum-demo

Axum demo
Rust
11
star
42

rsocket-deno

🦕RSocket Deno module
TypeScript
11
star
43

r2dbc-demo

R2DBC demo
Java
11
star
44

oh-my-zsh-spring-boot-plugin

oh-my-zsh Spring Boot plugin
10
star
45

team

Team Instruction
10
star
46

java-error-messages-wizard

Java Error Message Wizard
Java
10
star
47

ddd-demo

DDD demo with Java 8 and Spring Boot
Java
9
star
48

jenv-ps

jenv Power Shell Edition
PowerShell
9
star
49

nats-spring-boot-starter

Spring Boot starter for NATS
Java
9
star
50

struts2-spring-boot-integration

Spring Boot with Struts 2.5.x Integration
Java
8
star
51

kafka-demo

Kafka Demo with Spring Boot, Docker and Terraform
Java
8
star
52

rest-editor-client-contrib

REST Editor Client Contrib plugin for IntelliJ IDEA
Java
8
star
53

jenv-idea-plugin

jenv IntelliJ IDEA plugin
Java
8
star
54

mybatis-demo

MyBatis Demo with spring-boot-starter-mybatis integration
Java
8
star
55

platform-bom-contrib

Spring Platform IO BOM Contrib
Java
8
star
56

java9-in-action

Java 9 in Action
Java
7
star
57

aliyun-openapi-rust-sdk

Aliyun OpenAPI SDK for Rust
Rust
7
star
58

tgm

Template generator management
Rust
7
star
59

npm-export-spring-boot-starter

Spring Boot Starter to generate npm package to call REST API
Java
7
star
60

xterm-demo

Xterm.js with RSocket
JavaScript
7
star
61

apache-pulsar-demo

Apache Pulsar demo
Java
7
star
62

http2-java-demo

Spring Boot with HTTP/2
Java
7
star
63

spring-boot-apache-dubbo

Spring Boot 2.0 with Apache Dubbo
Java
7
star
64

markdown-chatgpt

Talk to ChatGPT from Markdown
Kotlin
7
star
65

trpc-spring-boot-starter

Spring Boot Starter for tRPC
Java
7
star
66

reactive-grpc-demo

gRPC with reactive Reactor
Java
6
star
67

unitTest_templates

unitTest IntelliJ IDEA plugin templates
6
star
68

proto-rsocket-plugin

Protobuf to RSocket service interface
Java
6
star
69

grpc-kotlin-demo

gRPC with Kotlin Demo
Kotlin
6
star
70

java11_in_action

Java 11 in Action
Java
6
star
71

markdown-wasm

Markdown wasm based on md4c for Deno 🦕
TypeScript
6
star
72

kotlin-deno-example

🦕Deno app development with Kotlin
Kotlin
6
star
73

zshmarks

zsh marks for oh-my-zsh
6
star
74

trunk-demo

WebAssembly development with Trunk & Vite.js
Rust
6
star
75

reactive-grpc-spring-boot-starter

Spring Boot Starter for Reactive gRPC
Java
6
star
76

hessian-spring-boot-starter

Spring Boot Start for Hessian
Java
6
star
77

alibaba-broker-example-parent

Alibaba RSocket Broker Example
Java
6
star
78

akka-demo

Akka Demo with Kotlin, Spring Integration
Java
5
star
79

intellij-templates

IntelliJ IDEA live templates
5
star
80

zookeeper-shell

New ZooKeeper shell just like bash
Java
5
star
81

embind_demo

Emscripten embind demo
C++
5
star
82

graal-native-image-demo

Graal native image demo with RSocket and Netty
Java
5
star
83

dkim-spring-boot-starter

DKIM for spring-boot-starter-mail
Java
5
star
84

akka-spring-boot-starter

akka-spring-boot-starter
Java
5
star
85

rsocket-dart

RSocket Dart SDK
Dart
4
star
86

deno-rust-wasm-demo

Deno with Rust Wasm
TypeScript
4
star
87

rest-graphql-rsocket-demo

One controller to combine HTTP REST, GraphQL and RSocket
Java
4
star
88

aria2-shell

Aria2 Shell
Java
4
star
89

spring-boot-starter-job-controller

Spring Boot with Job Controller
Java
4
star
90

aeron-demo

Aeron Spring Reactive Demo
Java
4
star
91

rsocket-transport-quic

RSocket QUIC transport
Java
4
star
92

spring-cloud-aliyun

Integration for Aliyun Cloud Services APIs with Spring Cloud
Shell
4
star
93

jvmtop

Clone from https://code.google.com/p/jvmtop
Java
4
star
94

kotlin-wasm-browser-demo

Kotlin WebAssembly demo for Browser
Kotlin
4
star
95

deno-httpx

httpx: cli to run requests from http file
TypeScript
4
star
96

kotlin-coroutine-demo

Kotlin Coroutines demo
Kotlin
4
star
97

spring-boot-polyglot-demo

Spring Boot Polyglot with Kotlin, Java + Lombok + Groovy
Kotlin
4
star
98

esnippet_idea_plugin

eSnippet IntelliJ IDEA plugin
Java
4
star
99

microservices-annotator

Java Annotations for Microservices annotator to mark your code
Java
3
star
100

logback-fluentd-appender

Logback Fluentd Appender
Java
3
star