initial commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package ru.penkrat.stbf.common.command;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.BotRequest;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
import ru.penkrat.stbf.api.Command;
|
||||
import ru.penkrat.stbf.api.CommandChain;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
import ru.penkrat.stbf.tools.RequestMatchers;
|
||||
|
||||
public abstract class AbstractActionCommand implements Command {
|
||||
|
||||
private RequestMatcher matcher;
|
||||
|
||||
public AbstractActionCommand(Action action) {
|
||||
matcher = RequestMatchers.action(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(BotRequest botRequest, BotResponse botResponse, CommandChain chain) {
|
||||
if (matcher.match(botRequest)) {
|
||||
doProcess(botRequest, botResponse);
|
||||
}
|
||||
|
||||
chain.processCommand(botRequest, botResponse);
|
||||
}
|
||||
|
||||
protected abstract void doProcess(BotRequest botRequest, BotResponse botResponse);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.penkrat.stbf.common.command;
|
||||
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.BotRequest;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
import ru.penkrat.stbf.api.Command;
|
||||
import ru.penkrat.stbf.api.CommandChain;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
import ru.penkrat.stbf.common.screen.TextScreen;
|
||||
import ru.penkrat.stbf.tools.RequestMatchers;
|
||||
|
||||
public class SimpleCommand implements Command {
|
||||
|
||||
private final RequestMatcher matcher;
|
||||
private final Screen screen;
|
||||
|
||||
public SimpleCommand(String input, String output) {
|
||||
this(Action.simple(input), new TextScreen(output));
|
||||
}
|
||||
|
||||
public SimpleCommand(Action action, Screen screen) {
|
||||
this.screen = screen;
|
||||
matcher = RequestMatchers.action(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(BotRequest botRequest, BotResponse botResponse, CommandChain chain) {
|
||||
if (matcher.match(botRequest)) {
|
||||
botResponse.send(screen);
|
||||
}
|
||||
|
||||
chain.processCommand(botRequest, botResponse);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ru.penkrat.stbf.common.screen;
|
||||
|
||||
import lombok.Getter;
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
import ru.penkrat.stbf.api.KeyboardBuilder;
|
||||
|
||||
@Getter
|
||||
public class TextScreen implements Screen {
|
||||
|
||||
private final String text;
|
||||
|
||||
private final Keyboard keyboard;
|
||||
|
||||
public TextScreen(String text) {
|
||||
this.text = text;
|
||||
this.keyboard = null;
|
||||
}
|
||||
|
||||
public TextScreen(String text, Keyboard keyboard) {
|
||||
this.text = text;
|
||||
this.keyboard = keyboard;
|
||||
}
|
||||
|
||||
public TextScreen(String text, Action btn) {
|
||||
this(text, KeyboardBuilder.singleKey(btn));
|
||||
}
|
||||
|
||||
public TextScreen(String text, Action btn1, Action btn2) {
|
||||
this(text, KeyboardBuilder.newKeyboard().add(btn1).add(btn2).build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.penkrat.stbf.tools;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.RequestMatcher;
|
||||
|
||||
@UtilityClass
|
||||
public class RequestMatchers {
|
||||
|
||||
public RequestMatcher action(Action action) {
|
||||
return request -> request.getMessageText()
|
||||
.filter(text -> matchText(action, text))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
private static boolean matchText(Action action, String inputText) {
|
||||
return inputText.equalsIgnoreCase(action.getText())
|
||||
|| (action.getCmd() != null && inputText.equalsIgnoreCase(action.getCmd()));
|
||||
}
|
||||
}
|
||||
20
stbf-common/src/test/java/ru/penkrat/stbf/AppTest.java
Normal file
20
stbf-common/src/test/java/ru/penkrat/stbf/AppTest.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ru.penkrat.stbf;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user