diff --git a/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt b/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt index 116e99e..565e3ce 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/BotConfiguration.kt @@ -8,6 +8,7 @@ import com.github.kotlintelegrambot.dispatcher.photos import com.pischule.memestv.bot.handler.PhotoHandlerService import com.pischule.memestv.bot.handler.ThisCommandHandlerService import io.github.oshai.kotlinlogging.KotlinLogging +import io.github.oshai.kotlinlogging.withLoggingContext import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -28,17 +29,27 @@ class BotConfiguration( token = botProps.token dispatch { message { - try { - thisCommandHandlerService.create().invoke(this) - } catch (e: Error) { - log.error(e) { "Error while handling message" } + withLoggingContext( + "messageId" to this.message.messageId.toString(), + "chatId" to this.message.chat.id.toString(), + ) { + try { + thisCommandHandlerService.create().invoke(this) + } catch (e: Error) { + log.error(e) { "Error while handling message" } + } } } photos { - try { - photoHandlerService.create().invoke(this) - } catch (e: Error) { - log.error(e) { "Error while handling photo" } + withLoggingContext( + "messageId" to this.message.messageId.toString(), + "chatId" to this.message.chat.id.toString(), + ) { + try { + photoHandlerService.create().invoke(this) + } catch (e: Error) { + log.error(e) { "Error while handling photo" } + } } } } diff --git a/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt b/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt index 2b6667e..cfd73db 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/handler/PhotoHandlerService.kt @@ -15,22 +15,23 @@ private val log = KotlinLogging.logger {} class PhotoHandlerService(private val botProps: BotProps) { fun create(): HandlePhotos = { - if (shouldForwardMessage()) { - forwardPhotoMessage() + if (shouldForwardMessage(this)) { + forwardPhotoMessage(this) } - reactToMessage("👀") + reactToMessage(this, "👀") } - private fun MediaHandlerEnvironment>.shouldForwardMessage(): Boolean { - return message.chat.id != botProps.destinationChatId + private fun shouldForwardMessage(env: MediaHandlerEnvironment>): Boolean { + return env.message.chat.id != botProps.destinationChatId } - private suspend fun MediaHandlerEnvironment>.forwardPhotoMessage() { - bot.forwardMessage( + private suspend fun forwardPhotoMessage(env: MediaHandlerEnvironment>) { + env.bot + .forwardMessage( chatId = ChatId.fromId(botProps.destinationChatId), - fromChatId = ChatId.fromId(message.chat.id), - messageId = message.messageId, + fromChatId = ChatId.fromId(env.message.chat.id), + messageId = env.message.messageId, ) .fold( { log.info { "Forwarded picture message: $it" } }, @@ -38,12 +39,16 @@ class PhotoHandlerService(private val botProps: BotProps) { ) } - private suspend fun MediaHandlerEnvironment>.reactToMessage(emoji: String) { - bot.setMessageReaction( - chatId = ChatId.fromId(message.chat.id), - messageId = message.messageId, + private suspend fun reactToMessage( + env: MediaHandlerEnvironment>, + emoji: String, + ) { + env.bot + .setMessageReaction( + chatId = ChatId.fromId(env.message.chat.id), + messageId = env.message.messageId, reaction = listOf(ReactionType.Emoji(emoji)), ) - .onError { error -> log.warn { "Failed to react to message: $error" } } + .onError { error -> log.atWarn { "Failed to react to message: $error" } } } } diff --git a/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt b/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt index d64146f..54cc344 100644 --- a/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt +++ b/src/main/kotlin/com/pischule/memestv/bot/handler/ThisCommandHandlerService.kt @@ -17,7 +17,7 @@ class ThisCommandHandlerService( private val fileUploaderService: FileUploaderService, ) { fun create(): HandleMessage = HandleMessage@{ - if (!shouldHandleMessage()) return@HandleMessage + if (!shouldHandleMessage(this)) return@HandleMessage val maxResPhotoId = message.replyToMessage!!.photo!!.last().fileId val fileBytes = bot.downloadFileBytes(maxResPhotoId) ?: return@HandleMessage @@ -27,20 +27,21 @@ class ThisCommandHandlerService( fileUploaderService.uploadFile(fileBytes) log.info { "Uploaded a file $maxResPhotoId to S3" } - reactToMessage("👍") + reactToMessage(this, "👍") } - private fun MessageHandlerEnvironment.shouldHandleMessage(): Boolean { - val isFromTargetChat = message.chat.id == botProps.destinationChatId - val isThisCommand = message.text?.lowercase() == "this" - val hasPhotoReply = message.replyToMessage?.photo?.isNotEmpty() == true + private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean { + val isFromTargetChat = env.message.chat.id == botProps.destinationChatId + val isThisCommand = env.message.text?.lowercase() == "this" + val hasPhotoReply = env.message.replyToMessage?.photo?.isNotEmpty() == true return isFromTargetChat && isThisCommand && hasPhotoReply } - private suspend fun MessageHandlerEnvironment.reactToMessage(emoji: String) { - bot.setMessageReaction( - chatId = ChatId.fromId(message.chat.id), - messageId = message.messageId, + private suspend fun reactToMessage(env: MessageHandlerEnvironment, emoji: String) { + env.bot + .setMessageReaction( + chatId = ChatId.fromId(env.message.chat.id), + messageId = env.message.messageId, reaction = listOf(ReactionType.Emoji(emoji)), ) .onError { error -> log.warn { "Failed to react to message: $error" } }