From 33888c5008044ca2aa1aa4999341f39fabf2f1f4 Mon Sep 17 00:00:00 2001 From: Maksim Pischulenok Date: Tue, 17 Jun 2025 22:08:13 +0300 Subject: [PATCH] Add support for multiple approvers --- .../com/pischule/memevizor/bot/BotConfiguration.kt | 13 +++++++++++++ .../kotlin/com/pischule/memevizor/bot/BotProps.kt | 3 ++- .../memevizor/bot/handler/PhotoHandlerService.kt | 4 ++-- .../bot/handler/ThisCommandHandlerService.kt | 4 ++-- .../resources/application-local.properties.dist | 3 ++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/pischule/memevizor/bot/BotConfiguration.kt b/src/main/kotlin/com/pischule/memevizor/bot/BotConfiguration.kt index d3d9fbb..0f3fab4 100644 --- a/src/main/kotlin/com/pischule/memevizor/bot/BotConfiguration.kt +++ b/src/main/kotlin/com/pischule/memevizor/bot/BotConfiguration.kt @@ -3,9 +3,12 @@ package com.pischule.memevizor.bot import com.github.kotlintelegrambot.Bot import com.github.kotlintelegrambot.bot import com.github.kotlintelegrambot.dispatch +import com.github.kotlintelegrambot.dispatcher.command import com.github.kotlintelegrambot.dispatcher.message import com.github.kotlintelegrambot.dispatcher.photos +import com.github.kotlintelegrambot.entities.ChatId import com.github.kotlintelegrambot.entities.Message +import com.github.kotlintelegrambot.entities.ParseMode import com.pischule.memevizor.bot.handler.PhotoHandlerService import com.pischule.memevizor.bot.handler.ThisCommandHandlerService import com.pischule.memevizor.util.getMaxResPhotoId @@ -52,6 +55,16 @@ class BotConfiguration( } } } + command("whoami") { + withLoggingContext(messageContext(message)) { + bot.sendMessage( + chatId = ChatId.fromId(message.chat.id), + text = "chatId: `${message.chat.id}`\nuserId: `${message.from?.id}`", + parseMode = ParseMode.MARKDOWN_V2, + ) + .onError { logger.warn { "Failed to reply to whoami command: $it" } } + } + } } private fun messageContext(message: Message): Map = diff --git a/src/main/kotlin/com/pischule/memevizor/bot/BotProps.kt b/src/main/kotlin/com/pischule/memevizor/bot/BotProps.kt index 0daa9b2..3149c30 100644 --- a/src/main/kotlin/com/pischule/memevizor/bot/BotProps.kt +++ b/src/main/kotlin/com/pischule/memevizor/bot/BotProps.kt @@ -2,4 +2,5 @@ package com.pischule.memevizor.bot import org.springframework.boot.context.properties.ConfigurationProperties -@ConfigurationProperties("bot") data class BotProps(val token: String, val destinationChatId: Long) +@ConfigurationProperties("bot") +data class BotProps(val token: String, val forwardChatId: Long, val approverUserIds: List) diff --git a/src/main/kotlin/com/pischule/memevizor/bot/handler/PhotoHandlerService.kt b/src/main/kotlin/com/pischule/memevizor/bot/handler/PhotoHandlerService.kt index c69a267..6c8e16f 100644 --- a/src/main/kotlin/com/pischule/memevizor/bot/handler/PhotoHandlerService.kt +++ b/src/main/kotlin/com/pischule/memevizor/bot/handler/PhotoHandlerService.kt @@ -22,13 +22,13 @@ class PhotoHandlerService(private val botProps: BotProps) { } private fun shouldForwardMessage(env: MediaHandlerEnvironment>): Boolean { - return env.message.chat.id != botProps.destinationChatId + return env.message.chat.id != botProps.forwardChatId } private suspend fun forwardPhotoMessage(env: MediaHandlerEnvironment>) { env.bot .forwardMessage( - chatId = ChatId.fromId(botProps.destinationChatId), + chatId = ChatId.fromId(botProps.forwardChatId), fromChatId = ChatId.fromId(env.message.chat.id), messageId = env.message.messageId, ) diff --git a/src/main/kotlin/com/pischule/memevizor/bot/handler/ThisCommandHandlerService.kt b/src/main/kotlin/com/pischule/memevizor/bot/handler/ThisCommandHandlerService.kt index c228807..efd0df4 100644 --- a/src/main/kotlin/com/pischule/memevizor/bot/handler/ThisCommandHandlerService.kt +++ b/src/main/kotlin/com/pischule/memevizor/bot/handler/ThisCommandHandlerService.kt @@ -35,11 +35,11 @@ class ThisCommandHandlerService( } private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean { - val isFromTargetChat = env.message.chat.id == botProps.destinationChatId + val isApprover = env.message.from?.id?.let { botProps.approverUserIds.contains(it) } == true val command = env.message.text?.lowercase() val isConfirmCommand = command in confirmCommands val hasPhotoReply = env.message.replyToMessage?.photo?.isNotEmpty() == true - return isFromTargetChat && isConfirmCommand && hasPhotoReply + return isApprover && isConfirmCommand && hasPhotoReply } private suspend fun reactToMessage(env: MessageHandlerEnvironment, emoji: String) { diff --git a/src/main/resources/application-local.properties.dist b/src/main/resources/application-local.properties.dist index 130129d..f015cf3 100644 --- a/src/main/resources/application-local.properties.dist +++ b/src/main/resources/application-local.properties.dist @@ -1,2 +1,3 @@ -bot.destination-chat-id= +bot.forward-chat-id= +bot.approver bot.token=