mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
Refactor handlers
This commit is contained in:
@@ -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" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<List<PhotoSize>>.shouldForwardMessage(): Boolean {
|
||||
return message.chat.id != botProps.destinationChatId
|
||||
private fun shouldForwardMessage(env: MediaHandlerEnvironment<List<PhotoSize>>): Boolean {
|
||||
return env.message.chat.id != botProps.destinationChatId
|
||||
}
|
||||
|
||||
private suspend fun MediaHandlerEnvironment<List<PhotoSize>>.forwardPhotoMessage() {
|
||||
bot.forwardMessage(
|
||||
private suspend fun forwardPhotoMessage(env: MediaHandlerEnvironment<List<PhotoSize>>) {
|
||||
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<List<PhotoSize>>.reactToMessage(emoji: String) {
|
||||
bot.setMessageReaction(
|
||||
chatId = ChatId.fromId(message.chat.id),
|
||||
messageId = message.messageId,
|
||||
private suspend fun reactToMessage(
|
||||
env: MediaHandlerEnvironment<List<PhotoSize>>,
|
||||
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" } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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" } }
|
||||
|
||||
Reference in New Issue
Block a user