Skip to content

Commit

Permalink
[TRAC-6] - Implement Message Service and basic Command Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrRym committed Nov 20, 2024
1 parent bdb116f commit 359c177
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 22 deletions.
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;
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class TelegramBotClient {

private final TelegramClient telegramClient;

public void sendWelcomeMessage(Long chatId, String messageText) {
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(e.getMessage());
log.error("Fail to send telegram message to chatId: {}", chatId, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class TelegramBotConsumer implements LongPollingSingleThreadUpdateConsume
@Override
public void consume(Update update) {
if (update.hasMessage() && update.getMessage().hasText()) {
CommandProcessor processMessage = commandProcessorRegistry.get(update.getMessage().getText());
processMessage.process(update);
CommandProcessor commandProcessor =
commandProcessorRegistry.get(update.getMessage().getText());
commandProcessor.process(update);
logUpdateMessage(update.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public CommandProcessor get(String messageText) {
}

return Arrays.stream(Command.values())
.filter(c -> c.getCommand().equalsIgnoreCase(messageText))
.findFirst()
.map(commandToProcessors::get)
.orElse(commandToProcessors.get(Command.NON_COMMAND));
.filter(c -> c.getCommand().equalsIgnoreCase(messageText))
.findFirst()
.map(commandToProcessors::get)
.orElse(commandToProcessors.get(Command.NON_COMMAND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HelpCommandProcess implements CommandProcessor {

@Override
public void process(Update update) {
telegramBotClient.sendWelcomeMessage(
telegramBotClient.sendMessage(
update.getMessage().getChatId(), telegramBotDescription.getHelp());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class InfoCommandProcessor implements CommandProcessor {

@Override
public void process(Update update) {
telegramBotClient.sendWelcomeMessage(
telegramBotClient.sendMessage(
update.getMessage().getChatId(), telegramBotDescription.getInfo());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class NonCommandProcessor implements CommandProcessor {

@Override
public void process(Update update) {
telegramBotClient.sendWelcomeMessage(
update.getMessage().getChatId(), telegramBotDescription.getNon_command());
telegramBotClient.sendMessage(
update.getMessage().getChatId(), telegramBotDescription.getNonCommand());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class StartCommandProcessor implements CommandProcessor {

@Override
public void process(Update update) {
telegramBotClient.sendWelcomeMessage(
telegramBotClient.sendMessage(
update.getMessage().getChatId(), telegramBotDescription.getStart());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public class TelegramBotDescription {

private String help;

private String non_command;
private String nonCommand;
}
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;
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;

0 comments on commit 359c177

Please sign in to comment.