issue #77 添加自定义消息匹配器的支持
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||
|
||||
/**
|
||||
* 消息匹配器,用在消息路由的时候
|
||||
*/
|
||||
public interface WxCpMessageMatcher {
|
||||
|
||||
/**
|
||||
* 消息是否匹配某种模式
|
||||
* @param message
|
||||
* @return
|
||||
*/
|
||||
public boolean match(WxCpXmlMessage message);
|
||||
|
||||
}
|
||||
@@ -228,6 +228,8 @@ public class WxCpMessageRouter {
|
||||
|
||||
private String rContent;
|
||||
|
||||
private WxCpMessageMatcher matcher;
|
||||
|
||||
private boolean reEnter = false;
|
||||
|
||||
private Integer agentId;
|
||||
@@ -320,6 +322,16 @@ public class WxCpMessageRouter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
|
||||
* @param matcher
|
||||
* @return
|
||||
*/
|
||||
public Rule matcher(WxCpMessageMatcher matcher) {
|
||||
this.matcher = matcher;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信消息拦截器
|
||||
* @param interceptor
|
||||
@@ -403,6 +415,8 @@ public class WxCpMessageRouter {
|
||||
(this.content == null || this.content.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
|
||||
&&
|
||||
(this.rContent == null || Pattern.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
|
||||
&&
|
||||
(this.matcher == null || this.matcher.match(wxMessage))
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user