1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-13 13:25:26 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt

137 lines
4.7 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.requests.send.media
2018-12-26 08:07:24 +00:00
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest
import dev.inmo.tgbotapi.requests.send.media.base.*
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.InputMedia.*
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass
import dev.inmo.tgbotapi.utils.*
2018-12-26 08:07:24 +00:00
import kotlinx.serialization.*
2020-03-22 07:37:01 +00:00
import kotlinx.serialization.builtins.ListSerializer
2020-08-18 06:50:11 +00:00
import kotlinx.serialization.json.buildJsonArray
2018-12-26 08:07:24 +00:00
2020-11-02 06:39:12 +00:00
const val rawSendingMediaGroupsWarning = "Media groups contains restrictions related to combinations of media" +
" types. Currently it is possible to combine photo + video OR audio OR documents"
@RiskFeature(rawSendingMediaGroupsWarning)
2018-12-26 08:07:24 +00:00
fun SendMediaGroup(
chatId: ChatIdentifier,
media: List<MediaGroupMemberInputMedia>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
): Request<List<MediaGroupMessage>> {
2020-06-01 05:53:33 +00:00
if (media.size !in mediaCountInMediaGroup) {
throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size)
2018-12-26 08:07:24 +00:00
}
val files: List<MultipartFile> = media.flatMap {
listOfNotNull(
it.file as? MultipartFile,
if (it is ThumbedInputMedia) {
it.thumb as? MultipartFile
} else {
null
}
)
}
val data = SendMediaGroupData(
chatId,
media,
disableNotification,
replyToMessageId,
allowSendingWithoutReply
2018-12-26 08:07:24 +00:00
)
return if (files.isEmpty()) {
data
} else {
MultipartRequestImpl(
data,
SendMediaGroupFiles(files)
)
}
}
/**
* Use this method to be sure that you are correctly sending playlist with audios
2020-11-02 06:39:12 +00:00
*
* @see InputMediaAudio
*/
@Suppress("NOTHING_TO_INLINE")
inline fun SendPlaylist(
chatId: ChatIdentifier,
2020-11-02 06:39:12 +00:00
media: List<AudioMediaGroupMemberInputMedia>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
/**
* Use this method to be sure that you are correctly sending documents media group
2020-11-02 06:39:12 +00:00
*
* @see InputMediaDocument
*/
@Suppress("NOTHING_TO_INLINE")
inline fun SendDocumentsGroup(
chatId: ChatIdentifier,
2020-11-02 06:39:12 +00:00
media: List<DocumentMediaGroupMemberInputMedia>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
/**
* Use this method to be sure that you are correctly sending visual media group
2020-11-02 06:39:12 +00:00
*
* @see InputMediaPhoto
* @see InputMediaVideo
*/
@Suppress("NOTHING_TO_INLINE")
inline fun SendVisualMediaGroup(
chatId: ChatIdentifier,
media: List<VisualMediaGroupMemberInputMedia>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
2020-03-22 07:37:01 +00:00
private val messagesListSerializer: KSerializer<List<MediaGroupMessage>>
= ListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass())
2018-12-26 08:07:24 +00:00
@Serializable
data class SendMediaGroupData internal constructor(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
val media: List<MediaGroupMemberInputMedia> = emptyList(),
@SerialName(disableNotificationField)
override val disableNotification: Boolean = false,
@SerialName(replyToMessageIdField)
override val replyToMessageId: MessageIdentifier? = null,
@SerialName(allowSendingWithoutReplyField)
override val allowSendingWithoutReply: Boolean? = null
) : DataRequest<List<MediaGroupMessage>>, SendMessageRequest<List<MediaGroupMessage>> {
2018-12-26 08:07:24 +00:00
@SerialName(mediaField)
private val convertedMedia: String
2020-08-18 06:50:11 +00:00
get() = buildJsonArray {
2018-12-26 08:07:24 +00:00
media.forEach {
2020-08-18 06:50:11 +00:00
add(it.toJsonWithoutNulls(MediaGroupMemberInputMediaSerializer))
2018-12-26 08:07:24 +00:00
}
}.toString()
override fun method(): String = "sendMediaGroup"
2019-12-02 08:35:37 +00:00
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override val resultDeserializer: DeserializationStrategy<List<MediaGroupMessage>>
2019-12-02 08:35:37 +00:00
get() = messagesListSerializer
2018-12-26 08:07:24 +00:00
}
data class SendMediaGroupFiles internal constructor(
val files: List<MultipartFile>
) : Files by (files.map { it.fileId to it }.toMap())