-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TRAC-10] - Add telegram bot basic setup with hexagonal packages
- Loading branch information
1 parent
d5589b3
commit 80dadca
Showing
12 changed files
with
147 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
src/main/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
29 changes: 29 additions & 0 deletions
29
...main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/telegram/TelegramBotConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotLifecycleConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramBotSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/vladyslavpalamarchuk/trackmycoin/config/TelegramClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/vladyslavpalamarchuk/trackmycoin/domain/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/vladyslavpalamarchuk/trackmycoin/service/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 0 additions & 13 deletions
13
src/test/java/com/vladyslavpalamarchuk/trackmycoin/TrackmycoinApplicationTests.java
This file was deleted.
Oops, something went wrong.