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

feature/TRAC-6 #8

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 sendWelcomeMessage(Long chatId, String messageText) {
VladyslavPalamarchuk marked this conversation as resolved.
Show resolved Hide resolved
SendMessage message = SendMessage.builder().chatId(chatId).text(messageText).build();
try {
telegramClient.execute(message);
} catch (TelegramApiException e) {
log.error(e.getMessage());
VladyslavPalamarchuk marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram;

import com.vladyslavpalamarchuk.trackmycoin.command.CommandProcessorRegistry;
import com.vladyslavpalamarchuk.trackmycoin.command.processor.CommandProcessor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
Expand All @@ -12,9 +14,13 @@
@RequiredArgsConstructor
public class TelegramBotConsumer implements LongPollingSingleThreadUpdateConsumer {

private final CommandProcessorRegistry commandProcessorRegistry;

@Override
public void consume(Update update) {
if (update.hasMessage() && update.getMessage().hasText()) {
CommandProcessor processMessage = commandProcessorRegistry.get(update.getMessage().getText());
VladyslavPalamarchuk marked this conversation as resolved.
Show resolved Hide resolved
processMessage.process(update);
logUpdateMessage(update.getMessage());
VladyslavPalamarchuk marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
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;
}
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));
}
}
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();
}
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.sendWelcomeMessage(
VladyslavPalamarchuk marked this conversation as resolved.
Show resolved Hide resolved
update.getMessage().getChatId(), telegramBotDescription.getHelp());
}

@Override
public Command getCommand() {
return Command.HELP;
}
}
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.sendWelcomeMessage(
update.getMessage().getChatId(), telegramBotDescription.getInfo());
}

@Override
public Command getCommand() {
return Command.INFO;
}
}
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.sendWelcomeMessage(
update.getMessage().getChatId(), telegramBotDescription.getNon_command());
}

@Override
public Command getCommand() {
return Command.NON_COMMAND;
}
}
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.sendWelcomeMessage(
update.getMessage().getChatId(), telegramBotDescription.getStart());
}

@Override
public Command getCommand() {
return Command.START;
}
}
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 non_command;
VladyslavPalamarchuk marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 16 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,19 @@ spring:
url: ${POSTGRESQL_JDBC_URL}
username: ${POSTGRESQL_USERNAME}
password: ${POSTGRESQL_PASSWORD}
driver-class-name: org.postgresql.Driver
driver-class-name: org.postgresql.Driver
description:
start: "Welcome to TrackMyCoin 📈💰!
\n\nThis bot helps you track cryptocurrency prices
and will send you notifications when your target price
is reached.
\n\nFor more information about the bot, type /info."
info: "TrackMyCoin — your crypto assistant 📈💰!

\nEasily track cryptocurrency prices and get notified
when your target price is reached.
\nPerfect for traders and crypto enthusiasts!

\nType /help for a list of all commands."
help: "List of commands 👾\nInfo -> /info\nHelp -> /help"
non_command: "non command /help "
Loading