Format code

This commit is contained in:
2025-04-08 23:44:37 +03:00
parent e2a1c867fd
commit 8ab982b360
9 changed files with 54 additions and 62 deletions

View File

@@ -2,8 +2,4 @@ package com.pischule.memestv
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 destinationChatId: Long)

View File

@@ -33,12 +33,12 @@ class BotService(
message {
try {
val chatId = message.chat.id
val replyToPhotos = message.replyToMessage
?.photo
?.takeIf { it.isNotEmpty() }
if (chatId == botProps.destinationChatId
&& message.text?.lowercase() == "this"
&& replyToPhotos != null
val replyToPhotos =
message.replyToMessage?.photo?.takeIf { it.isNotEmpty() }
if (
chatId == botProps.destinationChatId &&
message.text?.lowercase() == "this" &&
replyToPhotos != null
) {
val maxResPhoto = replyToPhotos.last().fileId
val fileBytes = bot.downloadFileBytes(maxResPhoto)
@@ -47,12 +47,13 @@ class BotService(
fileUploaderService.uploadFile(it)
log.info { "Uploaded a file $maxResPhoto to s3" }
bot.setMessageReaction(
chatId = ChatId.fromId(message.chat.id),
messageId = message.messageId,
reaction = listOf(ReactionType.Emoji("👍"))
).onError { error ->
log.warn { "Failed to react to message: $error" }
}
chatId = ChatId.fromId(message.chat.id),
messageId = message.messageId,
reaction = listOf(ReactionType.Emoji("👍")),
)
.onError { error ->
log.warn { "Failed to react to message: $error" }
}
}
}
} catch (e: Error) {
@@ -64,34 +65,27 @@ class BotService(
if (message.chat.id != botProps.destinationChatId) {
bot.forwardMessage(
chatId = ChatId.fromId(botProps.destinationChatId),
fromChatId = ChatId.fromId(message.chat.id),
messageId = message.messageId,
).fold(
{
log.info { "Forwarded pictures message: $it" }
},
{
log.error { "Failed to forward message: $it" }
}
)
chatId = ChatId.fromId(botProps.destinationChatId),
fromChatId = ChatId.fromId(message.chat.id),
messageId = message.messageId,
)
.fold(
{ log.info { "Forwarded pictures message: $it" } },
{ log.error { "Failed to forward message: $it" } },
)
}
bot.setMessageReaction(
chatId = ChatId.fromId(message.chat.id),
messageId = message.messageId,
reaction = listOf(ReactionType.Emoji("👀"))
).onError { error ->
log.warn { "Failed to react to message: $error" }
}
chatId = ChatId.fromId(message.chat.id),
messageId = message.messageId,
reaction = listOf(ReactionType.Emoji("👀")),
)
.onError { error -> log.warn { "Failed to react to message: $error" } }
}
}
}
Thread {
bot.startPolling()
}.start()
Thread { bot.startPolling() }.start()
log.info { "Initialized bot" }
}
@@ -101,4 +95,4 @@ class BotService(
bot.stopPolling()
log.info { "Stopped bot" }
}
}
}

View File

@@ -6,16 +6,15 @@ import aws.smithy.kotlin.runtime.content.ByteStream
import org.springframework.stereotype.Service
@Service
class FileUploaderService(
private val s3Client: S3Client,
private val s3Props: S3Props,
) {
class FileUploaderService(private val s3Client: S3Client, private val s3Props: S3Props) {
suspend fun uploadFile(fileBytes: ByteArray) {
s3Client.putObject(PutObjectRequest{
body = ByteStream.fromBytes(fileBytes)
bucket = s3Props.bucket
key = "_.jpeg"
})
s3Client.putObject(
PutObjectRequest {
body = ByteStream.fromBytes(fileBytes)
bucket = s3Props.bucket
key = "_.jpeg"
}
)
}
}
}

View File

@@ -1,11 +1,9 @@
package com.pischule.memestv
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
@SpringBootApplication
class MemesTvApplication
@SpringBootApplication class MemesTvApplication
fun main(args: Array<String>) {
runApplication<MemesTvApplication>(*args)

View File

@@ -21,4 +21,4 @@ class S3Config {
}
}
}
}
}

View File

@@ -3,8 +3,4 @@ package com.pischule.memestv
import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties("s3")
data class S3Props(
val accessKeyId: String,
val secretAccessKey: String,
val bucket: String,
)
data class S3Props(val accessKeyId: String, val secretAccessKey: String, val bucket: String)