Skip to content

Commit

Permalink
chore(message): try to fix send gif
Browse files Browse the repository at this point in the history
  • Loading branch information
KurenaiRyu committed Aug 7, 2024
1 parent 0963661 commit e7e7154
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/kotlin/kurenai/imsyncbot/bot/qq/MessageContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.jsoup.Jsoup
import org.jsoup.parser.Parser
import java.nio.file.Path
import kotlin.io.path.fileSize
import kotlin.io.path.inputStream
import kotlin.io.path.pathString

/**
Expand All @@ -41,6 +42,10 @@ sealed class MessageContext(
val bot: ImSyncBot
) {

companion object {
val GIF_BYTEARRAY = byteArrayOf(0x47, 0x49, 0x46, 0x38)
}

private var tgMsgFormat =
if (bot.configProperties.bot.tgMsgFormat.contains("\$msg")) bot.configProperties.bot.tgMsgFormat else "\$name: \$msg"

Expand Down Expand Up @@ -166,6 +171,7 @@ class GroupMessageContext(
val images = messageChain.filterIsInstance<Image>()
if (images.size == 1) {
val image = images.first()
image.imageId
if (image.imageType == ImageType.GIF ||
image.imageType == ImageType.APNG ||
(image.imageType == ImageType.UNKNOWN && image.isEmoji)
Expand Down Expand Up @@ -315,21 +321,33 @@ class GroupMessageContext(

private suspend fun sendFileMessage(inputFile: InputFile): Array<TdApi.Message> {
val content = buildFileMessageContent(inputFile)
val file = content.photo
val isGif: Boolean = if (file is InputFileLocal) {
val buff = ByteArray(4)
Path.of(file.path).inputStream().read(buff, 0, buff.size)
GIF_BYTEARRAY.contentEquals(buff)
} else false

val func = SendMessage().apply {
this.chatId = this@GroupMessageContext.chatId
this@GroupMessageContext.getReplayToMessageId().takeIf { it > 0 }?.let { this.setReplyToMessageId(it) }
this.inputMessageContent = content
this.inputMessageContent = if (isGif) {
InputMessageAnimation().apply {
this.caption = content.caption
this.animation = content.photo
}
} else content
}
return arrayOf(bot.tg.send(untilPersistent = true, function = func).also {
if (shouldBeFile && fileSize > 500 * 1024) {

if (!isGif && shouldBeFile && fileSize > 800 * 1024) {
CoroutineScope(bot.coroutineContext).launch {
func.apply {
this.replyTo = MessageReplyToMessage().apply {
this.chatId = it.chatId
this.messageId = it.id
}
this.inputMessageContent = InputMessageDocument().apply {
this.caption = content.caption
this.document = content.photo
}
}
Expand Down

0 comments on commit e7e7154

Please sign in to comment.