diff --git a/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt b/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt index 1ef0db0..276e116 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt @@ -29,24 +29,26 @@ class BotConfiguration( fun telegramBot(): Bot { return bot { token = botProps.token - dispatch { - message { - withLoggingContext(messageContext(message)) { - try { - thisCommandHandlerService.create(this) - } catch (e: Error) { - logger.error(e) { "Error while handling message" } - } - } + setupDispatchers() + } + } + + private fun Bot.Builder.setupDispatchers() = dispatch { + message { + withLoggingContext(messageContext(message)) { + try { + thisCommandHandlerService.create(this) + } catch (e: Error) { + logger.error(e) { "Error while handling message" } } - photos { - withLoggingContext(messageContext(message)) { - try { - photoHandlerService.create(this) - } catch (e: Error) { - logger.error(e) { "Error while handling photo" } - } - } + } + } + photos { + withLoggingContext(messageContext(message)) { + try { + photoHandlerService.create(this) + } catch (e: Error) { + logger.error(e) { "Error while handling photo" } } } } diff --git a/src/main/kotlin/com/pischule/memestv/bot/BotService.kt b/src/main/kotlin/com/pischule/memestv/bot/BotService.kt index 67047c1..365e0f6 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/BotService.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/BotService.kt @@ -10,16 +10,23 @@ private val logger = KotlinLogging.logger {} @Service class BotService(private val bot: Bot) { + private var pollingThread: Thread? = null @PostConstruct fun start() { - Thread { bot.startPolling() }.start() - logger.info { "Initialized bot" } + pollingThread = + Thread { bot.startPolling() } + .apply { + name = "telegram-bot-polling" + start() + } + logger.info { "Bot service started" } } @PreDestroy fun stop() { bot.stopPolling() - logger.info { "Stopped bot" } + pollingThread?.join(1000) + logger.info { "Bot service stopped" } } }