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.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,13 +29,22 @@ class BotConfiguration(
|
|||||||
token = botProps.token
|
token = botProps.token
|
||||||
dispatch {
|
dispatch {
|
||||||
message {
|
message {
|
||||||
|
withLoggingContext(
|
||||||
|
"messageId" to this.message.messageId.toString(),
|
||||||
|
"chatId" to this.message.chat.id.toString(),
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
thisCommandHandlerService.create().invoke(this)
|
thisCommandHandlerService.create().invoke(this)
|
||||||
} catch (e: Error) {
|
} catch (e: Error) {
|
||||||
log.error(e) { "Error while handling message" }
|
log.error(e) { "Error while handling message" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
photos {
|
photos {
|
||||||
|
withLoggingContext(
|
||||||
|
"messageId" to this.message.messageId.toString(),
|
||||||
|
"chatId" to this.message.chat.id.toString(),
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
photoHandlerService.create().invoke(this)
|
photoHandlerService.create().invoke(this)
|
||||||
} catch (e: Error) {
|
} catch (e: Error) {
|
||||||
@@ -44,4 +54,5 @@ class BotConfiguration(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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" } }
|
||||||
|
|||||||
Reference in New Issue
Block a user