Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Dockerfile
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be here

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

Choose a reason for hiding this comment

The 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram;

import com.vladyslavpalamarchuk.trackmycoin.service.MessageHandlerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand All @@ -12,9 +14,13 @@
@RequiredArgsConstructor
public class TelegramBotConsumer implements LongPollingSingleThreadUpdateConsumer {

@Autowired
private final MessageHandlerService messageHandlerService;

@Override
public void consume(Update update) {
if (update.hasMessage() && update.getMessage().hasText()) {
messageHandlerService.processMessage(update.getMessage().getChatId(), update.getMessage().getText() );
logUpdateMessage(update.getMessage());
}
}
Expand Down
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."),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move all static values (text) to application.yml

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use strategy pattern

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be like this
commandProcessorsRegistry.getProcessor(Command).process(chatId)


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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is separate responsibility (SRP), not for MessageHandler
Move this method to telegramBotClient

SendMessage message = SendMessage
.builder()
.chatId(chatId)
.text(messageText)
.build();
try {
telegramBotClient.getTelegramClient().execute(message);
} catch (TelegramApiException e) {
e.printStackTrace();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never just print stack trace, you should use log

}
}
}
Loading