Skip to content

Commit

Permalink
[TRAC-10] - Add telegram bot basic setup with hexagonal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
VladyslavPalamarchuk authored Nov 7, 2024
1 parent d5589b3 commit 80dadca
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 20 deletions.
19 changes: 17 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,34 @@

<properties>
<java.version>21</java.version>
<telegrambots-longpolling.version>7.10.0</telegrambots-longpolling.version>
<telegrambots-client.version>7.10.0</telegrambots-client.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-longpolling</artifactId>
<version>${telegrambots-longpolling.version}</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-client</artifactId>
<version>${telegrambots-client.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* 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
@@ -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;
11 changes: 10 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

0 comments on commit 80dadca

Please sign in to comment.