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:
@@ -7,7 +7,7 @@ import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
@ToString(of = "text")
|
||||
@ToString(of = {"text", "inline"})
|
||||
public class Action {
|
||||
|
||||
public static Action simple(String text) {
|
||||
|
||||
@@ -22,7 +22,7 @@ public class BotCommandChain implements CommandChain {
|
||||
if (!commands.isEmpty()) {
|
||||
try {
|
||||
Command command = commands.get(0);
|
||||
log.debug("Run command {}", command.getClass().getSimpleName());
|
||||
log.debug("Run command {} ({})", command.getClass().getSimpleName(), command);
|
||||
command.process(botRequest, botResponse,
|
||||
new BotCommandChain(commands.subList(1, commands.size())));
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1,8 +1,20 @@
|
||||
package ru.penkrat.stbf.api;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface RequestMatcher {
|
||||
|
||||
boolean match(BotRequest botRequest);
|
||||
|
||||
default RequestMatcher or(RequestMatcher other) {
|
||||
Objects.requireNonNull(other);
|
||||
return (botRequest) -> match(botRequest) || other.match(botRequest);
|
||||
}
|
||||
|
||||
default RequestMatcher and(RequestMatcher other) {
|
||||
Objects.requireNonNull(other);
|
||||
return (botRequest) -> match(botRequest) && other.match(botRequest);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import lombok.experimental.UtilityClass;
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@UtilityClass
|
||||
public class RequestMatchers {
|
||||
|
||||
@@ -13,6 +16,41 @@ public class RequestMatchers {
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
public RequestMatcher cmd(Action action) {
|
||||
return request -> request.getMessageText()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(data -> Objects.equals(action.getCmd(), data))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
public RequestMatcher text(String callbackData) {
|
||||
return request -> request.getCallbackData()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(data -> Objects.equals(callbackData, data))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
public RequestMatcher callbackData(String callbackData) {
|
||||
return request -> request.getCallbackData()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(data -> Objects.equals(callbackData, data))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
public RequestMatcher callbackDataStartsWith(String callbackDataPrefix) {
|
||||
return request -> request.getCallbackData()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(data -> data.startsWith(callbackDataPrefix))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
public RequestMatcher callbackDataRegexp(String callbackDataPrefix) {
|
||||
return request -> request.getCallbackData()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(Pattern.compile(callbackDataPrefix).asPredicate())
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
private static boolean matchText(Action action, String inputText) {
|
||||
return inputText.equalsIgnoreCase(action.getText())
|
||||
|| (action.getCmd() != null && inputText.equalsIgnoreCase(action.getCmd()));
|
||||
|
||||
@@ -2,27 +2,74 @@
|
||||
<actions>
|
||||
<action id="10001" name="start-action" command="/start">Start</action>
|
||||
<action id="10002" name="help-action" command="/help">Help</action>
|
||||
<action id="10003" name="to-inline-action" command="/inline">Inline</action>
|
||||
<action id="10004" name="inline1-action" callbackData="cmd:inline1">Inline button #1</action>
|
||||
<action id="10005" name="inline2-action" callbackData="cmd:inline2">Inline button #2</action>
|
||||
<action id="10006" name="url-action" url="https://git.penkrat.ru/ruslan/stbf">Git repo</action>
|
||||
</actions>
|
||||
<screens>
|
||||
<screen id="20001" name="on-start-screen">
|
||||
<text>This is demo bot</text>
|
||||
<text>This is demo bot
|
||||
use Help or /help to view help
|
||||
</text>
|
||||
<keyboard>
|
||||
<row>
|
||||
<button actionRef="help-action">Action.name</button>
|
||||
</row>
|
||||
</keyboard>
|
||||
</screen>
|
||||
<screen id="20001" name="on-help-screen">
|
||||
<text>This is demo help</text>
|
||||
<screen id="20002" name="on-help-screen">
|
||||
<text>This is demo help
|
||||
use /inline to switch to inline mode
|
||||
</text>
|
||||
<keyboard>
|
||||
<row>
|
||||
<button actionRef="help-action">Action.name</button>
|
||||
</row>
|
||||
</keyboard>
|
||||
</screen>
|
||||
<screen id="20003" name="inline-test-screen">
|
||||
<text>This is inline buttons:</text>
|
||||
<keyboard>
|
||||
<row>
|
||||
<button actionRef="inline1-action"></button>
|
||||
<button actionRef="inline2-action"></button>
|
||||
</row>
|
||||
<row>
|
||||
<button actionRef="url-action"></button>
|
||||
</row>
|
||||
</keyboard>
|
||||
</screen>
|
||||
<screen id="20004" name="inline-test-1-screen">
|
||||
<text>Inline Screen #1</text>
|
||||
<keyboard>
|
||||
<row>
|
||||
<button actionRef="inline1-action"></button>
|
||||
<button actionRef="inline2-action"></button>
|
||||
</row>
|
||||
<row>
|
||||
<button actionRef="url-action"></button>
|
||||
</row>
|
||||
</keyboard>
|
||||
</screen>
|
||||
<screen id="20005" name="inline-test-2-screen">
|
||||
<text>Inline Screen #2</text>
|
||||
<keyboard>
|
||||
<row>
|
||||
<button actionRef="inline1-action"></button>
|
||||
<button actionRef="inline2-action"></button>
|
||||
</row>
|
||||
<row>
|
||||
<button actionRef="url-action"></button>
|
||||
</row>
|
||||
</keyboard>
|
||||
</screen>
|
||||
</screens>
|
||||
<commands>
|
||||
<command actionRef="start-action" screenRef="on-start-screen" id="30001" name="startCommand"/>
|
||||
<command actionRef="help-action" screenRef="on-help-screen" id="30002" name="helpCommand"/>
|
||||
<command actionRef="to-inline-action" screenRef="inline-test-screen" id="30003" name="inlineTestCommand"/>
|
||||
<command actionRef="inline1-action" screenRef="inline-test-1-screen" edit="true" id="30004" name="inlineTest1Command"/>
|
||||
<command actionRef="inline2-action" screenRef="inline-test-2-screen" edit="true" id="30005" name="inlineTest2Command"/>
|
||||
</commands>
|
||||
</flow>
|
||||
@@ -2,7 +2,11 @@ package ru.penkrat.stbf.impl.pengrad;
|
||||
|
||||
import com.pengrad.telegrambot.TelegramBot;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
import com.pengrad.telegrambot.model.request.*;
|
||||
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
|
||||
import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
|
||||
import com.pengrad.telegrambot.model.request.KeyboardButton;
|
||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||
import com.pengrad.telegrambot.model.request.ReplyKeyboardMarkup;
|
||||
import com.pengrad.telegrambot.request.DeleteMessage;
|
||||
import com.pengrad.telegrambot.request.EditMessageText;
|
||||
import com.pengrad.telegrambot.request.SendDocument;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package ru.penkrat.stbf.impl.pengrad;
|
||||
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
|
||||
class NoKeyboard implements Keyboard {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EmptyKeyboard(pengrad)";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package ru.penkrat.stbf.impl.pengrad;
|
||||
|
||||
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
|
||||
import com.pengrad.telegrambot.model.request.KeyboardButton;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
import ru.penkrat.stbf.api.KeyboardBuilder;
|
||||
@@ -21,7 +20,15 @@ public class PengradKeyboardBuilder implements KeyboardBuilder {
|
||||
|
||||
@Override
|
||||
public Keyboard build() {
|
||||
return new KeyboardImpl(keyboard, inlineKeyboard);
|
||||
if (keyboard != null) {
|
||||
return new KeyboardImpl(keyboard, null);
|
||||
}
|
||||
if (inlineKeyboard != null) {
|
||||
InlineKeyboardButton[][] smallerArray = new InlineKeyboardButton[inlineKeyboard.length - 1][];
|
||||
System.arraycopy(inlineKeyboard, 0, smallerArray, 0, inlineKeyboard.length - 1);
|
||||
return new KeyboardImpl(null, smallerArray);
|
||||
}
|
||||
return new NoKeyboard();
|
||||
}
|
||||
|
||||
public KeyboardBuilder addGetContact(String text) {
|
||||
@@ -53,7 +60,13 @@ public class PengradKeyboardBuilder implements KeyboardBuilder {
|
||||
for (int i = 0; i < buttons.length; i++) {
|
||||
put(buttons[i]);
|
||||
}
|
||||
if (buttons.length > 0) {
|
||||
if (buttons[0].isInline()) {
|
||||
nextRowI();
|
||||
} else {
|
||||
nextRow();
|
||||
}
|
||||
}
|
||||
return self();
|
||||
}
|
||||
|
||||
@@ -107,9 +120,13 @@ public class PengradKeyboardBuilder implements KeyboardBuilder {
|
||||
|
||||
private void put(Action action) {
|
||||
if (action.isInline()) {
|
||||
put(new InlineKeyboardButton(action.getText()).callbackData(action.getCallbackData()));
|
||||
put(new InlineKeyboardButton(action.getText())
|
||||
.callbackData(action.getCallbackData())
|
||||
.url(action.getUrl()));
|
||||
} else {
|
||||
put(new KeyboardButton(action.getText()).requestContact(action.isRequestContact()));
|
||||
put(new KeyboardButton(action.getText())
|
||||
.requestContact(action.isRequestContact())
|
||||
.requestLocation(action.isRequestLocation()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,32 +50,36 @@ 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)) {
|
||||
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