-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TRAC-6] Implement Message Service and basic Command Handling #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM maven:3.9.4-eclipse-temurin-21-alpine as build | ||
WORKDIR /app | ||
COPY pom.xml /app/ | ||
RUN mvn dependency:go-offline | ||
|
||
COPY src /app/src/ | ||
RUN mvn clean package -DskipTests | ||
RUN ls -al /app/target/ | ||
|
||
FROM openjdk:21-slim | ||
VOLUME /tmp | ||
COPY --from=build /app/target/*.jar /trackmycoin.jar | ||
|
||
ENTRYPOINT ["java", "-jar", "/trackmycoin.jar"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.generics.TelegramClient; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Getter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need this getter? |
||
public class TelegramBotClient { | ||
|
||
private final TelegramClient telegramClient; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.domain; | ||
|
||
import lombok.Getter; | ||
|
||
|
||
@Getter | ||
public enum Command { | ||
|
||
START("/start", "Welcome to TrackMyCoin 📈💰!\nThis bot helps you track cryptocurrency prices\nand will send you notifications when your target price \nis reached.\n\nFor more information about the bot, type /info."), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move all static values (text) to application.yml There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please create task in Jira to move this values from application.yml to database. It will increase flexibility in changes for us |
||
INFO("/info", "TrackMyCoin — your crypto assistant! 📈💰\n\nEasily track cryptocurrency prices and get notified\nwhen your target price is reached.\nPerfect for traders and crypto enthusiasts!\n\nType /help for a list of all commands."), | ||
HELP("/help", "List of commands 👾\nInfo -> /info\nHelp -> /help"), | ||
NON_COMMAND("","non command /help "); | ||
|
||
private final String command; | ||
private final String description; | ||
|
||
Command(String command, String description) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use @requiredargsconstructor instead of creating manually constructor |
||
this.command = command; | ||
this.description = description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.service; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.domain.Command; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class MessageHandlerService { | ||
|
||
@Autowired | ||
private TelegramBotClient telegramBotClient; | ||
|
||
private final List<Command> commands = List.of(Command.START , Command.INFO , Command.HELP); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use strategy pattern There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be like this |
||
|
||
public void processMessage(Long chatId, String messageText) { | ||
Optional<Command> commandOptional = commands.stream() | ||
.filter(c -> c.getCommand().equals(messageText)) | ||
.findFirst(); | ||
|
||
if (commandOptional.isPresent()) { | ||
sendWelcomeMessage(chatId, commandOptional.get().getDescription()); | ||
}else { | ||
sendWelcomeMessage(chatId, Command.NON_COMMAND.getDescription()); | ||
} | ||
} | ||
|
||
private void sendWelcomeMessage(Long chatId , String messageText) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is separate responsibility (SRP), not for MessageHandler |
||
SendMessage message = SendMessage | ||
.builder() | ||
.chatId(chatId) | ||
.text(messageText) | ||
.build(); | ||
try { | ||
telegramBotClient.getTelegramClient().execute(message); | ||
} catch (TelegramApiException e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never just print stack trace, you should use log |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be here