initial commit

This commit is contained in:
2021-08-10 23:22:25 +03:00
commit 18465ada30
42 changed files with 1647 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package ru.penkrat.stbf.api;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
@Getter
@Builder
@ToString(of = "text")
public class Action {
public static Action simple(String text) {
return builder().text(text).build();
}
public static Action simple(String text, String cmd) {
validateCmd(cmd);
return builder().text(text).cmd(cmd).build();
}
public static Action callback(String text, String callbackData) {
return builder().inline(true).text(text).callbackData(callbackData).build();
}
public static Action requestContact(String text) {
return builder().text(text).requestContact(true).build();
}
private boolean inline;
private String text;
private String cmd;
// in-line
private String callbackData;
private String url;
// keyboard
private boolean requestContact;
private boolean requestLocation;
/**
* Text of the command, 1-32 characters. Can contain only lowercase English
* letters, digits and underscores.
*/
private static void validateCmd(@NonNull String cmd) {
if (cmd.length() > 32) {
throw new IllegalArgumentException("Max length - 32 characters");
}
if (!cmd.startsWith("/")) {
throw new IllegalArgumentException("Command must start / character");
}
// TODO validate lowercase etc
}
}

View File

@@ -0,0 +1,39 @@
package ru.penkrat.stbf.api;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class BotCommandChain implements CommandChain {
private final List<Command> commands;
public BotCommandChain() {
this(new ArrayList<>());
}
@Override
public void processCommand(BotRequest botRequest, BotResponse botResponse) {
if (!commands.isEmpty()) {
try {
Command command = commands.get(0);
log.debug("Run command {}", command.getClass().getSimpleName());
command.process(botRequest, botResponse,
new BotCommandChain(commands.subList(1, commands.size())));
} catch (Exception e) {
log.error("Error in command:", e);
}
}
}
public BotCommandChain add(Command cmd) {
commands.add(cmd);
return this;
}
}

View File

@@ -0,0 +1,17 @@
package ru.penkrat.stbf.api;
import java.util.Optional;
public interface BotRequest {
Optional<String> getMessageText();
Optional<String> getPhoneNumber();
Optional<String> getCallbackData();
Optional<String> getCallbackMessageText();
Long getChatId();
}

View File

@@ -0,0 +1,15 @@
package ru.penkrat.stbf.api;
public interface BotResponse {
void send(Screen screen);
void sendFile(String filename, byte[] data);
void editMessage(String text);
void deleteMessage();
void edit(Screen screen);
}

View File

@@ -0,0 +1,7 @@
package ru.penkrat.stbf.api;
public interface Command {
void process(BotRequest botRequest, BotResponse botResponse, CommandChain chain);
}

View File

@@ -0,0 +1,7 @@
package ru.penkrat.stbf.api;
public interface CommandChain {
void processCommand(BotRequest botRequest, BotResponse botResponse);
}

View File

@@ -0,0 +1,5 @@
package ru.penkrat.stbf.api;
public interface Keyboard {
}

View File

@@ -0,0 +1,33 @@
package ru.penkrat.stbf.api;
import java.util.ServiceLoader;
public interface KeyboardBuilder {
ServiceLoader<KeyboardBuilder> keyboardBuilderLoader = ServiceLoader.load(KeyboardBuilder.class);
public static KeyboardBuilder newKeyboard() {
for (KeyboardBuilder kb : keyboardBuilderLoader) {
return kb.newInstance();
}
throw new IllegalStateException("No service KeyboardBuilder found");
}
KeyboardBuilder newInstance();
public static Keyboard singleKey(Action action) {
return newKeyboard().add(action).build();
}
default KeyboardBuilder add(String text, String callbackData) {
return add(Action.callback(text, callbackData));
}
KeyboardBuilder add(Action action);
KeyboardBuilder row(Action... buttons);
KeyboardBuilder column(Action... buttons);
Keyboard build();
}

View File

@@ -0,0 +1,8 @@
package ru.penkrat.stbf.api;
@FunctionalInterface
public interface RequestMatcher {
boolean match(BotRequest botRequest);
}

View File

@@ -0,0 +1,14 @@
package ru.penkrat.stbf.api;
public interface Screen {
String getText();
default Keyboard getKeyboard() {
return null;
}
default ScreenProperties getScreenProperties() {
return ScreenProperties.DEFAULT;
}
}

View File

@@ -0,0 +1,23 @@
package ru.penkrat.stbf.api;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Getter
@Setter
@Accessors(chain = true)
public class ScreenProperties {
public static final ScreenProperties DEFAULT = new ScreenProperties()
.setDisableWebPagePreview(true)
.setDisableNotification(true)
.setParseModeHtml(true);
private boolean disableWebPagePreview;
private boolean disableNotification;
private boolean parseModeHtml;
}

View File

@@ -0,0 +1 @@
ru.penkrat.stbf.impl.pengrad.PengradKeyboardBuilder