mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
Add support for multiple approvers
This commit is contained in:
@@ -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<String, String?> =
|
||||
|
||||
@@ -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<Long>)
|
||||
|
||||
@@ -22,13 +22,13 @@ class PhotoHandlerService(private val botProps: BotProps) {
|
||||
}
|
||||
|
||||
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>>) {
|
||||
env.bot
|
||||
.forwardMessage(
|
||||
chatId = ChatId.fromId(botProps.destinationChatId),
|
||||
chatId = ChatId.fromId(botProps.forwardChatId),
|
||||
fromChatId = ChatId.fromId(env.message.chat.id),
|
||||
messageId = env.message.messageId,
|
||||
)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
bot.destination-chat-id=<destination chat id>
|
||||
bot.forward-chat-id=<?>
|
||||
bot.approver<?>
|
||||
bot.token=<bot token>
|
||||
|
||||
Reference in New Issue
Block a user