mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
Refactor logging
This commit is contained in:
@@ -5,15 +5,17 @@ import com.github.kotlintelegrambot.bot
|
|||||||
import com.github.kotlintelegrambot.dispatch
|
import com.github.kotlintelegrambot.dispatch
|
||||||
import com.github.kotlintelegrambot.dispatcher.message
|
import com.github.kotlintelegrambot.dispatcher.message
|
||||||
import com.github.kotlintelegrambot.dispatcher.photos
|
import com.github.kotlintelegrambot.dispatcher.photos
|
||||||
|
import com.github.kotlintelegrambot.entities.Message
|
||||||
import com.pischule.memestv.bot.handler.PhotoHandlerService
|
import com.pischule.memestv.bot.handler.PhotoHandlerService
|
||||||
import com.pischule.memestv.bot.handler.ThisCommandHandlerService
|
import com.pischule.memestv.bot.handler.ThisCommandHandlerService
|
||||||
|
import com.pischule.memestv.util.getMaxResPhotoId
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.github.oshai.kotlinlogging.withLoggingContext
|
import io.github.oshai.kotlinlogging.withLoggingContext
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
|
|
||||||
private val log = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
@EnableConfigurationProperties(BotProps::class)
|
@EnableConfigurationProperties(BotProps::class)
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -29,30 +31,33 @@ class BotConfiguration(
|
|||||||
token = botProps.token
|
token = botProps.token
|
||||||
dispatch {
|
dispatch {
|
||||||
message {
|
message {
|
||||||
withLoggingContext(
|
withLoggingContext(messageContext(message)) {
|
||||||
"messageId" to this.message.messageId.toString(),
|
|
||||||
"chatId" to this.message.chat.id.toString(),
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
thisCommandHandlerService.create().invoke(this)
|
thisCommandHandlerService.create(this)
|
||||||
} catch (e: Error) {
|
} catch (e: Error) {
|
||||||
log.error(e) { "Error while handling message" }
|
logger.error(e) { "Error while handling message" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
photos {
|
photos {
|
||||||
withLoggingContext(
|
withLoggingContext(messageContext(message)) {
|
||||||
"messageId" to this.message.messageId.toString(),
|
|
||||||
"chatId" to this.message.chat.id.toString(),
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
photoHandlerService.create().invoke(this)
|
photoHandlerService.create(this)
|
||||||
} catch (e: Error) {
|
} catch (e: Error) {
|
||||||
log.error(e) { "Error while handling photo" }
|
logger.error(e) { "Error while handling photo" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun messageContext(message: Message): Map<String, String?> =
|
||||||
|
mapOf(
|
||||||
|
"message_id" to message.messageId.toString(),
|
||||||
|
"chat_id" to message.chat.id.toString(),
|
||||||
|
"from_user_id" to message.from?.id.toString(),
|
||||||
|
"from_user_username" to message.from?.username.toString(),
|
||||||
|
"file_id" to message.getMaxResPhotoId(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import jakarta.annotation.PostConstruct
|
|||||||
import jakarta.annotation.PreDestroy
|
import jakarta.annotation.PreDestroy
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
private val log = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class BotService(private val bot: Bot) {
|
class BotService(private val bot: Bot) {
|
||||||
@@ -14,12 +14,12 @@ class BotService(private val bot: Bot) {
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
fun start() {
|
fun start() {
|
||||||
Thread { bot.startPolling() }.start()
|
Thread { bot.startPolling() }.start()
|
||||||
log.info { "Initialized bot" }
|
logger.info { "Initialized bot" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
fun stop() {
|
fun stop() {
|
||||||
bot.stopPolling()
|
bot.stopPolling()
|
||||||
log.info { "Stopped bot" }
|
logger.info { "Stopped bot" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.pischule.memestv.bot.handler
|
package com.pischule.memestv.bot.handler
|
||||||
|
|
||||||
import com.github.kotlintelegrambot.dispatcher.handlers.HandlePhotos
|
|
||||||
import com.github.kotlintelegrambot.dispatcher.handlers.media.MediaHandlerEnvironment
|
import com.github.kotlintelegrambot.dispatcher.handlers.media.MediaHandlerEnvironment
|
||||||
import com.github.kotlintelegrambot.entities.ChatId
|
import com.github.kotlintelegrambot.entities.ChatId
|
||||||
import com.github.kotlintelegrambot.entities.files.PhotoSize
|
import com.github.kotlintelegrambot.entities.files.PhotoSize
|
||||||
@@ -9,17 +8,17 @@ import com.pischule.memestv.bot.BotProps
|
|||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
private val log = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class PhotoHandlerService(private val botProps: BotProps) {
|
class PhotoHandlerService(private val botProps: BotProps) {
|
||||||
|
|
||||||
fun create(): HandlePhotos = {
|
suspend fun create(env: MediaHandlerEnvironment<List<PhotoSize>>) {
|
||||||
if (shouldForwardMessage(this)) {
|
if (shouldForwardMessage(env)) {
|
||||||
forwardPhotoMessage(this)
|
forwardPhotoMessage(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
reactToMessage(this, "👀")
|
reactToMessage(env, "👀")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): Boolean {
|
private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): Boolean {
|
||||||
@@ -34,8 +33,8 @@ class PhotoHandlerService(private val botProps: BotProps) {
|
|||||||
messageId = env.message.messageId,
|
messageId = env.message.messageId,
|
||||||
)
|
)
|
||||||
.fold(
|
.fold(
|
||||||
{ log.info { "Forwarded picture message: $it" } },
|
{ logger.atInfo { "Forwarded picture message: $it" } },
|
||||||
{ log.error { "Failed to forward message: $it" } },
|
{ logger.error { "Failed to forward message: $it" } },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +48,6 @@ class PhotoHandlerService(private val botProps: BotProps) {
|
|||||||
messageId = env.message.messageId,
|
messageId = env.message.messageId,
|
||||||
reaction = listOf(ReactionType.Emoji(emoji)),
|
reaction = listOf(ReactionType.Emoji(emoji)),
|
||||||
)
|
)
|
||||||
.onError { error -> log.atWarn { "Failed to react to message: $error" } }
|
.onError { error -> logger.atWarn { "Failed to react to message: $error" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,36 @@
|
|||||||
package com.pischule.memestv.bot.handler
|
package com.pischule.memestv.bot.handler
|
||||||
|
|
||||||
import com.github.kotlintelegrambot.dispatcher.handlers.HandleMessage
|
|
||||||
import com.github.kotlintelegrambot.dispatcher.handlers.MessageHandlerEnvironment
|
import com.github.kotlintelegrambot.dispatcher.handlers.MessageHandlerEnvironment
|
||||||
import com.github.kotlintelegrambot.entities.ChatId
|
import com.github.kotlintelegrambot.entities.ChatId
|
||||||
import com.github.kotlintelegrambot.entities.reaction.ReactionType
|
import com.github.kotlintelegrambot.entities.reaction.ReactionType
|
||||||
import com.pischule.memestv.bot.BotProps
|
import com.pischule.memestv.bot.BotProps
|
||||||
import com.pischule.memestv.s3.FileUploaderService
|
import com.pischule.memestv.s3.FileUploaderService
|
||||||
|
import com.pischule.memestv.util.getMaxResPhotoId
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
|
import io.github.oshai.kotlinlogging.withLoggingContext
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
private val log = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
class ThisCommandHandlerService(
|
class ThisCommandHandlerService(
|
||||||
private val botProps: BotProps,
|
private val botProps: BotProps,
|
||||||
private val fileUploaderService: FileUploaderService,
|
private val fileUploaderService: FileUploaderService,
|
||||||
) {
|
) {
|
||||||
fun create(): HandleMessage = HandleMessage@{
|
suspend fun create(env: MessageHandlerEnvironment) {
|
||||||
if (!shouldHandleMessage(this)) return@HandleMessage
|
if (!shouldHandleMessage(env)) return
|
||||||
|
|
||||||
val maxResPhotoId = message.replyToMessage!!.photo!!.last().fileId
|
val maxResPhotoId = env.message.replyToMessage?.getMaxResPhotoId() ?: return
|
||||||
val fileBytes = bot.downloadFileBytes(maxResPhotoId) ?: return@HandleMessage
|
|
||||||
|
|
||||||
log.info { "Downloaded a file $maxResPhotoId from Telegram" }
|
withLoggingContext("file_id" to maxResPhotoId) {
|
||||||
|
val fileBytes = env.bot.downloadFileBytes(maxResPhotoId) ?: return
|
||||||
|
logger.info { "Downloaded a file from Telegram" }
|
||||||
|
|
||||||
fileUploaderService.uploadFile(fileBytes)
|
fileUploaderService.uploadFile(fileBytes)
|
||||||
log.info { "Uploaded a file $maxResPhotoId to S3" }
|
logger.info { "Uploaded a file to S3" }
|
||||||
|
|
||||||
reactToMessage(this, "👍")
|
reactToMessage(env, "👍")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean {
|
private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean {
|
||||||
@@ -44,6 +47,6 @@ class ThisCommandHandlerService(
|
|||||||
messageId = env.message.messageId,
|
messageId = env.message.messageId,
|
||||||
reaction = listOf(ReactionType.Emoji(emoji)),
|
reaction = listOf(ReactionType.Emoji(emoji)),
|
||||||
)
|
)
|
||||||
.onError { error -> log.warn { "Failed to react to message: $error" } }
|
.onError { error -> logger.warn { "Failed to react to message: $error" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.pischule.memestv.util
|
||||||
|
|
||||||
|
import com.github.kotlintelegrambot.entities.Message
|
||||||
|
|
||||||
|
fun Message.getMaxResPhotoId(): String? = this.replyToMessage?.photo?.lastOrNull()?.fileId
|
||||||
Reference in New Issue
Block a user