commands with callbackData support
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package ru.penkrat.stbf.templates;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
|
||||
public interface ActionResolver {
|
||||
|
||||
Action getAction(String name);
|
||||
|
||||
RequestMatcher getMatcher(String actionName);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,12 @@ class ActionItem extends NamedItem {
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private String callbackData;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private String callbackDataRegexp;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private String callbackDataStartWith;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private String url;
|
||||
}
|
||||
|
||||
@@ -21,4 +21,7 @@ public class CommandItem extends NamedItem {
|
||||
@JacksonXmlProperty(isAttribute = true, localName = "class")
|
||||
private String clazz;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private boolean edit;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package ru.penkrat.stbf.templates.xml;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
import ru.penkrat.stbf.templates.ActionResolver;
|
||||
import ru.penkrat.stbf.templates.utils.StringUtils;
|
||||
import ru.penkrat.stbf.tools.RequestMatchers;
|
||||
|
||||
class FlowActionResolverDelegate implements ActionResolver {
|
||||
|
||||
@@ -17,7 +19,7 @@ class FlowActionResolverDelegate implements ActionResolver {
|
||||
final ActionItem actionItem = resolver.get(key);
|
||||
|
||||
boolean isInline = StringUtils.isNotEmpty(actionItem.getCallbackData())
|
||||
&& StringUtils.isNotEmpty(actionItem.getUrl());
|
||||
|| StringUtils.isNotEmpty(actionItem.getUrl());
|
||||
|
||||
if (isInline) {
|
||||
if (StringUtils.isNotEmpty(actionItem.getCommand())) {
|
||||
@@ -43,4 +45,21 @@ class FlowActionResolverDelegate implements ActionResolver {
|
||||
.url(actionItem.getUrl())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestMatcher getMatcher(String actionName) {
|
||||
final ActionItem actionItem = resolver.get(actionName);
|
||||
|
||||
if (StringUtils.isNotEmpty(actionItem.getCallbackDataRegexp())) {
|
||||
return RequestMatchers.callbackDataRegexp(actionItem.getCallbackDataRegexp());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(actionItem.getCallbackDataStartWith())) {
|
||||
return RequestMatchers.callbackDataStartsWith(actionItem.getCallbackDataStartWith());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(actionItem.getCallbackData())) {
|
||||
return RequestMatchers.callbackData(actionItem.getCallbackData());
|
||||
}
|
||||
|
||||
return RequestMatchers.action(getAction(actionName));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import ru.penkrat.stbf.templates.ActionResolver;
|
||||
import ru.penkrat.stbf.templates.CommandResolver;
|
||||
import ru.penkrat.stbf.templates.ScreenResolver;
|
||||
import ru.penkrat.stbf.templates.utils.StringUtils;
|
||||
import ru.penkrat.stbf.tools.RequestMatchers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -51,31 +50,35 @@ public class FlowCommandResolverDelegate implements CommandResolver {
|
||||
|
||||
private Command createCommand(CommandItem commandItem) {
|
||||
Action action = null;
|
||||
RequestMatcher actionMatcher = null;
|
||||
Screen screen = null;
|
||||
Function<Object, Screen> screenFactory;
|
||||
if (StringUtils.isNotEmpty(commandItem.getActionRef())) {
|
||||
action = actionResolver.getAction(commandItem.getActionRef());
|
||||
|
||||
actionMatcher = actionResolver.getMatcher(commandItem.getActionRef());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(commandItem.getScreenRef())) {
|
||||
screen = screenResolver.getScreen(commandItem.getScreenRef());
|
||||
screenFactory = screenResolver.getScreenFactory(commandItem.getScreenRef());
|
||||
}
|
||||
if (action != null && screen != null) {
|
||||
return simpleCommand(action, screen, commandItem.getId(), commandItem.getName());
|
||||
if (actionMatcher != null && screen != null) {
|
||||
return simpleCommand(actionMatcher, screen, commandItem.isEdit(), commandItem.getId(), commandItem.getName());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Command simpleCommand(Action action, Screen screen, String id, String name) {
|
||||
private Command simpleCommand(RequestMatcher matcher, Screen screen, boolean edit, String id, String name) {
|
||||
return new Command() {
|
||||
RequestMatcher matcher = RequestMatchers.action(action);
|
||||
|
||||
@Override
|
||||
public void process(BotRequest botRequest, BotResponse botResponse, CommandChain chain) {
|
||||
if (matcher.match(botRequest)) {
|
||||
botResponse.send(screen);
|
||||
if (edit) {
|
||||
botResponse.edit(screen);
|
||||
} else {
|
||||
botResponse.send(screen);
|
||||
}
|
||||
}
|
||||
chain.processCommand(botRequest, botResponse);
|
||||
}
|
||||
|
||||
@@ -81,6 +81,8 @@ class FlowScreenResolverDelegate implements ScreenResolver {
|
||||
.map(btn -> getAction(btn))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.info("Keyboard: {}", buttons);
|
||||
|
||||
builder.row(buttons.toArray(new Action[buttons.size()]));
|
||||
}
|
||||
|
||||
@@ -97,6 +99,9 @@ class FlowScreenResolverDelegate implements ScreenResolver {
|
||||
.text(btn.getText())
|
||||
.requestContact(btn.isRequestContact())
|
||||
.requestLocation(btn.isRequestLocation())
|
||||
.url(btn.getUrl())
|
||||
.callbackData(btn.getCallbackData())
|
||||
.inline(StringUtils.isNotEmpty(btn.getUrl()) || StringUtils.isNotEmpty(btn.getCallbackData()))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package ru.penkrat.stbf.templates.xml;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.Command;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
import ru.penkrat.stbf.templates.TemplateRenderer;
|
||||
|
||||
@@ -47,6 +48,11 @@ public class XmlFlowResolver implements FlowResolver {
|
||||
return actionDelegate.getAction(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestMatcher getMatcher(String actionName) {
|
||||
return actionDelegate.getMatcher(actionName);
|
||||
}
|
||||
|
||||
public void setTemplateRenderer(TemplateRenderer templateRenderer) {
|
||||
screenDelegate.setTemplateRenderer(templateRenderer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user