1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-18 06:13:47 +00:00

support of new stickers API

This commit is contained in:
InsanusMokrassar 2024-04-18 15:50:21 +06:00
parent 5f4a327c2e
commit 84abd006ad
7 changed files with 64 additions and 228 deletions

View File

@ -11,11 +11,10 @@ suspend fun TelegramBot.createNewStickerSet(
userId: UserId,
name: String,
title: String,
stickersFormat: StickerFormat,
stickers: List<InputSticker>,
needsRepainting: Boolean = false
) = execute(
CreateNewStickerSet(userId, name, title, stickersFormat, stickers, needsRepainting)
CreateNewStickerSet(userId, name, title, stickers, needsRepainting)
)
@ -23,9 +22,8 @@ suspend fun TelegramBot.createNewStickerSet(
user: CommonUser,
name: String,
title: String,
stickersFormat: StickerFormat,
stickers: List<InputSticker>,
needsRepainting: Boolean = false,
) = createNewStickerSet(
user.id, name, title, stickersFormat, stickers, needsRepainting
user.id, name, title, stickers, needsRepainting
)

View File

@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
import dev.inmo.tgbotapi.requests.stickers.SetStickerSetThumbnail
import dev.inmo.tgbotapi.types.StickerFormat
import dev.inmo.tgbotapi.types.StickerSetName
import dev.inmo.tgbotapi.types.chat.CommonUser
import dev.inmo.tgbotapi.types.UserId
@ -12,63 +13,71 @@ import dev.inmo.tgbotapi.types.stickers.StickerSet
suspend fun TelegramBot.setStickerSetThumbnail(
userId: UserId,
stickerSetName: StickerSetName,
format: StickerFormat,
thumbnail: FileId
) = execute(
SetStickerSetThumbnail(userId, stickerSetName, thumbnail)
SetStickerSetThumbnail(userId, stickerSetName, format, thumbnail)
)
suspend fun TelegramBot.setStickerSetThumbnail(
userId: UserId,
stickerSetName: StickerSetName,
format: StickerFormat,
thumbnail: MultipartFile
) = execute(
SetStickerSetThumbnail(userId, stickerSetName, thumbnail)
SetStickerSetThumbnail(userId, stickerSetName, format, thumbnail)
)
suspend fun TelegramBot.setStickerSetThumbnail(
user: CommonUser,
stickerSetName: StickerSetName,
format: StickerFormat,
thumbnail: FileId
) = setStickerSetThumbnail(
user.id, stickerSetName, thumbnail
user.id, stickerSetName, format, thumbnail
)
suspend fun TelegramBot.setStickerSetThumbnail(
user: CommonUser,
stickerSetName: StickerSetName,
format: StickerFormat,
thumbnail: MultipartFile
) = setStickerSetThumbnail(
user.id, stickerSetName, thumbnail
user.id, stickerSetName, format, thumbnail
)
suspend fun TelegramBot.setStickerSetThumbnail(
userId: UserId,
stickerSet: StickerSet,
format: StickerFormat,
thumbnail: FileId
) = setStickerSetThumbnail(
userId, stickerSet.name, thumbnail
userId, stickerSet.name, format, thumbnail
)
suspend fun TelegramBot.setStickerSetThumbnail(
userId: UserId,
stickerSet: StickerSet,
format: StickerFormat,
thumbnail: MultipartFile
) = setStickerSetThumbnail(
userId, stickerSet.name, thumbnail
userId, stickerSet.name, format, thumbnail
)
suspend fun TelegramBot.setStickerSetThumbnail(
user: CommonUser,
stickerSet: StickerSet,
format: StickerFormat,
thumbnail: FileId
) = setStickerSetThumbnail(
user.id, stickerSet, thumbnail
user.id, stickerSet, format, thumbnail
)
suspend fun TelegramBot.setStickerSetThumbnail(
user: CommonUser,
stickerSet: StickerSet,
format: StickerFormat,
thumbnail: MultipartFile
) = setStickerSetThumbnail(
user.id, stickerSet, thumbnail
user.id, stickerSet, format, thumbnail
)

View File

@ -21,14 +21,13 @@ fun CreateNewStickerSet(
userId: UserId,
name: StickerSetName,
title: String,
stickersFormat: StickerFormat,
stickers: List<InputSticker>,
needsRepainting: Boolean? = null
): Request<Boolean> {
val data = when(stickers.first()) {
is InputSticker.Mask -> CreateNewStickerSet.Mask(userId, name, title, stickersFormat, stickers.filterIsInstance<InputSticker.Mask>())
is InputSticker.WithKeywords.CustomEmoji -> CreateNewStickerSet.CustomEmoji(userId, name, title, stickersFormat, stickers.filterIsInstance<InputSticker.WithKeywords.CustomEmoji>(), needsRepainting)
is InputSticker.WithKeywords.Regular -> CreateNewStickerSet.Regular(userId, name, title, stickersFormat, stickers.filterIsInstance<InputSticker.WithKeywords.Regular>())
is InputSticker.Mask -> CreateNewStickerSet.Mask(userId, name, title, stickers.filterIsInstance<InputSticker.Mask>())
is InputSticker.WithKeywords.CustomEmoji -> CreateNewStickerSet.CustomEmoji(userId, name, title, stickers.filterIsInstance<InputSticker.WithKeywords.CustomEmoji>(), needsRepainting)
is InputSticker.WithKeywords.Regular -> CreateNewStickerSet.Regular(userId, name, title, stickers.filterIsInstance<InputSticker.WithKeywords.Regular>())
}
val multipartParts = stickers.mapNotNull {
(it.sticker as? MultipartFile)
@ -63,14 +62,12 @@ fun CreateNewStickerSet(
userId: UserId,
name: String,
title: String,
stickersFormat: StickerFormat,
stickers: List<InputSticker>,
needsRepainting: Boolean? = null
) = CreateNewStickerSet(
userId = userId,
name = StickerSetName(name),
title = title,
stickersFormat = stickersFormat,
stickers = stickers,
needsRepainting = needsRepainting
)
@ -79,7 +76,6 @@ fun CreateNewStickerSet(
sealed interface CreateNewStickerSet : CreateStickerSetAction {
val stickerType: StickerType
val stickers: List<InputSticker>
val stickersFormat: StickerFormat
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
@ -94,8 +90,6 @@ sealed interface CreateNewStickerSet : CreateStickerSetAction {
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickerFormatField)
override val stickersFormat: StickerFormat,
@SerialName(stickersField)
override val stickers: List<InputSticker.WithKeywords.Regular>
) : CreateNewStickerSet {
@ -111,8 +105,6 @@ sealed interface CreateNewStickerSet : CreateStickerSetAction {
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickerFormatField)
override val stickersFormat: StickerFormat,
@SerialName(stickersField)
override val stickers: List<InputSticker.Mask>
) : CreateNewStickerSet {
@ -128,8 +120,6 @@ sealed interface CreateNewStickerSet : CreateStickerSetAction {
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickerFormatField)
override val stickersFormat: StickerFormat,
@SerialName(stickersField)
override val stickers: List<InputSticker.WithKeywords.CustomEmoji>,
@SerialName(needsRepaintingField)
@ -148,8 +138,6 @@ sealed interface CreateNewStickerSet : CreateStickerSetAction {
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickerFormatField)
val stickersFormat: StickerFormat,
@SerialName(stickersField)
val stickers: List<InputSticker>,
@SerialName(stickerTypeField)
@ -172,7 +160,6 @@ object CreateNewStickerSetSerializer : KSerializer<CreateNewStickerSet>,
it.userId,
it.name,
it.title,
it.stickersFormat,
it.stickers,
it.stickerType,
(it as? CreateNewStickerSet.CustomEmoji)?.needsRepainting
@ -184,7 +171,6 @@ object CreateNewStickerSetSerializer : KSerializer<CreateNewStickerSet>,
it.userId,
it.name,
it.title,
it.stickersFormat,
it.stickers.filterIsInstance<InputSticker.WithKeywords.CustomEmoji>(),
it.needsRepainting
)
@ -192,14 +178,12 @@ object CreateNewStickerSetSerializer : KSerializer<CreateNewStickerSet>,
it.userId,
it.name,
it.title,
it.stickersFormat,
it.stickers.filterIsInstance<InputSticker.Mask>(),
)
StickerType.Regular -> CreateNewStickerSet.Regular(
it.userId,
it.name,
it.title,
it.stickersFormat,
it.stickers.filterIsInstance<InputSticker.WithKeywords.Regular>(),
)
is StickerType.Unknown -> error("Unable to create new sticker set due to error in type format: ${it.stickerType}")

View File

@ -2,11 +2,7 @@ package dev.inmo.tgbotapi.requests.stickers
import dev.inmo.micro_utils.serialization.mapper.MapperSerializer
import dev.inmo.tgbotapi.requests.abstracts.InputFile
import dev.inmo.tgbotapi.types.StickerType
import dev.inmo.tgbotapi.types.emojiListField
import dev.inmo.tgbotapi.types.keywordsField
import dev.inmo.tgbotapi.types.maskPositionField
import dev.inmo.tgbotapi.types.stickerField
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.stickers.MaskPosition
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
import kotlinx.serialization.KSerializer
@ -17,12 +13,15 @@ import kotlinx.serialization.Serializable
@Serializable(InputStickerSerializer::class)
sealed interface InputSticker {
val sticker: InputFile
val format: StickerFormat
val emojisList: List<String>
@Serializable
data class Mask(
@SerialName(stickerField)
override val sticker: InputFile,
@SerialName(formatField)
override val format: StickerFormat,
@SerialName(emojiListField)
override val emojisList: List<String>,
@SerialName(maskPositionField)
@ -37,6 +36,8 @@ sealed interface InputSticker {
data class Regular(
@SerialName(stickerField)
override val sticker: InputFile,
@SerialName(formatField)
override val format: StickerFormat,
@SerialName(emojiListField)
override val emojisList: List<String>,
@SerialName(keywordsField)
@ -47,6 +48,8 @@ sealed interface InputSticker {
data class CustomEmoji(
@SerialName(stickerField)
override val sticker: InputFile,
@SerialName(formatField)
override val format: StickerFormat,
@SerialName(emojiListField)
override val emojisList: List<String>,
@SerialName(keywordsField)
@ -61,6 +64,7 @@ object InputStickerSerializer : KSerializer<InputSticker>, MapperSerializer<Inpu
when (it) {
is InputSticker.Mask -> SurrogateInputSticker(
it.sticker,
it.format,
it.emojisList,
emptyList(),
it.maskPosition,
@ -68,6 +72,7 @@ object InputStickerSerializer : KSerializer<InputSticker>, MapperSerializer<Inpu
)
is InputSticker.WithKeywords.CustomEmoji -> SurrogateInputSticker(
it.sticker,
it.format,
it.emojisList,
it.keywords,
null,
@ -75,6 +80,7 @@ object InputStickerSerializer : KSerializer<InputSticker>, MapperSerializer<Inpu
)
is InputSticker.WithKeywords.Regular -> SurrogateInputSticker(
it.sticker,
it.format,
it.emojisList,
it.keywords,
null,
@ -86,21 +92,25 @@ object InputStickerSerializer : KSerializer<InputSticker>, MapperSerializer<Inpu
when (it.internalType) {
StickerType.CustomEmoji -> InputSticker.WithKeywords.CustomEmoji(
it.sticker,
it.format,
it.emojisList,
it.keywords
)
StickerType.Mask -> InputSticker.Mask(
it.sticker,
it.format,
it.emojisList,
it.maskPosition
)
StickerType.Regular -> InputSticker.WithKeywords.Regular(
it.sticker,
it.format,
it.emojisList,
it.keywords
)
is StickerType.Unknown -> InputSticker.WithKeywords.Regular(
it.sticker,
it.format,
it.emojisList,
it.keywords
)
@ -111,6 +121,8 @@ object InputStickerSerializer : KSerializer<InputSticker>, MapperSerializer<Inpu
data class SurrogateInputSticker internal constructor(
@SerialName(stickerField)
val sticker: InputFile,
@SerialName(formatField)
val format: StickerFormat,
@SerialName(emojiListField)
val emojisList: List<String>,
@SerialName(keywordsField)

View File

@ -9,10 +9,11 @@ import kotlinx.serialization.*
fun SetStickerSetThumbnail(
userId: UserId,
stickerSetName: StickerSetName,
format: StickerFormat,
thumbnail: MultipartFile
): Request<Boolean> {
return CommonMultipartFileRequest(
SetStickerSetThumbnail(userId, stickerSetName),
SetStickerSetThumbnail(userId, stickerSetName, format),
mapOf(thumbnailField to thumbnail)
)
}
@ -20,10 +21,12 @@ fun SetStickerSetThumbnail(
fun SetStickerSetThumbnail(
userId: UserId,
stickerSetName: String,
format: StickerFormat,
thumbnail: MultipartFile
): Request<Boolean> = SetStickerSetThumbnail(
userId = userId,
stickerSetName = StickerSetName(stickerSetName),
stickerSetName = StickerSetName(stickerSetName, ),
format = format,
thumbnail = thumbnail
)
@ -33,6 +36,8 @@ data class SetStickerSetThumbnail (
override val userId: UserId,
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(formatField)
val format: StickerFormat,
@SerialName(thumbnailField)
val thumbnail: FileId? = null
) : OwnerStickerSetAction {

View File

@ -379,6 +379,7 @@ const val oldChatMemberField = "old_chat_member"
const val newChatMemberField = "new_chat_member"
const val stickerTypeField = "sticker_type"
const val stickerFormatField = "sticker_format"
const val formatField = "format"
const val needsRepaintingField = "needs_repainting"
const val okField = "ok"

View File

@ -25,12 +25,7 @@ sealed interface StickerSet {
val name: StickerSetName
val title: String
val stickerType: StickerType
val stickerFormat: StickerFormat
val stickers: List<Sticker>
val isAnimated: Boolean
get() = false
val isVideo: Boolean
get() = false
val thumbnail: PhotoSize?
object Serializer : KSerializer<StickerSet> {
@ -42,69 +37,33 @@ sealed interface StickerSet {
return when (surrogate.sticker_type) {
StickerType.CustomEmoji -> when {
surrogate.is_animated == true -> CustomEmojiAnimatedStickerSet(
else -> CustomEmojiStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<CustomEmojiAnimatedSticker>(),
surrogate.thumb
)
surrogate.is_video == true -> CustomEmojiVideoStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<CustomEmojiVideoSticker>(),
surrogate.thumb
)
else -> CustomEmojiSimpleStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<CustomEmojiSimpleSticker>(),
surrogate.stickers.filterIsInstance<CustomEmojiSticker>(),
surrogate.thumb
)
}
StickerType.Mask -> when {
surrogate.is_animated == true -> MaskAnimatedStickerSet(
else -> MaskStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<MaskAnimatedSticker>(),
surrogate.thumb
)
surrogate.is_video == true -> MaskVideoStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<MaskVideoSticker>(),
surrogate.thumb
)
else -> MaskSimpleStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<MaskSimpleSticker>(),
surrogate.stickers.filterIsInstance<MaskSticker>(),
surrogate.thumb
)
}
StickerType.Regular -> when {
surrogate.is_animated == true -> RegularAnimatedStickerSet(
else -> RegularStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularAnimatedSticker>(),
surrogate.thumb
)
surrogate.is_video == true -> RegularVideoStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularVideoSticker>(),
surrogate.thumb
)
else -> RegularSimpleStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularSimpleSticker>(),
surrogate.stickers.filterIsInstance<RegularSticker>(),
surrogate.thumb
)
}
is StickerType.Unknown -> UnknownStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularSimpleSticker>(),
surrogate.stickers.filterIsInstance<RegularSticker>(),
surrogate.sticker_type,
surrogate.thumb,
json
@ -119,180 +78,48 @@ sealed interface StickerSet {
}
@Serializable
sealed interface AnimatedStickerSet : StickerSet {
override val isAnimated: Boolean
get() = true
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat
get() = StickerFormat.Animated
}
@Serializable
sealed interface VideoStickerSet : StickerSet {
override val isVideo: Boolean
get() = true
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat
get() = StickerFormat.Video
}
@Serializable
sealed interface RegularStickerSet : StickerSet
@Serializable
sealed interface MaskStickerSet : StickerSet
@Serializable
sealed interface CustomEmojiStickerSet : StickerSet
@Serializable
data class RegularSimpleStickerSet(
data class RegularStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularSimpleSticker>,
override val stickers: List<RegularSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : RegularStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat = StickerFormat.Static
}
@Serializable
data class RegularAnimatedStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularAnimatedSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : RegularStickerSet, AnimatedStickerSet {
) : StickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
data class RegularVideoStickerSet(
data class MaskStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularVideoSticker>,
override val stickers: List<MaskSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : RegularStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
data class MaskSimpleStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<MaskSimpleSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : MaskStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat = StickerFormat.Static
}
@Serializable
data class MaskAnimatedStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<MaskAnimatedSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : MaskStickerSet, AnimatedStickerSet {
) : StickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
}
@Serializable
data class MaskVideoStickerSet(
data class CustomEmojiStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<MaskVideoSticker>,
override val stickers: List<CustomEmojiSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : MaskStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
}
@Serializable
data class CustomEmojiSimpleStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiSimpleSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : CustomEmojiStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat = StickerFormat.Static
}
@Serializable
data class CustomEmojiAnimatedStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiAnimatedSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : CustomEmojiStickerSet, AnimatedStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
}
@Serializable
data class CustomEmojiVideoStickerSet(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiVideoSticker>,
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null
) : CustomEmojiStickerSet, VideoStickerSet {
) : StickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
@ -311,4 +138,4 @@ data class UnknownStickerSet(
@SerialName(thumbnailField)
override val thumbnail: PhotoSize? = null,
val raw: JsonElement
) : CustomEmojiStickerSet, VideoStickerSet
) : StickerSet