All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendMediaGroup.kt Maven / Gradle / Ivy

package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media

import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.SendMessageRequest
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.*
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls
import kotlinx.serialization.*
import kotlinx.serialization.internal.ArrayListSerializer
import kotlinx.serialization.json.jsonArray

val membersCountInMediaGroup: IntRange = 2 .. 10

fun SendMediaGroup(
    chatId: ChatIdentifier,
    media: List,
    disableNotification: Boolean = false,
    replyToMessageId: MessageIdentifier? = null
): Request> {
    if (media.size !in membersCountInMediaGroup) {
        throw IllegalArgumentException("Count of members for media group must be in $membersCountInMediaGroup range")
    }

    val files: List = media.flatMap {
        listOfNotNull(
            it.file as? MultipartFile,
            if (it is ThumbedInputMedia) {
                it.thumb as? MultipartFile
            } else {
                null
            }
        )
    }

    val data = SendMediaGroupData(
        chatId,
        media,
        disableNotification,
        replyToMessageId
    )

    return if (files.isEmpty()) {
        data
    } else {
        MultipartRequestImpl(
            data,
            SendMediaGroupFiles(files)
        )
    }
}

private val messagesListSerializer: ArrayListSerializer
    = ArrayListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass())

@Serializable
data class SendMediaGroupData internal constructor(
    @SerialName(chatIdField)
    override val chatId: ChatIdentifier,
    val media: List = emptyList(),
    @SerialName(disableNotificationField)
    override val disableNotification: Boolean = false,
    @SerialName(replyToMessageIdField)
    override val replyToMessageId: MessageIdentifier? = null
) : DataRequest>, SendMessageRequest> {
    @SerialName(mediaField)
    private val convertedMedia: String
        get() = jsonArray {
            media.forEach {
                +it.toJsonWithoutNulls(MediaGroupMemberInputMediaSerializer)
            }
        }.toString()


    override fun method(): String = "sendMediaGroup"
    override val requestSerializer: SerializationStrategy<*>
        get() = serializer()
    override val resultDeserializer: DeserializationStrategy>
        get() = messagesListSerializer
}

data class SendMediaGroupFiles internal constructor(
    val files: List
) : Files by (files.map { it.fileId to it }.toMap())




© 2015 - 2024 Weber Informatics LLC | Privacy Policy