From ba371d161c97be123ce6a1c13104105de033da0d Mon Sep 17 00:00:00 2001 From: "woo-jin.shin" Date: Sun, 5 Sep 2021 17:29:13 +0900 Subject: [PATCH] Implement TextCommandProcessor --- .../message/text/CommonTextHandler.java | 18 +++++++++++ .../text/EmptyTextCommandProcessor.java | 24 ++++++++++++++ .../text/StartTextCommandProcessor.java | 27 ++++++++++++++++ .../handler/message/text/TextCommandCode.java | 32 +++++++++++++++++++ .../message/text/TextCommandProcessor.java | 17 ++++++++++ 5 files changed, 118 insertions(+) create mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/EmptyTextCommandProcessor.java create mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/StartTextCommandProcessor.java create mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandCode.java create mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandProcessor.java diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommonTextHandler.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommonTextHandler.java index 8eda43f..5904c52 100644 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommonTextHandler.java +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommonTextHandler.java @@ -1,5 +1,7 @@ package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text; +import java.util.List; + import org.springframework.stereotype.Component; import org.telegram.telegrambots.meta.api.objects.Message; @@ -17,6 +19,12 @@ import lombok.extern.slf4j.Slf4j; @Component public class CommonTextHandler implements TextMessageHandler { + private final List processors; + + public CommonTextHandler(List processors) { + this.processors = processors; + } + @Override public boolean isApplicable(Message message) { return TextMessageHandler.super.isApplicable(message) && message.isCommand() == false; @@ -25,5 +33,15 @@ public class CommonTextHandler implements TextMessageHandler { @Override public void handle(Message message) { log.info("CommonTextHandler : {}", message.getText()); + TextCommandCode commandCode = TextCommandCode.find(message.getText()); + TextCommandProcessor applicableProcessor = getApplicableProcessor(commandCode); + applicableProcessor.process(message); + } + + private TextCommandProcessor getApplicableProcessor(TextCommandCode commandCode) { + return processors.stream() + .filter(e -> e.isApplicable(commandCode)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Can not found")); } } diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/EmptyTextCommandProcessor.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/EmptyTextCommandProcessor.java new file mode 100644 index 0000000..e0c4449 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/EmptyTextCommandProcessor.java @@ -0,0 +1,24 @@ +package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text; + +import org.springframework.stereotype.Component; +import org.telegram.telegrambots.meta.api.objects.Message; + +/** + * EmptyTextCommandProcessor + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-09-05 + * + */ +@Component +public class EmptyTextCommandProcessor implements TextCommandProcessor { + + @Override + public boolean isApplicable(TextCommandCode commandCode) { + return commandCode == TextCommandCode.EMPTY; + } + + @Override + public void process(Message message) { + + } +} diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/StartTextCommandProcessor.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/StartTextCommandProcessor.java new file mode 100644 index 0000000..08c8315 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/StartTextCommandProcessor.java @@ -0,0 +1,27 @@ +package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text; + +import org.springframework.stereotype.Component; +import org.telegram.telegrambots.meta.api.objects.Message; + +import lombok.extern.slf4j.Slf4j; + +/** + * StartCommandProcessor + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-09-05 + * + */ +@Slf4j +@Component +public class StartTextCommandProcessor implements TextCommandProcessor { + + @Override + public boolean isApplicable(TextCommandCode commandCode) { + return TextCommandCode.START == commandCode; + } + + @Override + public void process(Message message) { + log.info("[process] user: {}, command: {}", message.getChatId(), message.getText()); + } +} diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandCode.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandCode.java new file mode 100644 index 0000000..7dfbd87 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandCode.java @@ -0,0 +1,32 @@ +package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text; + +import java.util.Arrays; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * CommandTextCode + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-09-05 + * + */ +@Getter +@NoArgsConstructor +public enum TextCommandCode { + EMPTY(null), + START("/start"), + ; + private String value; + + TextCommandCode(String value) { + this.value = value; + } + + public static TextCommandCode find(String value) { + return Arrays.stream(TextCommandCode.values()) + .filter(e -> e.getValue().startsWith(value)) + .findFirst() + .orElse(TextCommandCode.EMPTY); + } +} diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandProcessor.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandProcessor.java new file mode 100644 index 0000000..3f8ff49 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandProcessor.java @@ -0,0 +1,17 @@ +package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text; + +import org.telegram.telegrambots.meta.api.objects.Message; + +/** + * TextCommandProcessor + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-09-05 + * + */ +public interface TextCommandProcessor { + + boolean isApplicable(TextCommandCode commandCode); + + void process(Message message); + +}