1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

start implement new bot api

This commit is contained in:
InsanusMokrassar 2024-06-22 23:31:38 +06:00
parent c7b8187829
commit 66cf0689e1
4 changed files with 304 additions and 0 deletions

View File

@ -509,6 +509,7 @@ const val showAboveTextField = "show_above_text"
const val isDisabledField = "is_disabled"
const val preferSmallMediaField = "prefer_small_media"
const val preferLargeMediaField = "prefer_large_media"
const val withdrawalStateField = "withdrawal_state"
const val requireNameField = "need_name"
const val requirePhoneNumberField = "need_phone_number"

View File

@ -0,0 +1,92 @@
package dev.inmo.tgbotapi.types.payments.stars
import dev.inmo.tgbotapi.types.TelegramDate
import dev.inmo.tgbotapi.utils.decodeDataAndJson
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(RevenueWithdrawalState.Companion::class)
sealed interface RevenueWithdrawalState {
val type: String
@Serializable(RevenueWithdrawalState.Companion::class)
data object Pending : RevenueWithdrawalState {
override val type: String
get() = "pending"
}
@Serializable(RevenueWithdrawalState.Companion::class)
data class Succeeded(
val date: TelegramDate,
val url: String
) : RevenueWithdrawalState {
override val type: String
get() = Companion.type
companion object {
const val type: String = "succeeded"
}
}
@Serializable(RevenueWithdrawalState.Companion::class)
data object Failed : RevenueWithdrawalState {
override val type: String
get() = "failed"
}
@Serializable(RevenueWithdrawalState.Companion::class)
data class Unknown(
override val type: String,
val raw: JsonElement?
) : RevenueWithdrawalState
companion object : KSerializer<RevenueWithdrawalState> {
@Serializable
private data class Surrogate(
val type: String,
val date: TelegramDate? = null,
val url: String? = null
)
override val descriptor: SerialDescriptor
get() = Surrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): RevenueWithdrawalState {
val (data, json) = decoder.decodeDataAndJson(Surrogate.serializer())
val unknown by lazy {
Unknown(data.type, json)
}
return when (data.type) {
Pending.type -> Pending
Failed.type -> Failed
Succeeded.type -> Succeeded(
data.date ?: return unknown,
data.url ?: return unknown,
)
else -> unknown
}
}
override fun serialize(encoder: Encoder, value: RevenueWithdrawalState) {
val surrogate = when (value) {
Failed -> Surrogate(value.type)
Pending -> Surrogate(value.type)
is Succeeded -> Surrogate(
value.type,
value.date,
value.url
)
is Unknown -> value.raw ?.let {
return JsonElement.serializer().serialize(encoder, it)
} ?: Surrogate(value.type)
}
Surrogate.serializer().serialize(encoder, surrogate)
}
}
}

View File

@ -0,0 +1,109 @@
package dev.inmo.tgbotapi.types.payments.stars
import dev.inmo.tgbotapi.types.TelegramDate
import dev.inmo.tgbotapi.types.UserId
import dev.inmo.tgbotapi.types.chat.PreviewUser
import dev.inmo.tgbotapi.types.userField
import dev.inmo.tgbotapi.types.withdrawalStateField
import dev.inmo.tgbotapi.utils.decodeDataAndJson
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(StarTransaction.Companion::class)
sealed interface StarTransaction {
val amount: Int
val date: TelegramDate
val partner: TransactionPartner
val source: TransactionPartner?
val receiver: TransactionPartner?
@Serializable(StarTransaction.Companion::class)
data class Incoming(
) : StarTransaction {
override val type: String
get() = Companion.type
companion object {
const val type: String = "fragment"
}
}
@Serializable(StarTransaction.Companion::class)
data class User(
@SerialName(userField)
val user: PreviewUser
) : StarTransaction {
override val type: String
get() = Companion.type
companion object {
const val type: String = "user"
}
}
@Serializable(StarTransaction.Companion::class)
data object Other : StarTransaction {
override val type: String = "other"
}
@Serializable(StarTransaction.Companion::class)
data class Unknown(
override val type: String,
val raw: JsonElement?
) : StarTransaction
companion object : KSerializer<StarTransaction> {
@Serializable
private data class Surrogate(
val type: String,
val amount: Int,
val date: TelegramDate,
val source: TransactionPartner?,
val receiver: TransactionPartner?,
)
override val descriptor: SerialDescriptor
get() = Surrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): StarTransaction {
val (data, json) = decoder.decodeDataAndJson(Surrogate.serializer())
val unknown by lazy {
Unknown(data.type, json)
}
return when (data.type) {
Other.type -> Other
User.type -> User(
data.user ?: return unknown,
)
Fragment.type -> Fragment(
data.withdrawal_state ?: return unknown,
)
else -> unknown
}
}
override fun serialize(encoder: Encoder, value: StarTransaction) {
val surrogate = when (value) {
Other -> Surrogate(value.type)
is User -> Surrogate(value.type, user = value.user)
is Fragment -> Surrogate(
value.type,
value.withdrawalState
)
is Unknown -> value.raw ?.let {
return JsonElement.serializer().serialize(encoder, it)
} ?: Surrogate(value.type)
}
Surrogate.serializer().serialize(encoder, surrogate)
}
}
}

View File

@ -0,0 +1,102 @@
package dev.inmo.tgbotapi.types.payments.stars
import dev.inmo.tgbotapi.types.chat.PreviewUser
import dev.inmo.tgbotapi.types.userField
import dev.inmo.tgbotapi.types.withdrawalStateField
import dev.inmo.tgbotapi.utils.decodeDataAndJson
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(TransactionPartner.Companion::class)
sealed interface TransactionPartner {
val type: String
@Serializable(TransactionPartner.Companion::class)
data class Fragment(
@SerialName(withdrawalStateField)
val withdrawalState: RevenueWithdrawalState
) : TransactionPartner {
override val type: String
get() = Companion.type
companion object {
const val type: String = "fragment"
}
}
@Serializable(TransactionPartner.Companion::class)
data class User(
@SerialName(userField)
val user: PreviewUser
) : TransactionPartner {
override val type: String
get() = Companion.type
companion object {
const val type: String = "user"
}
}
@Serializable(TransactionPartner.Companion::class)
data object Other : TransactionPartner {
override val type: String = "other"
}
@Serializable(TransactionPartner.Companion::class)
data class Unknown(
override val type: String,
val raw: JsonElement?
) : TransactionPartner
companion object : KSerializer<TransactionPartner> {
@Serializable
private data class Surrogate(
val type: String,
val withdrawal_state: RevenueWithdrawalState? = null,
val user: PreviewUser? = null
)
override val descriptor: SerialDescriptor
get() = Surrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): TransactionPartner {
val (data, json) = decoder.decodeDataAndJson(Surrogate.serializer())
val unknown by lazy {
Unknown(data.type, json)
}
return when (data.type) {
Other.type -> Other
User.type -> User(
data.user ?: return unknown,
)
Fragment.type -> Fragment(
data.withdrawal_state ?: return unknown,
)
else -> unknown
}
}
override fun serialize(encoder: Encoder, value: TransactionPartner) {
val surrogate = when (value) {
Other -> Surrogate(value.type)
is User -> Surrogate(value.type, user = value.user)
is Fragment -> Surrogate(
value.type,
value.withdrawalState
)
is Unknown -> value.raw ?.let {
return JsonElement.serializer().serialize(encoder, it)
} ?: Surrogate(value.type)
}
Surrogate.serializer().serialize(encoder, surrogate)
}
}
}