Implement TextCommandProcessor

This commit is contained in:
woo-jin.shin 2021-09-05 17:29:13 +09:00
parent ff9b7bf85b
commit ba371d161c
5 changed files with 118 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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