From 41bc9cad37f7b5dc23c2ca8614196e7fcb236d62 Mon Sep 17 00:00:00 2001 From: woo-jin-shin Date: Sat, 21 Aug 2021 00:45:18 +0900 Subject: [PATCH] Implement MessageHandler hierarchy --- .../receiver/ReceiverApplication.java | 10 ---- .../TelegramBotConfiguration.java | 37 ++++++++++++++ .../receiver/dispatch/MessageDispatcher.java | 51 +++++++++++++++++++ .../receiver/dispatch/MessageHandler.java | 29 ----------- .../handler/message/ImageMessageHandler.java | 12 +++++ .../handler/message/MessageHandler.java | 17 +++++++ .../handler/message/TextMessageHandler.java | 17 +++++++ .../message/text/AddCommandHandler.java | 29 +++++++++++ .../message/text/CommonTextHandler.java | 29 +++++++++++ 9 files changed, 192 insertions(+), 39 deletions(-) create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/configuration/TelegramBotConfiguration.java create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageDispatcher.java delete mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageHandler.java create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/ImageMessageHandler.java create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/MessageHandler.java create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/TextMessageHandler.java create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/AddCommandHandler.java create mode 100644 receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/CommonTextHandler.java diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/ReceiverApplication.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/ReceiverApplication.java index 5c49c76..f7c1610 100644 --- a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/ReceiverApplication.java +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/ReceiverApplication.java @@ -1,11 +1,7 @@ package com.myoa.engineering.music.soundhoundfound.receiver; -import com.myoa.engineering.music.soundhoundfound.receiver.dispatch.MessageHandler; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.telegram.telegrambots.meta.TelegramBotsApi; -import org.telegram.telegrambots.meta.exceptions.TelegramApiException; -import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; /** * ReceiverApplication @@ -18,11 +14,5 @@ public class ReceiverApplication { public static void main(String[] args) { SpringApplication.run(ReceiverApplication.class, args); - try { - TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class); - api.registerBot(new MessageHandler()); - } catch(TelegramApiException e) { - e.printStackTrace(); - } } } diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/configuration/TelegramBotConfiguration.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/configuration/TelegramBotConfiguration.java new file mode 100644 index 0000000..78b40f6 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/configuration/TelegramBotConfiguration.java @@ -0,0 +1,37 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.configuration; + +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.telegram.telegrambots.meta.TelegramBotsApi; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; + +import com.myoa.engineering.music.soundhoundfound.receiver.dispatch.MessageDispatcher; +import com.myoa.engineering.music.soundhoundfound.receiver.handler.message.MessageHandler; + +/** + * TelegramBotConfiguration + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-08-21 + * + */ +@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 + + @Bean + public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException { + TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class); + api.registerBot(messageDispatcher); + return api; + } + + @Bean + public MessageDispatcher messageDispatcher(List messageHandlers) { + return new MessageDispatcher(messageHandlers, BOT_TOKEN, BOT_NAME); + } +} diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageDispatcher.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageDispatcher.java new file mode 100644 index 0000000..98c8494 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageDispatcher.java @@ -0,0 +1,51 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.dispatch; + +import java.util.List; + +import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.meta.api.objects.Update; + +import com.myoa.engineering.music.soundhoundfound.receiver.handler.message.MessageHandler; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class MessageDispatcher extends TelegramLongPollingBot { + + private final List messageHandlers; + private final String botToken; + private final String botName; + + public MessageDispatcher(List messageHandlers, String botToken, String botName) { + this.messageHandlers = messageHandlers; + this.botToken = botToken; + this.botName = botName; + } + + @Override + public String getBotToken() { + return botToken; + } + + @Override + public void onUpdateReceived(Update update) { + + Message message = update.getMessage(); + MessageHandler handler = getMessageHandler(message); + log.info(message.getText()); + handler.handle(message); + } + + private MessageHandler getMessageHandler(Message message) { + return messageHandlers.stream() + .filter(e -> e.isApplicable(message)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Can not found applicable handler")); + } + + @Override + public String getBotUsername() { + return botName; + } +} diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageHandler.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageHandler.java deleted file mode 100644 index a978f85..0000000 --- a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/dispatch/MessageHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.myoa.engineering.music.soundhoundfound.receiver.dispatch; - -import lombok.extern.slf4j.Slf4j; -import org.telegram.telegrambots.bots.TelegramLongPollingBot; -import org.telegram.telegrambots.meta.api.objects.Message; -import org.telegram.telegrambots.meta.api.objects.Update; - -@Slf4j -public class MessageHandler extends TelegramLongPollingBot { - - private static final String BOT_TOKEN = "bottoken"; - private static final String BOT_NAME = "nthfuncx_SoundHoundFoundBot"; - - @Override - public String getBotToken() { - return BOT_TOKEN; - } - - @Override - public void onUpdateReceived(Update update) { - Message message = update.getMessage(); - log.info(message.getText()); - } - - @Override - public String getBotUsername() { - return BOT_NAME; - } -} diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/ImageMessageHandler.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/ImageMessageHandler.java new file mode 100644 index 0000000..10e8e13 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/ImageMessageHandler.java @@ -0,0 +1,12 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.handler.message; + +/** + * ImageHandler + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-08-21 + * + */ +public interface ImageMessageHandler extends MessageHandler { + + +} diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/MessageHandler.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/MessageHandler.java new file mode 100644 index 0000000..cdb79e2 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/MessageHandler.java @@ -0,0 +1,17 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.handler.message; + +import org.telegram.telegrambots.meta.api.objects.Message; + +/** + * MessageHandler + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-08-21 + * + */ +public interface MessageHandler { + + boolean isApplicable(Message message); + + void handle(Message message); + +} diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/TextMessageHandler.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/TextMessageHandler.java new file mode 100644 index 0000000..cefc96a --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/TextMessageHandler.java @@ -0,0 +1,17 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.handler.message; + +import org.telegram.telegrambots.meta.api.objects.Message; + +/** + * TextMessageHandler + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-08-21 + * + */ +public interface TextMessageHandler extends MessageHandler { + + @Override + default boolean isApplicable(Message message) { + return message.isUserMessage() && message.hasText(); + } +} diff --git a/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/AddCommandHandler.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/AddCommandHandler.java new file mode 100644 index 0000000..83031f4 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/AddCommandHandler.java @@ -0,0 +1,29 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.handler.message.text; + +import org.springframework.stereotype.Component; +import org.telegram.telegrambots.meta.api.objects.Message; + +import com.myoa.engineering.music.soundhoundfound.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/music/soundhoundfound/receiver/handler/message/text/CommonTextHandler.java b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/CommonTextHandler.java new file mode 100644 index 0000000..db82d70 --- /dev/null +++ b/receiver/src/main/java/com/myoa/engineering/music/soundhoundfound/receiver/handler/message/text/CommonTextHandler.java @@ -0,0 +1,29 @@ +package com.myoa.engineering.music.soundhoundfound.receiver.handler.message.text; + +import org.springframework.stereotype.Component; +import org.telegram.telegrambots.meta.api.objects.Message; + +import com.myoa.engineering.music.soundhoundfound.receiver.handler.message.TextMessageHandler; + +import lombok.extern.slf4j.Slf4j; + +/** + * NormalTextHandler + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-08-21 + * + */ +@Slf4j +@Component +public class CommonTextHandler implements TextMessageHandler { + + @Override + public boolean isApplicable(Message message) { + return TextMessageHandler.super.isApplicable(message) && message.isCommand() == false; + } + + @Override + public void handle(Message message) { + log.info("CommonTextHandler : {}", message.getText()); + } +}