From 3f912d8c846745decdab749a649b0ae0465ff493 Mon Sep 17 00:00:00 2001 From: "woo-jin.shin" Date: Sun, 5 Sep 2021 17:48:00 +0900 Subject: [PATCH] Implement TextCommandProcessor --- .../TelegramBotConfiguration.java | 4 +- .../message/text/AddCommandHandler.java | 29 ----------- .../handler/message/text/CommandHandler.java | 48 +++++++++++++++++++ .../message/text/CommonTextHandler.java | 18 +------ .../handler/message/text/TextCommandCode.java | 3 +- 5 files changed, 53 insertions(+), 49 deletions(-) delete mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/AddCommandHandler.java create mode 100644 receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommandHandler.java diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java index 458ced4..df3541c 100644 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/configuration/TelegramBotConfiguration.java @@ -20,8 +20,8 @@ import com.myoa.engineering.crawl.ppomppu.receiver.dispatch.MessageDispatcher; @Configuration public class TelegramBotConfiguration { - private static final String BOT_TOKEN = "bottoken"; // TODO extract to property - private static final String BOT_NAME = "nthfuncx_SoundHoundFoundBot"; // TODO extract to property + private static final String BOT_TOKEN = ""; // TODO extract to property + private static final String BOT_NAME = ""; // TODO extract to property @Bean public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException { diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/AddCommandHandler.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/AddCommandHandler.java deleted file mode 100644 index c82c74a..0000000 --- a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/AddCommandHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text; - -import org.springframework.stereotype.Component; -import org.telegram.telegrambots.meta.api.objects.Message; - -import com.myoa.engineering.crawl.ppomppu.receiver.handler.message.TextMessageHandler; - -import lombok.extern.slf4j.Slf4j; - -/** - * AddCommandHandler - * @author Shin Woo-jin (woo-jin.shin@linecorp.com) - * @since 2021-08-21 - * - */ -@Slf4j -@Component -public class AddCommandHandler implements TextMessageHandler { - - @Override - public boolean isApplicable(Message message) { - return TextMessageHandler.super.isApplicable(message) && message.getText().startsWith("/add"); - } - - @Override - public void handle(Message message) { - log.info("AddCommandHandler : {}", message.getText()); - } -} diff --git a/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommandHandler.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommandHandler.java new file mode 100644 index 0000000..3c22204 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommandHandler.java @@ -0,0 +1,48 @@ +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; + +import com.myoa.engineering.crawl.ppomppu.receiver.handler.message.TextMessageHandler; + +import lombok.extern.slf4j.Slf4j; + +/** + * CommandHandler + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-08-21 + * + */ +@Slf4j +@Component +public class CommandHandler implements TextMessageHandler { + + private final List processors; + + public CommandHandler(List processors) { + this.processors = processors; + } + + @Override + public boolean isApplicable(Message message) { + return TextMessageHandler.super.isApplicable(message) + && message.isCommand(); // && message.getText().startsWith("/"); + } + + @Override + public void handle(Message message) { + log.info("CommandHandler : {}", 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/CommonTextHandler.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/CommonTextHandler.java index 5904c52..04b04ce 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,7 +1,5 @@ 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; @@ -19,12 +17,6 @@ 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; @@ -33,15 +25,7 @@ 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/TextCommandCode.java b/receiver/src/main/java/com/myoa/engineering/crawl/ppomppu/receiver/handler/message/text/TextCommandCode.java index 7dfbd87..364d655 100644 --- 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 @@ -25,7 +25,8 @@ public enum TextCommandCode { public static TextCommandCode find(String value) { return Arrays.stream(TextCommandCode.values()) - .filter(e -> e.getValue().startsWith(value)) + .filter(e -> e != EMPTY) + .filter(e -> value.startsWith(e.getValue())) .findFirst() .orElse(TextCommandCode.EMPTY); }