From 80dadca9074d85ec32ddac523304239a65bcf1af Mon Sep 17 00:00:00 2001 From: VladyslavPalamarchuk <65964034+VladyslavPalamarchuk@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:38:15 +0200 Subject: [PATCH] [TRAC-10] - Add telegram bot basic setup with hexagonal packages --- pom.xml | 19 ++++++++++-- .../trackmycoin/TrackmycoinApplication.java | 10 ++++--- .../adaptors/persistence/package-info.java | 5 ++++ .../adaptors/telegram/TelegramBotClient.java | 12 ++++++++ .../telegram/TelegramBotConsumer.java | 29 +++++++++++++++++++ .../config/TelegramBotLifecycleConfig.java | 29 +++++++++++++++++++ .../config/TelegramBotSettings.java | 11 +++++++ .../config/TelegramClientConfig.java | 18 ++++++++++++ .../trackmycoin/domain/package-info.java | 5 ++++ .../trackmycoin/service/package-info.java | 5 ++++ src/main/resources/application.yml | 11 ++++++- .../TrackmycoinApplicationTests.java | 13 --------- 12 files changed, 147 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotClient.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotConsumer.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotLifecycleConfig.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotSettings.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramClientConfig.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/domain/package-info.java create mode 100644 src/main/java/com/vladyslavpalamarchuk/trackmycoin/service/package-info.java delete mode 100644 src/test/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplicationTests.java diff --git a/pom.xml b/pom.xml index add2977..63f8dd2 100644 --- a/pom.xml +++ b/pom.xml @@ -17,19 +17,34 @@ 21 + 7.10.0 + 7.10.0 org.springframework.boot - spring-boot-starter + spring-boot-starter-data-jpa + + + org.telegram + telegrambots-longpolling + ${telegrambots-longpolling.version} + + + org.telegram + telegrambots-client + ${telegrambots-client.version} - org.projectlombok lombok true + + org.postgresql + postgresql + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplication.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplication.java index 3fb5089..1f95d9d 100644 --- a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplication.java +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplication.java @@ -1,13 +1,15 @@ package com.vladyslavpalamarchuk.trackmycoin; +import com.vladyslavpalamarchuk.trackmycoin.config.TelegramBotSettings; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication +@EnableConfigurationProperties(TelegramBotSettings.class) public class TrackmycoinApplication { - public static void main(String[] args) { - SpringApplication.run(TrackmycoinApplication.class, args); - } - + public static void main(String[] args) { + SpringApplication.run(TrackmycoinApplication.class, args); + } } diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java new file mode 100644 index 0000000..932fb71 --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains persistence config for application + * TODO delete this file after adding new files + */ +package com.vladyslavpalamarchuk.trackmycoin.adaptors.persistence; diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotClient.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotClient.java new file mode 100644 index 0000000..0ba84d4 --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotClient.java @@ -0,0 +1,12 @@ +package com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.telegram.telegrambots.meta.generics.TelegramClient; + +@Component +@RequiredArgsConstructor +public class TelegramBotClient { + + private final TelegramClient telegramClient; +} diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotConsumer.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotConsumer.java new file mode 100644 index 0000000..bfd234b --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotConsumer.java @@ -0,0 +1,29 @@ +package com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer; +import org.telegram.telegrambots.meta.api.objects.Update; +import org.telegram.telegrambots.meta.api.objects.message.Message; + +@Slf4j +@Component +@RequiredArgsConstructor +public class TelegramBotConsumer implements LongPollingSingleThreadUpdateConsumer { + + @Override + public void consume(Update update) { + if (update.hasMessage() && update.getMessage().hasText()) { + logUpdateMessage(update.getMessage()); + } + } + + private void logUpdateMessage(Message message) { + log.debug( + "New message: {} from username: {} chatId: {}", + message.getText(), + message.getFrom().getUserName(), + message.getChatId()); + } +} diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotLifecycleConfig.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotLifecycleConfig.java new file mode 100644 index 0000000..76be2d9 --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotLifecycleConfig.java @@ -0,0 +1,29 @@ +package com.vladyslavpalamarchuk.trackmycoin.config; + +import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotConsumer; +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication; + +@Configuration +@RequiredArgsConstructor +public class TelegramBotLifecycleConfig { + + private final TelegramBotConsumer telegramBotConsumer; + + private final TelegramBotSettings telegramBotSettings; + + @Bean + @DependsOn("telegramBotConsumer") + public TelegramBotsLongPollingApplication telegramBotApplication() { + try { + TelegramBotsLongPollingApplication application = new TelegramBotsLongPollingApplication(); + application.registerBot(telegramBotSettings.getToken(), telegramBotConsumer); + return application; + } catch (Exception e) { + throw new IllegalStateException("Fail to register telegram bot", e); + } + } +} diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotSettings.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotSettings.java new file mode 100644 index 0000000..cc72f20 --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotSettings.java @@ -0,0 +1,11 @@ +package com.vladyslavpalamarchuk.trackmycoin.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Data +@ConfigurationProperties(prefix = "telegram.bot") +public class TelegramBotSettings { + + private String token; +} diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramClientConfig.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramClientConfig.java new file mode 100644 index 0000000..0edcb14 --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramClientConfig.java @@ -0,0 +1,18 @@ +package com.vladyslavpalamarchuk.trackmycoin.config; + +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient; + +@Configuration +@RequiredArgsConstructor +public class TelegramClientConfig { + + private final TelegramBotSettings telegramBotSettings; + + @Bean + public OkHttpTelegramClient telegramClient() { + return new OkHttpTelegramClient(telegramBotSettings.getToken()); + } +} diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/domain/package-info.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/domain/package-info.java new file mode 100644 index 0000000..38fff6f --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/domain/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains domain entities + * TODO delete this file after adding new files + */ +package com.vladyslavpalamarchuk.trackmycoin.domain; diff --git a/src/main/java/com/vladyslavpalamarchuk/trackmycoin/service/package-info.java b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/service/package-info.java new file mode 100644 index 0000000..7d59657 --- /dev/null +++ b/src/main/java/com/vladyslavpalamarchuk/trackmycoin/service/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains business logic of the application + * TODO delete this file after adding new files + */ +package com.vladyslavpalamarchuk.trackmycoin.service; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a9dae42..31b5174 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1 +1,10 @@ -spring.application.name=trackmycoin +telegram: + bot: + token: ${TELEGRAM_BOT_TOKEN} + +spring: + datasource: + url: ${POSTGRESQL_JDBC_URL} + username: ${POSTGRESQL_USERNAME} + password: ${POSTGRESQL_PASSWORD} + driver-class-name: org.postgresql.Driver \ No newline at end of file diff --git a/src/test/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplicationTests.java b/src/test/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplicationTests.java deleted file mode 100644 index eb68bd8..0000000 --- a/src/test/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.vladyslavpalamarchuk.trackmycoin; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class TrackmycoinApplicationTests { - - @Test - void contextLoads() { - } - -}