一款轻量级的微信消息处理框架,可以让业务代码与微信微信处理框架代码解耦,并且你根本不需要关心消息是如何发送的,你只需要关注你的业务即可.
It encapsulates WeChat message receiving and sending, and can use annotation-driven development to facilitate business development without having to pay attention to the details of message receiving and message sending.
The message processor abstract class is encapsulated internally, which provides functions such as message filtering before processing messages, default behavior logs, etc. Users can inherit this class to implement their own business.
Just add annotations to separate the message processors that process each type. It also supports separating message processors for different event types, avoiding the need to use large sections of if elseif elseif to determine the type of messages in the code that handles business logic. way. The responsibilities of each interface are clear and the implementation is more decoupled;
Create a wechat.properties property file in the root path of the classpath and configure wechat.appId, wechat.appsecret, wechat.token, and wechat.encodingAESKey ( the name must be the same as this ). The example is as follows:
Write a message processor class, inherit the AbstractMessageHandler abstract class, implement the doHandleMessage method, add the @MessageProcessor annotation on the class, and specify the message type to be processed. The attribute messageType specifies the message type to be processed, and eventType specifies the event type to be processed. . When the message type is a normal message, the eventType attribute does not need to be specified (even if specified, it will be invalid). The sample code is as follows:
@ Component
@ MessageProcessor ( messageType = MessageType . TEXT_MESSAGE )
public class TextMessageHandlerExample extends AbstractMessageHandler {
private static final Logger LOGGER = LoggerFactory . getLogger ( TextMessageHandlerExample . class );
public BaseResponseMessage doHandleMessage ( BaseRequestMessage requestMessage ) {
//在这里实现你自己的业务逻辑
TextRequestMessage textRequestMessage = ( TextRequestMessage ) baseRequestMessage ;
return MessageUtils . buildTextResponseMessage ( baseRequestMessage , textRequestMessage . getContent ());
}
}
@ Component
@ MessageProcessor ( messageType = MessageType . EVENT , eventType = EventType . EVENT_SUBSCRIBE )
public class SubscribeEventMessageHandlerExample extends AbstractMessageHandler {
private static final Logger LOGGER = LoggerFactory . getLogger ( SubscribeEventMessageHandlerExample . class );
@ Override
public BaseResponseMessage doHandleMessage ( BaseRequestMessage baseRequestMessage ) {
SubOrUnSubEventRequestMessage subOrUnSubEventRequestMessage = ( SubOrUnSubEventRequestMessage ) baseRequestMessage ;
//在这里实现你自己的业务逻辑
}
}
just run your application!! have fun...
The sample code links for receiving and replying to various message types are as follows for readers’ reference.
Basic message types
Event message type
Email: [email protected], bugs and suggestions are welcome.
Online resume: https://151376liujie.github.io/resume/