1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-26 10:37:13 +00:00

include audio playlists, documents and slot machine dice

This commit is contained in:
InsanusMokrassar 2020-11-02 12:02:12 +06:00
parent 9e771fa04a
commit e9a05c4930
10 changed files with 87 additions and 8 deletions

View File

@ -8,12 +8,14 @@ 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.*
import dev.inmo.tgbotapi.utils.throwRangeError
import dev.inmo.tgbotapi.utils.toJsonWithoutNulls
import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.buildJsonArray
@RiskFeature("Media groups contains restrictions related to combinations of media types. Currently it is possible to" +
" combine photo + video OR audio OR documents")
fun SendMediaGroup(
chatId: ChatIdentifier,
media: List<MediaGroupMemberInputMedia>,
@ -52,6 +54,39 @@ fun SendMediaGroup(
}
}
/**
* Use this method to be sure that you are correctly sending playlist with audios
*/
@Suppress("NOTHING_TO_INLINE")
inline fun SendPlaylist(
chatId: ChatIdentifier,
media: List<InputMediaAudio>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId)
/**
* Use this method to be sure that you are correctly sending documents media group
*/
@Suppress("NOTHING_TO_INLINE")
inline fun SendDocumentsGroup(
chatId: ChatIdentifier,
media: List<InputMediaDocument>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId)
/**
* Use this method to be sure that you are correctly sending visual media group
*/
@Suppress("NOTHING_TO_INLINE")
inline fun SendVisualMediaGroup(
chatId: ChatIdentifier,
media: List<VisualMediaGroupMemberInputMedia>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId)
private val messagesListSerializer: KSerializer<List<MediaGroupMessage>>
= ListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass())

View File

@ -17,7 +17,7 @@ data class InputMediaPhoto(
override val caption: String? = null,
@SerialName(parseModeField)
override val parseMode: ParseMode? = null
) : InputMedia, MediaGroupMemberInputMedia {
) : InputMedia, VisualMediaGroupMemberInputMedia {
override val type: String = photoInputMediaType
override fun serialize(format: StringFormat): String = format.encodeToString(serializer(), this)

View File

@ -19,7 +19,7 @@ data class InputMediaVideo(
override val height: Int? = null,
override val duration: Long? = null,
override val thumb: InputFile? = null
) : InputMedia, SizedInputMedia, DuratedInputMedia, ThumbedInputMedia, MediaGroupMemberInputMedia {
) : InputMedia, SizedInputMedia, DuratedInputMedia, ThumbedInputMedia, VisualMediaGroupMemberInputMedia {
override val type: String = videoInputMediaType
override fun serialize(format: StringFormat): String = format.encodeToString(serializer(), this)

View File

@ -18,3 +18,6 @@ internal fun <T> T.buildArguments(withSerializer: SerializationStrategy<T>) = ar
interface MediaGroupMemberInputMedia : InputMedia, CaptionedOutput {
fun serialize(format: StringFormat): String
}
@Serializable(MediaGroupMemberInputMediaSerializer::class)
interface VisualMediaGroupMemberInputMedia : MediaGroupMemberInputMedia

View File

@ -22,3 +22,12 @@ data class DocumentFile(
@SerialName(fileNameField)
override val fileName: String? = null
) : TelegramMediaFile, MimedMediaFile, ThumbedMediaFile, CustomNamedMediaFile
inline fun <reified T : TelegramMediaFile> T.asDocumentFile() = DocumentFile(
fileId,
fileUniqueId,
fileSize,
(this as? ThumbedMediaFile) ?.thumb,
(this as? MimedMediaFile) ?.mimeType,
(this as? CustomNamedMediaFile) ?.fileName
)

View File

@ -5,4 +5,6 @@ import dev.inmo.tgbotapi.types.InputMedia.MediaGroupMemberInputMedia
interface MediaGroupContent : MediaContent, CaptionedInput {
fun toMediaGroupMemberInputMedia(): MediaGroupMemberInputMedia
}
}
interface VisualMediaGroupContent : MediaGroupContent

View File

@ -11,6 +11,7 @@ import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.DocumentFile
import dev.inmo.tgbotapi.types.files.asDocumentFile
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
@ -50,3 +51,14 @@ data class DocumentContent(
media.thumb ?.fileId
)
}
inline fun <reified T : MediaContent> T.asDocumentContent() = when (this) {
is CaptionedInput -> DocumentContent(
media.asDocumentFile(),
caption,
captionEntities
)
else -> DocumentContent(
media.asDocumentFile()
)
}

View File

@ -12,8 +12,7 @@ import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaCollectionContent
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.abstracts.*
import dev.inmo.tgbotapi.utils.toHtmlCaptions
import dev.inmo.tgbotapi.utils.toMarkdownV2Captions
@ -21,7 +20,7 @@ data class PhotoContent(
override val mediaCollection: Photo,
override val caption: String? = null,
override val captionEntities: List<TextPart> = emptyList()
) : MediaCollectionContent<PhotoSize>, MediaGroupContent {
) : MediaCollectionContent<PhotoSize>, VisualMediaGroupContent {
override val media: PhotoSize = mediaCollection.biggest() ?: throw IllegalStateException("Can't locate any photo size for this content")
override fun createResend(

View File

@ -13,6 +13,7 @@ import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.VideoFile
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.abstracts.VisualMediaGroupContent
import dev.inmo.tgbotapi.utils.toHtmlCaptions
import dev.inmo.tgbotapi.utils.toMarkdownV2Captions
@ -20,7 +21,7 @@ data class VideoContent(
override val media: VideoFile,
override val caption: String? = null,
override val captionEntities: List<TextPart> = emptyList()
) : MediaGroupContent {
) : VisualMediaGroupContent {
override fun createResend(
chatId: ChatIdentifier,
disableNotification: Boolean,

View File

@ -17,3 +17,21 @@ package dev.inmo.tgbotapi.utils
AnnotationTarget.TYPE_PARAMETER
)
annotation class PreviewFeature
@RequiresOptIn(
"This feature can work unstable and may have some restrictions in Telegram System",
RequiresOptIn.Level.WARNING
)
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.FIELD,
AnnotationTarget.PROPERTY,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.FUNCTION,
AnnotationTarget.TYPE,
AnnotationTarget.TYPEALIAS,
AnnotationTarget.TYPE_PARAMETER
)
annotation class RiskFeature(val message: String)