-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [TRAC-6] - Implement Message Service and basic Command Handling * [TRAC-6] - Implement Message Service and basic Command Handling * [TRAC-6] - Implement Message Service and basic Command Handling
- Loading branch information
1 parent
dd90567
commit d6ef3c0
Showing
14 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* This package contains persistence config for application | ||
* TODO delete this file after adding new files | ||
* This package contains persistence config for application TODO delete this file after adding new | ||
* files | ||
*/ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.persistence; |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import org.telegram.telegrambots.meta.generics.TelegramClient; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class TelegramBotClient { | ||
|
||
private final TelegramClient telegramClient; | ||
|
||
public void sendMessage(Long chatId, String messageText) { | ||
SendMessage message = SendMessage.builder().chatId(chatId).text(messageText).build(); | ||
try { | ||
telegramClient.execute(message); | ||
} catch (TelegramApiException e) { | ||
log.error("Fail to send telegram message to chatId: {}", chatId, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/vladyslavpalamarchuk/trackmycoin/command/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum Command { | ||
START("/start"), | ||
INFO("/info"), | ||
HELP("/help"), | ||
NON_COMMAND(""); | ||
|
||
private final String command; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/vladyslavpalamarchuk/trackmycoin/command/CommandProcessorRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.command.processor.CommandProcessor; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CommandProcessorRegistry { | ||
|
||
private final Map<Command, CommandProcessor> commandToProcessors; | ||
|
||
public CommandProcessorRegistry(List<CommandProcessor> processors) { | ||
commandToProcessors = | ||
processors.stream() | ||
.collect(Collectors.toMap(CommandProcessor::getCommand, Function.identity())); | ||
} | ||
|
||
public CommandProcessor get(String messageText) { | ||
if (!messageText.startsWith("/")) { | ||
return commandToProcessors.get(Command.NON_COMMAND); | ||
} | ||
|
||
return Arrays.stream(Command.values()) | ||
.filter(c -> c.getCommand().equalsIgnoreCase(messageText)) | ||
.findFirst() | ||
.map(commandToProcessors::get) | ||
.orElse(commandToProcessors.get(Command.NON_COMMAND)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/vladyslavpalamarchuk/trackmycoin/command/processor/CommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
public interface CommandProcessor { | ||
|
||
void process(Update update); | ||
|
||
Command getCommand(); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/vladyslavpalamarchuk/trackmycoin/command/processor/HelpCommandProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import com.vladyslavpalamarchuk.trackmycoin.config.TelegramBotDescription; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class HelpCommandProcess implements CommandProcessor { | ||
|
||
private final TelegramBotClient telegramBotClient; | ||
|
||
private final TelegramBotDescription telegramBotDescription; | ||
|
||
@Override | ||
public void process(Update update) { | ||
telegramBotClient.sendMessage( | ||
update.getMessage().getChatId(), telegramBotDescription.getHelp()); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
return Command.HELP; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ain/java/com/vladyslavpalamarchuk/trackmycoin/command/processor/InfoCommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import com.vladyslavpalamarchuk.trackmycoin.config.TelegramBotDescription; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class InfoCommandProcessor implements CommandProcessor { | ||
|
||
private final TelegramBotClient telegramBotClient; | ||
|
||
private final TelegramBotDescription telegramBotDescription; | ||
|
||
@Override | ||
public void process(Update update) { | ||
telegramBotClient.sendMessage( | ||
update.getMessage().getChatId(), telegramBotDescription.getInfo()); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
return Command.INFO; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...main/java/com/vladyslavpalamarchuk/trackmycoin/command/processor/NonCommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import com.vladyslavpalamarchuk.trackmycoin.config.TelegramBotDescription; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class NonCommandProcessor implements CommandProcessor { | ||
private final TelegramBotClient telegramBotClient; | ||
|
||
private final TelegramBotDescription telegramBotDescription; | ||
|
||
@Override | ||
public void process(Update update) { | ||
telegramBotClient.sendMessage( | ||
update.getMessage().getChatId(), telegramBotDescription.getNonCommand()); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
return Command.NON_COMMAND; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...in/java/com/vladyslavpalamarchuk/trackmycoin/command/processor/StartCommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import com.vladyslavpalamarchuk.trackmycoin.config.TelegramBotDescription; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class StartCommandProcessor implements CommandProcessor { | ||
|
||
private final TelegramBotClient telegramBotClient; | ||
|
||
private final TelegramBotDescription telegramBotDescription; | ||
|
||
@Override | ||
public void process(Update update) { | ||
telegramBotClient.sendMessage( | ||
update.getMessage().getChatId(), telegramBotDescription.getStart()); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
return Command.START; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix = "description") | ||
public class TelegramBotDescription { | ||
|
||
private String start; | ||
|
||
private String info; | ||
|
||
private String help; | ||
|
||
private String nonCommand; | ||
} |
5 changes: 1 addition & 4 deletions
5
src/main/java/com/vladyslavpalamarchuk/trackmycoin/domain/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
/** | ||
* This package contains domain entities | ||
* TODO delete this file after adding new files | ||
*/ | ||
/** This package contains domain entities TODO delete this file after adding new files */ | ||
package com.vladyslavpalamarchuk.trackmycoin.domain; |
4 changes: 2 additions & 2 deletions
4
src/main/java/com/vladyslavpalamarchuk/trackmycoin/service/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* This package contains business logic of the application | ||
* TODO delete this file after adding new files | ||
* This package contains business logic of the application TODO delete this file after adding new | ||
* files | ||
*/ | ||
package com.vladyslavpalamarchuk.trackmycoin.service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters