Refactor handlers

This commit is contained in:
2025-04-28 02:02:18 +03:00
parent 774aeaa564
commit a1ad81d4e5
3 changed files with 49 additions and 32 deletions

View File

@@ -8,6 +8,7 @@ import com.github.kotlintelegrambot.dispatcher.photos
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 io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
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
@@ -28,17 +29,27 @@ class BotConfiguration(
token = botProps.token token = botProps.token
dispatch { dispatch {
message { message {
try { withLoggingContext(
thisCommandHandlerService.create().invoke(this) "messageId" to this.message.messageId.toString(),
} catch (e: Error) { "chatId" to this.message.chat.id.toString(),
log.error(e) { "Error while handling message" } ) {
try {
thisCommandHandlerService.create().invoke(this)
} catch (e: Error) {
log.error(e) { "Error while handling message" }
}
} }
} }
photos { photos {
try { withLoggingContext(
photoHandlerService.create().invoke(this) "messageId" to this.message.messageId.toString(),
} catch (e: Error) { "chatId" to this.message.chat.id.toString(),
log.error(e) { "Error while handling photo" } ) {
try {
photoHandlerService.create().invoke(this)
} catch (e: Error) {
log.error(e) { "Error while handling photo" }
}
} }
} }
} }

View File

@@ -15,22 +15,23 @@ private val log = KotlinLogging.logger {}
class PhotoHandlerService(private val botProps: BotProps) { class PhotoHandlerService(private val botProps: BotProps) {
fun create(): HandlePhotos = { fun create(): HandlePhotos = {
if (shouldForwardMessage()) { if (shouldForwardMessage(this)) {
forwardPhotoMessage() forwardPhotoMessage(this)
} }
reactToMessage("👀") reactToMessage(this, "👀")
} }
private fun MediaHandlerEnvironment<List<PhotoSize>>.shouldForwardMessage(): Boolean { private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): Boolean {
return message.chat.id != botProps.destinationChatId return env.message.chat.id != botProps.destinationChatId
} }
private suspend fun MediaHandlerEnvironment<List<PhotoSize>>.forwardPhotoMessage() { private suspend fun forwardPhotoMessage(env: MediaHandlerEnvironment<List<PhotoSize>>) {
bot.forwardMessage( env.bot
.forwardMessage(
chatId = ChatId.fromId(botProps.destinationChatId), chatId = ChatId.fromId(botProps.destinationChatId),
fromChatId = ChatId.fromId(message.chat.id), fromChatId = ChatId.fromId(env.message.chat.id),
messageId = message.messageId, messageId = env.message.messageId,
) )
.fold( .fold(
{ log.info { "Forwarded picture message: $it" } }, { log.info { "Forwarded picture message: $it" } },
@@ -38,12 +39,16 @@ class PhotoHandlerService(private val botProps: BotProps) {
) )
} }
private suspend fun MediaHandlerEnvironment<List<PhotoSize>>.reactToMessage(emoji: String) { private suspend fun reactToMessage(
bot.setMessageReaction( env: MediaHandlerEnvironment<List<PhotoSize>>,
chatId = ChatId.fromId(message.chat.id), emoji: String,
messageId = message.messageId, ) {
env.bot
.setMessageReaction(
chatId = ChatId.fromId(env.message.chat.id),
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 -> log.atWarn { "Failed to react to message: $error" } }
} }
} }

View File

@@ -17,7 +17,7 @@ class ThisCommandHandlerService(
private val fileUploaderService: FileUploaderService, private val fileUploaderService: FileUploaderService,
) { ) {
fun create(): HandleMessage = HandleMessage@{ fun create(): HandleMessage = HandleMessage@{
if (!shouldHandleMessage()) return@HandleMessage if (!shouldHandleMessage(this)) return@HandleMessage
val maxResPhotoId = message.replyToMessage!!.photo!!.last().fileId val maxResPhotoId = message.replyToMessage!!.photo!!.last().fileId
val fileBytes = bot.downloadFileBytes(maxResPhotoId) ?: return@HandleMessage val fileBytes = bot.downloadFileBytes(maxResPhotoId) ?: return@HandleMessage
@@ -27,20 +27,21 @@ class ThisCommandHandlerService(
fileUploaderService.uploadFile(fileBytes) fileUploaderService.uploadFile(fileBytes)
log.info { "Uploaded a file $maxResPhotoId to S3" } log.info { "Uploaded a file $maxResPhotoId to S3" }
reactToMessage("👍") reactToMessage(this, "👍")
} }
private fun MessageHandlerEnvironment.shouldHandleMessage(): Boolean { private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean {
val isFromTargetChat = message.chat.id == botProps.destinationChatId val isFromTargetChat = env.message.chat.id == botProps.destinationChatId
val isThisCommand = message.text?.lowercase() == "this" val isThisCommand = env.message.text?.lowercase() == "this"
val hasPhotoReply = message.replyToMessage?.photo?.isNotEmpty() == true val hasPhotoReply = env.message.replyToMessage?.photo?.isNotEmpty() == true
return isFromTargetChat && isThisCommand && hasPhotoReply return isFromTargetChat && isThisCommand && hasPhotoReply
} }
private suspend fun MessageHandlerEnvironment.reactToMessage(emoji: String) { private suspend fun reactToMessage(env: MessageHandlerEnvironment, emoji: String) {
bot.setMessageReaction( env.bot
chatId = ChatId.fromId(message.chat.id), .setMessageReaction(
messageId = message.messageId, chatId = ChatId.fromId(env.message.chat.id),
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 -> log.warn { "Failed to react to message: $error" } }