refactor: improve bot configuration and service structure

This commit is contained in:
2025-05-29 15:02:59 +03:00
committed by Maksim Pischulenok
parent b096722510
commit c8cd44224b
2 changed files with 29 additions and 20 deletions

View File

@@ -29,7 +29,11 @@ class BotConfiguration(
fun telegramBot(): Bot { fun telegramBot(): Bot {
return bot { return bot {
token = botProps.token token = botProps.token
dispatch { setupDispatchers()
}
}
private fun Bot.Builder.setupDispatchers() = dispatch {
message { message {
withLoggingContext(messageContext(message)) { withLoggingContext(messageContext(message)) {
try { try {
@@ -49,8 +53,6 @@ class BotConfiguration(
} }
} }
} }
}
}
private fun messageContext(message: Message): Map<String, String?> = private fun messageContext(message: Message): Map<String, String?> =
mapOf( mapOf(

View File

@@ -10,16 +10,23 @@ private val logger = KotlinLogging.logger {}
@Service @Service
class BotService(private val bot: Bot) { class BotService(private val bot: Bot) {
private var pollingThread: Thread? = null
@PostConstruct @PostConstruct
fun start() { fun start() {
Thread { bot.startPolling() }.start() pollingThread =
logger.info { "Initialized bot" } Thread { bot.startPolling() }
.apply {
name = "telegram-bot-polling"
start()
}
logger.info { "Bot service started" }
} }
@PreDestroy @PreDestroy
fun stop() { fun stop() {
bot.stopPolling() bot.stopPolling()
logger.info { "Stopped bot" } pollingThread?.join(1000)
logger.info { "Bot service stopped" }
} }
} }