mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-25 01:14:36 +08:00
issue #13 添加正则匹配content路由消息的功能
This commit is contained in:
parent
b938419f7f
commit
fef9f82b23
@ -6,6 +6,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import chanjarster.weixin.bean.WxXmlMessage;
|
import chanjarster.weixin.bean.WxXmlMessage;
|
||||||
import chanjarster.weixin.bean.WxXmlOutMessage;
|
import chanjarster.weixin.bean.WxXmlOutMessage;
|
||||||
@ -110,6 +111,8 @@ public class WxMessageRouter {
|
|||||||
|
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
private String rContent;
|
||||||
|
|
||||||
private boolean reEnter = false;
|
private boolean reEnter = false;
|
||||||
|
|
||||||
private List<WxMessageHandler> handlers = new ArrayList<WxMessageHandler>();
|
private List<WxMessageHandler> handlers = new ArrayList<WxMessageHandler>();
|
||||||
@ -170,6 +173,16 @@ public class WxMessageRouter {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果content匹配该正则表达式
|
||||||
|
* @param regex
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Rule rContent(String regex) {
|
||||||
|
this.rContent = regex;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置微信消息拦截器
|
* 设置微信消息拦截器
|
||||||
* @param interceptor
|
* @param interceptor
|
||||||
@ -247,6 +260,8 @@ public class WxMessageRouter {
|
|||||||
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
|
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
|
||||||
&&
|
&&
|
||||||
(this.content == null || this.content.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
|
(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()))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +39,7 @@ public class WxMessageRouterTest {
|
|||||||
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoMessageHandler(sb, WxConsts.EVT_CLICK)).end()
|
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoMessageHandler(sb, WxConsts.EVT_CLICK)).end()
|
||||||
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMessageHandler(sb, "KEY_1")).end()
|
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMessageHandler(sb, "KEY_1")).end()
|
||||||
.rule().async(async).content("CONTENT_1").handler(new WxEchoMessageHandler(sb, "CONTENT_1")).end()
|
.rule().async(async).content("CONTENT_1").handler(new WxEchoMessageHandler(sb, "CONTENT_1")).end()
|
||||||
|
.rule().async(async).rContent(".*bc.*").handler(new WxEchoMessageHandler(sb, "abcd")).end()
|
||||||
.rule().async(async).handler(new WxEchoMessageHandler(sb, "ALL")).end();
|
.rule().async(async).handler(new WxEchoMessageHandler(sb, "ALL")).end();
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
@ -106,6 +107,9 @@ public class WxMessageRouterTest {
|
|||||||
WxXmlMessage message5 = new WxXmlMessage();
|
WxXmlMessage message5 = new WxXmlMessage();
|
||||||
message5.setContent("BLA");
|
message5.setContent("BLA");
|
||||||
|
|
||||||
|
WxXmlMessage message6 = new WxXmlMessage();
|
||||||
|
message6.setContent("abcd");
|
||||||
|
|
||||||
WxXmlMessage c2 = new WxXmlMessage();
|
WxXmlMessage c2 = new WxXmlMessage();
|
||||||
c2.setMsgType(WxConsts.XML_MSG_TEXT);
|
c2.setMsgType(WxConsts.XML_MSG_TEXT);
|
||||||
c2.setEvent(WxConsts.EVT_CLICK);
|
c2.setEvent(WxConsts.EVT_CLICK);
|
||||||
@ -127,6 +131,7 @@ public class WxMessageRouterTest {
|
|||||||
new Object[] { message3, "KEY_1," },
|
new Object[] { message3, "KEY_1," },
|
||||||
new Object[] { message4, "CONTENT_1," },
|
new Object[] { message4, "CONTENT_1," },
|
||||||
new Object[] { message5, "ALL," },
|
new Object[] { message5, "ALL," },
|
||||||
|
new Object[] { message6, "abcd," },
|
||||||
new Object[] { c2, "COMBINE_2," },
|
new Object[] { c2, "COMBINE_2," },
|
||||||
new Object[] { c3, "COMBINE_3," },
|
new Object[] { c3, "COMBINE_3," },
|
||||||
new Object[] { c4, "COMBINE_4," }
|
new Object[] { c4, "COMBINE_4," }
|
||||||
|
Loading…
Reference in New Issue
Block a user