48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
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;
|
|
|
|
/**
|
|
* NormalTextHandler
|
|
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
|
|
* @since 2021-08-21
|
|
*
|
|
*/
|
|
@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;
|
|
}
|
|
|
|
@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"));
|
|
}
|
|
}
|