1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-11-30 11:25:44 +00:00

add support of gift things

This commit is contained in:
2025-05-10 13:25:21 +06:00
parent d36b11c002
commit 881763f3af
6 changed files with 889 additions and 0 deletions

View File

@@ -755,3 +755,12 @@ const val themeNameField = "theme_name"
const val dayField = "day"
const val monthField = "month"
const val yearField = "year"
const val rawField = "raw"
const val reactionTypeField = "reaction_type"
const val isDarkField = "is_dark"
const val isFlippedField = "is_flipped"
const val temperatureField = "temperature"
const val backgroundColorField = "background_color"

View File

@@ -0,0 +1,125 @@
package dev.inmo.tgbotapi.types.reactions
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.utils.decodeDataAndJson
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
import kotlin.jvm.JvmInline
@Serializable(ReactionType.Companion::class)
sealed interface ReactionType {
val type: Type
sealed interface Type {
val name: String
}
@Serializable
data class Emoji(
@SerialName(emojiField)
val emoji: String
) : ReactionType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "emoji"
}
}
@Serializable
data class CustomEmoji(
@SerialName(customEmojiIdField)
val customEmojiId: String
) : ReactionType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "custom_emoji"
}
}
@Serializable
class Paid : ReactionType {
@EncodeDefault
override val type: Type = Companion
override fun equals(other: Any?): Boolean {
return other is Paid
}
override fun hashCode(): Int {
return type.name.hashCode()
}
companion object : Type {
override val name: String = "paid"
}
}
@Serializable
data class Unknown(
@SerialName(typeField)
override val type: Custom,
@SerialName(rawField)
val raw: JsonElement? = null
) : ReactionType {
@Serializable
@JvmInline
value class Custom(override val name: String) : Type
}
companion object : KSerializer<ReactionType> {
@Serializable
data class Surrogate(
@SerialName(typeField)
val type: String,
@SerialName(emojiField)
val emoji: String? = null,
@SerialName(customEmojiIdField)
val customEmojiId: String? = null
)
override val descriptor: SerialDescriptor = Surrogate.serializer().descriptor
override fun serialize(encoder: Encoder, value: ReactionType) {
val surrogate = when (value) {
is Emoji -> Surrogate(
type = value.type.name,
emoji = value.emoji
)
is CustomEmoji -> Surrogate(
type = value.type.name,
customEmojiId = value.customEmojiId
)
is Paid -> Surrogate(
type = value.type.name
)
is Unknown -> Surrogate(
type = value.type.name
)
}
encoder.encodeSerializableValue(Surrogate.serializer(), surrogate)
}
override fun deserialize(decoder: Decoder): ReactionType {
val (surrogate, json) = decoder.decodeDataAndJson(Surrogate.serializer())
val unknown by lazy { Unknown(Unknown.Custom(surrogate.type), json) }
return when (surrogate.type) {
Emoji.name -> Emoji(
emoji = surrogate.emoji ?: return unknown
)
CustomEmoji.name -> CustomEmoji(
customEmojiId = surrogate.customEmojiId ?: return unknown
)
Paid.name -> Paid()
else -> unknown
}
}
}
}

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.types.stories
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class StoryArea(
val position: StoryAreaPosition,
val type: StoryAreaType
)

View File

@@ -0,0 +1,212 @@
package dev.inmo.tgbotapi.types.stories
import dev.inmo.micro_utils.colors.common.HEXAColor
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.location.LocationAddress
import dev.inmo.tgbotapi.types.reactions.ReactionType
import dev.inmo.tgbotapi.utils.IntRGB24HEXAColorSerializer
import dev.inmo.tgbotapi.utils.LongRGBAFromHEXAColorSerializer
import dev.inmo.tgbotapi.utils.decodeDataAndJson
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
import kotlin.jvm.JvmInline
@Serializable(StoryAreaType.Companion::class)
sealed interface StoryAreaType {
val type: Type
sealed interface Type {
val name: String
}
@Serializable
data class Location(
@SerialName(latitudeField)
val latitude: Double,
@SerialName(longitudeField)
val longitude: Double,
@SerialName(addressField)
val address: LocationAddress? = null
) : StoryAreaType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "location"
}
}
@Serializable
data class SuggestedReaction(
@SerialName(reactionTypeField)
val reactionType: ReactionType,
@SerialName(isDarkField)
val isDark: Boolean = false,
@SerialName(isFlippedField)
val isFlipped: Boolean = false
) : StoryAreaType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "suggested_reaction"
}
}
@Serializable
data class Link(
@SerialName(urlField)
val url: String
) : StoryAreaType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "link"
}
}
@Serializable
data class Weather(
@SerialName(temperatureField)
val temperature: Double,
@SerialName(emojiField)
val emoji: String,
@SerialName(backgroundColorField)
@Serializable(LongRGBAFromHEXAColorSerializer::class)
val backgroundColor: HEXAColor
) : StoryAreaType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "weather"
}
}
@Serializable
data class UniqueGift(
@SerialName(nameField)
val name: String
) : StoryAreaType {
@EncodeDefault
override val type: Type = Companion
companion object : Type {
override val name: String = "unique_gift"
}
}
@Serializable
data class Unknown(
@SerialName(typeField)
override val type: Custom,
@SerialName(rawField)
val raw: JsonElement? = null
) : StoryAreaType {
@Serializable
@JvmInline
value class Custom(override val name: String) : Type
}
companion object : KSerializer<StoryAreaType> {
@Serializable
data class Surrogate(
@SerialName(typeField)
val type: String,
@SerialName(latitudeField)
val latitude: Double? = null,
@SerialName(longitudeField)
val longitude: Double? = null,
@SerialName(addressField)
val address: LocationAddress? = null,
@SerialName(reactionTypeField)
val reactionType: ReactionType? = null,
@SerialName(isDarkField)
val isDark: Boolean? = null,
@SerialName(isFlippedField)
val isFlipped: Boolean? = null,
@SerialName(urlField)
val url: String? = null,
@SerialName(temperatureField)
val temperature: Double? = null,
@SerialName(emojiField)
val emoji: String? = null,
@SerialName(backgroundColorField)
@Serializable(LongRGBAFromHEXAColorSerializer::class)
val backgroundColor: HEXAColor? = null,
@SerialName(nameField)
val name: String? = null
)
override val descriptor: SerialDescriptor = Surrogate.serializer().descriptor
override fun serialize(encoder: Encoder, value: StoryAreaType) {
val surrogate = when (value) {
is Location -> Surrogate(
type = value.type.name,
latitude = value.latitude,
longitude = value.longitude,
address = value.address
)
is SuggestedReaction -> Surrogate(
type = value.type.name,
reactionType = value.reactionType,
isDark = value.isDark,
isFlipped = value.isFlipped
)
is Link -> Surrogate(
type = value.type.name,
url = value.url
)
is Weather -> Surrogate(
type = value.type.name,
temperature = value.temperature,
emoji = value.emoji,
backgroundColor = value.backgroundColor
)
is UniqueGift -> Surrogate(
type = value.type.name,
name = value.name
)
is Unknown -> Surrogate(
type = value.type.name
)
}
encoder.encodeSerializableValue(Surrogate.serializer(), surrogate)
}
override fun deserialize(decoder: Decoder): StoryAreaType {
val (surrogate, json) = decoder.decodeDataAndJson(Surrogate.serializer())
val unknown by lazy { Unknown(Unknown.Custom(surrogate.type), json) }
return when (surrogate.type) {
Location.name -> Location(
latitude = surrogate.latitude ?: return unknown,
longitude = surrogate.longitude ?: return unknown,
address = surrogate.address
)
SuggestedReaction.name -> SuggestedReaction(
reactionType = surrogate.reactionType ?: return unknown,
isDark = surrogate.isDark ?: false,
isFlipped = surrogate.isFlipped ?: false
)
Link.name -> Link(
url = surrogate.url ?: return unknown
)
Weather.name -> Weather(
temperature = surrogate.temperature ?: return unknown,
emoji = surrogate.emoji ?: return unknown,
backgroundColor = surrogate.backgroundColor ?: return unknown
)
UniqueGift.name -> UniqueGift(
name = surrogate.name ?: return unknown
)
else -> unknown
}
}
}
}

View File

@@ -0,0 +1,22 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.colors.common.HEXAColor
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
object LongRGBAFromHEXAColorSerializer : KSerializer<HEXAColor> {
override val descriptor: SerialDescriptor
get() = Int.serializer().descriptor
override fun deserialize(decoder: Decoder): HEXAColor {
val raw = decoder.decodeLong()
return HEXAColor(raw.toUInt())
}
override fun serialize(encoder: Encoder, value: HEXAColor) {
encoder.encodeLong(value.hexaUInt.toLong())
}
}