Implement TextCommandProcessor
This commit is contained in:
parent
ba371d161c
commit
3f912d8c84
|
@ -20,8 +20,8 @@ import com.myoa.engineering.crawl.ppomppu.receiver.dispatch.MessageDispatcher;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class TelegramBotConfiguration {
|
public class TelegramBotConfiguration {
|
||||||
|
|
||||||
private static final String BOT_TOKEN = "bottoken"; // TODO extract to property
|
private static final String BOT_TOKEN = ""; // TODO extract to property
|
||||||
private static final String BOT_NAME = "nthfuncx_SoundHoundFoundBot"; // TODO extract to property
|
private static final String BOT_NAME = ""; // TODO extract to property
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException {
|
public TelegramBotsApi telegramBotsApi(MessageDispatcher messageDispatcher) throws TelegramApiException {
|
||||||
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,5 @@
|
||||||
package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text;
|
package com.myoa.engineering.crawl.ppomppu.receiver.handler.message.text;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Message;
|
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||||
|
|
||||||
|
@ -19,12 +17,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
@Component
|
@Component
|
||||||
public class CommonTextHandler implements TextMessageHandler {
|
public class CommonTextHandler implements TextMessageHandler {
|
||||||
|
|
||||||
private final List<TextCommandProcessor> processors;
|
|
||||||
|
|
||||||
public CommonTextHandler(List<TextCommandProcessor> processors) {
|
|
||||||
this.processors = processors;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isApplicable(Message message) {
|
public boolean isApplicable(Message message) {
|
||||||
return TextMessageHandler.super.isApplicable(message) && message.isCommand() == false;
|
return TextMessageHandler.super.isApplicable(message) && message.isCommand() == false;
|
||||||
|
@ -33,15 +25,7 @@ public class CommonTextHandler implements TextMessageHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(Message message) {
|
public void handle(Message message) {
|
||||||
log.info("CommonTextHandler : {}", message.getText());
|
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"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,8 @@ public enum TextCommandCode {
|
||||||
|
|
||||||
public static TextCommandCode find(String value) {
|
public static TextCommandCode find(String value) {
|
||||||
return Arrays.stream(TextCommandCode.values())
|
return Arrays.stream(TextCommandCode.values())
|
||||||
.filter(e -> e.getValue().startsWith(value))
|
.filter(e -> e != EMPTY)
|
||||||
|
.filter(e -> value.startsWith(e.getValue()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(TextCommandCode.EMPTY);
|
.orElse(TextCommandCode.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue