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

add support of replaceStickerInSet

This commit is contained in:
InsanusMokrassar 2024-04-18 15:21:07 +06:00
parent c5102590cc
commit 5f4a327c2e
5 changed files with 199 additions and 3 deletions

View File

@ -0,0 +1,137 @@
package dev.inmo.tgbotapi.extensions.api.stickers
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.requests.abstracts.InputFile
import dev.inmo.tgbotapi.requests.stickers.AddStickerToSet
import dev.inmo.tgbotapi.requests.stickers.InputSticker
import dev.inmo.tgbotapi.requests.stickers.ReplaceStickerInSet
import dev.inmo.tgbotapi.types.StickerSetName
import dev.inmo.tgbotapi.types.StickerType
import dev.inmo.tgbotapi.types.chat.CommonUser
import dev.inmo.tgbotapi.types.UserId
import dev.inmo.tgbotapi.types.stickers.MaskPosition
import dev.inmo.tgbotapi.types.stickers.StickerSet
suspend fun TelegramBot.replaceStickerInSet(
userId: UserId,
stickerSetName: StickerSetName,
oldSticker: FileId,
newSticker: InputSticker
) = execute(
ReplaceStickerInSet(userId, stickerSetName, oldSticker, newSticker)
)
suspend fun TelegramBot.replaceStickerInSet(
userId: UserId,
stickerSetName: String,
oldSticker: FileId,
newSticker: InputSticker
) = replaceStickerInSet(userId, StickerSetName(stickerSetName), oldSticker, newSticker)
suspend fun TelegramBot.replaceStickerInSet(
userId: UserId,
stickerSet: StickerSet,
oldSticker: FileId,
newSticker: InputSticker
) = replaceStickerInSet(
userId,
stickerSet.name,
oldSticker,
newSticker
)
suspend fun TelegramBot.replaceStickerInSet(
userId: UserId,
stickerSet: StickerSet,
oldSticker: FileId,
sticker: InputFile,
emojis: List<String>,
keywords: List<String> = emptyList()
) = replaceStickerInSet(
userId,
stickerSet,
oldSticker,
when (stickerSet.stickerType) {
StickerType.CustomEmoji -> InputSticker.WithKeywords.CustomEmoji(
sticker,
emojis,
keywords
)
StickerType.Mask -> InputSticker.Mask(
sticker,
emojis
)
StickerType.Regular -> InputSticker.WithKeywords.Regular(
sticker,
emojis,
keywords
)
is StickerType.Unknown -> error("Unable to create sticker to the set with type ${stickerSet.stickerType}")
}
)
suspend fun TelegramBot.replaceStickerInSet(
userId: UserId,
stickerSet: StickerSet,
oldSticker: FileId,
sticker: InputFile,
emojis: List<String>,
maskPosition: MaskPosition? = null
) = replaceStickerInSet(
userId,
stickerSet.name,
oldSticker,
when (stickerSet.stickerType) {
StickerType.CustomEmoji -> InputSticker.WithKeywords.CustomEmoji(
sticker,
emojis,
emptyList()
)
StickerType.Mask -> InputSticker.Mask(
sticker,
emojis,
maskPosition
)
StickerType.Regular -> InputSticker.WithKeywords.Regular(
sticker,
emojis,
emptyList()
)
is StickerType.Unknown -> error("Unable to create sticker to the set with type ${stickerSet.stickerType}")
}
)
suspend fun TelegramBot.replaceStickerInSet(
user: CommonUser,
stickerSet: StickerSet,
oldSticker: FileId,
newSticker: InputSticker
) = replaceStickerInSet(
user.id,
stickerSet.name,
oldSticker,
newSticker
)
suspend fun TelegramBot.replaceStickerInSet(
user: CommonUser,
stickerSet: StickerSet,
oldSticker: FileId,
sticker: InputFile,
emojis: List<String>,
keywords: List<String> = emptyList()
) = replaceStickerInSet(
user.id, stickerSet, oldSticker, sticker, emojis, keywords
)
suspend fun TelegramBot.replaceStickerInSet(
user: CommonUser,
stickerSet: StickerSet,
oldSticker: FileId,
sticker: InputFile,
emojis: List<String>,
maskPosition: MaskPosition? = null
) = replaceStickerInSet(
user.id, stickerSet, oldSticker, sticker, emojis, maskPosition
)

View File

@ -38,7 +38,7 @@ data class AddStickerToSetData internal constructor(
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(stickerField)
override val inputSticker: InputSticker
override val newSticker: InputSticker
) : StandardStickerSetAction {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()

View File

@ -0,0 +1,59 @@
package dev.inmo.tgbotapi.requests.stickers
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.common.CommonMultipartFileRequest
import dev.inmo.tgbotapi.requests.stickers.abstracts.StandardStickerSetAction
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.File
import kotlinx.serialization.*
fun ReplaceStickerInSet(
userId: UserId,
stickerSetName: StickerSetName,
oldSticker: FileId,
newSticker: InputSticker
): Request<Boolean> {
val data = ReplaceStickerInSetData(
userId = userId,
name = stickerSetName,
oldSticker = oldSticker,
newSticker = newSticker
)
return when (val sticker = newSticker.sticker) {
is MultipartFile -> CommonMultipartFileRequest(
data,
mapOf(sticker.fileId to sticker)
)
is FileId -> data
}
}
fun ReplaceStickerInSet(
userId: UserId,
stickerSetName: String,
oldSticker: FileId,
newSticker: InputSticker
): Request<Boolean> = ReplaceStickerInSetData(
userId = userId,
name = StickerSetName(stickerSetName),
oldSticker = oldSticker,
newSticker = newSticker
)
@Serializable
data class ReplaceStickerInSetData internal constructor(
@SerialName(userIdField)
override val userId: UserId,
@SerialName(nameField)
override val name: StickerSetName,
@SerialName(oldStickerField)
val oldSticker: FileId,
@SerialName(stickerField)
override val newSticker: InputSticker
) : StandardStickerSetAction {
override fun method(): String = "replaceStickerInSet"
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -1,8 +1,7 @@
package dev.inmo.tgbotapi.requests.stickers.abstracts
import dev.inmo.tgbotapi.requests.stickers.InputSticker
import dev.inmo.tgbotapi.types.stickers.MaskPosition
interface StandardStickerSetAction : OwnerStickerSetAction {
val inputSticker: InputSticker
val newSticker: InputSticker
}

View File

@ -424,6 +424,7 @@ const val offsetField = "offset"
const val limitField = "limit"
const val stickersField = "stickers"
const val stickerField = "sticker"
const val oldStickerField = "old_sticker"
const val keywordsField = "keywords"
const val urlField = "url"
const val addressField = "address"