Implement MessageHandler hierarchy
This commit is contained in:
parent
3866c20cea
commit
41bc9cad37
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<MessageHandler> messageHandlers) {
|
||||
return new MessageDispatcher(messageHandlers, BOT_TOKEN, BOT_NAME);
|
||||
}
|
||||
}
|
|
@ -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<MessageHandler> messageHandlers;
|
||||
private final String botToken;
|
||||
private final String botName;
|
||||
|
||||
public MessageDispatcher(List<MessageHandler> 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue