Add support for multiple approvers

This commit is contained in:
2025-06-17 22:08:13 +03:00
parent 453b9d03ab
commit 33888c5008
5 changed files with 21 additions and 6 deletions

View File

@@ -3,9 +3,12 @@ package com.pischule.memevizor.bot
import com.github.kotlintelegrambot.Bot import com.github.kotlintelegrambot.Bot
import com.github.kotlintelegrambot.bot import com.github.kotlintelegrambot.bot
import com.github.kotlintelegrambot.dispatch import com.github.kotlintelegrambot.dispatch
import com.github.kotlintelegrambot.dispatcher.command
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.ChatId
import com.github.kotlintelegrambot.entities.Message 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.PhotoHandlerService
import com.pischule.memevizor.bot.handler.ThisCommandHandlerService import com.pischule.memevizor.bot.handler.ThisCommandHandlerService
import com.pischule.memevizor.util.getMaxResPhotoId 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<String, String?> = private fun messageContext(message: Message): Map<String, String?> =

View File

@@ -2,4 +2,5 @@ package com.pischule.memevizor.bot
import org.springframework.boot.context.properties.ConfigurationProperties 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<Long>)

View File

@@ -22,13 +22,13 @@ class PhotoHandlerService(private val botProps: BotProps) {
} }
private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): Boolean { private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): Boolean {
return env.message.chat.id != botProps.destinationChatId return env.message.chat.id != botProps.forwardChatId
} }
private suspend fun forwardPhotoMessage(env: MediaHandlerEnvironment<List<PhotoSize>>) { private suspend fun forwardPhotoMessage(env: MediaHandlerEnvironment<List<PhotoSize>>) {
env.bot env.bot
.forwardMessage( .forwardMessage(
chatId = ChatId.fromId(botProps.destinationChatId), chatId = ChatId.fromId(botProps.forwardChatId),
fromChatId = ChatId.fromId(env.message.chat.id), fromChatId = ChatId.fromId(env.message.chat.id),
messageId = env.message.messageId, messageId = env.message.messageId,
) )

View File

@@ -35,11 +35,11 @@ class ThisCommandHandlerService(
} }
private fun shouldHandleMessage(env: MessageHandlerEnvironment): Boolean { 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 command = env.message.text?.lowercase()
val isConfirmCommand = command in confirmCommands val isConfirmCommand = command in confirmCommands
val hasPhotoReply = env.message.replyToMessage?.photo?.isNotEmpty() == true val hasPhotoReply = env.message.replyToMessage?.photo?.isNotEmpty() == true
return isFromTargetChat && isConfirmCommand && hasPhotoReply return isApprover && isConfirmCommand && hasPhotoReply
} }
private suspend fun reactToMessage(env: MessageHandlerEnvironment, emoji: String) { private suspend fun reactToMessage(env: MessageHandlerEnvironment, emoji: String) {

View File

@@ -1,2 +1,3 @@
bot.destination-chat-id=<destination chat id> bot.forward-chat-id=<?>
bot.approver<?>
bot.token=<bot token> bot.token=<bot token>