Implement TextCommandProcessor

This commit is contained in:
woo-jin.shin 2021-09-05 17:48:00 +09:00
parent ba371d161c
commit 3f912d8c84
5 changed files with 53 additions and 49 deletions

View File

@ -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 {

View File

@ -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());
}
}

View File

@ -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<TextCommandProcessor> processors;
public CommandHandler(List<TextCommandProcessor> 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"));
}
}

View File

@ -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<TextCommandProcessor> processors;
public CommonTextHandler(List<TextCommandProcessor> 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"));
}
}

View File

@ -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);
}