mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
refactor: improve bot configuration and service structure
This commit is contained in:
@@ -29,24 +29,26 @@ class BotConfiguration(
|
|||||||
fun telegramBot(): Bot {
|
fun telegramBot(): Bot {
|
||||||
return bot {
|
return bot {
|
||||||
token = botProps.token
|
token = botProps.token
|
||||||
dispatch {
|
setupDispatchers()
|
||||||
message {
|
}
|
||||||
withLoggingContext(messageContext(message)) {
|
}
|
||||||
try {
|
|
||||||
thisCommandHandlerService.create(this)
|
private fun Bot.Builder.setupDispatchers() = dispatch {
|
||||||
} catch (e: Error) {
|
message {
|
||||||
logger.error(e) { "Error while handling message" }
|
withLoggingContext(messageContext(message)) {
|
||||||
}
|
try {
|
||||||
}
|
thisCommandHandlerService.create(this)
|
||||||
|
} catch (e: Error) {
|
||||||
|
logger.error(e) { "Error while handling message" }
|
||||||
}
|
}
|
||||||
photos {
|
}
|
||||||
withLoggingContext(messageContext(message)) {
|
}
|
||||||
try {
|
photos {
|
||||||
photoHandlerService.create(this)
|
withLoggingContext(messageContext(message)) {
|
||||||
} catch (e: Error) {
|
try {
|
||||||
logger.error(e) { "Error while handling photo" }
|
photoHandlerService.create(this)
|
||||||
}
|
} catch (e: Error) {
|
||||||
}
|
logger.error(e) { "Error while handling photo" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user