From 228ecba6ccd2c898f8a1bc2cd3822242f3b5d232 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Aug 2024 20:05:17 +0600 Subject: [PATCH] start implementation of paid invitation links --- .../chat/invite_links/CreateChatInviteLink.kt | 41 +++++++++++++++++++ .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 2 + 2 files changed, 43 insertions(+) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt index 18a375ba8b..78084d2ea7 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt @@ -4,6 +4,8 @@ import korlibs.time.DateTime import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest import dev.inmo.tgbotapi.requests.chat.abstracts.* import dev.inmo.tgbotapi.types.* +import korlibs.time.TimeSpan +import korlibs.time.days import kotlinx.serialization.* sealed interface CreateChatInviteLink : EditChatInviteLinkRequest { @@ -12,6 +14,11 @@ sealed interface CreateChatInviteLink : EditChatInv get() = expirationUnixTimeStamp ?.asDate override fun method(): String = "createChatInviteLink" + sealed interface Subscription : CreateChatInviteLink { + val subscriptionPeriod: TimeSpan + val subscriptionPrice: UInt + } + companion object { fun unlimited( chatId: ChatIdentifier, @@ -45,6 +52,13 @@ sealed interface CreateChatInviteLink : EditChatInv expiration: DateTime, name: String? = null, ) = withJoinRequest(chatId, name, expiration.toTelegramDate()) + fun paid( + chatId: ChatIdentifier, + subscriptionPrice: UInt, + subscriptionPeriod: TimeSpan = 30.days, + name: String? = null, + expirationUnixTimeStamp: TelegramDate? = null, + ) = CreateChatSubscriptionInviteLink(chatId, subscriptionPrice, name, subscriptionPeriod, expirationUnixTimeStamp) } } @@ -71,6 +85,33 @@ data class CreateChatInviteLinkUnlimited( get() = ChatInviteLinkUnlimited.serializer() } +/** + * Represent [https://core.telegram.org/bots/api#createchatsubscriptioninvitelink] request + * + * @see CreateChatInviteLink.unlimited + * @see CreateChatInviteLinkWithLimitedMembers + * @see CreateChatInviteLinkWithJoinRequest + */ +@Serializable +data class CreateChatSubscriptionInviteLink( + @SerialName(chatIdField) + override val chatId: ChatIdentifier, + @SerialName(subscriptionPriceField) + override val subscriptionPrice: UInt, + @SerialName(nameField) + override val name: String? = null, + @SerialName(subscriptionPeriodField) + @EncodeDefault + override val subscriptionPeriod: TimeSpan = 30.days, + @SerialName(expireDateField) + override val expirationUnixTimeStamp: TelegramDate? = null, +) : CreateChatInviteLink.Subscription { + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + override val resultDeserializer: DeserializationStrategy + get() = ChatInviteLinkUnlimited.serializer() +} + /** * Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITH `member_limit` * and WITHOUT `creates_join_request` diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index d8de107a3c..dfe941eb30 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -457,6 +457,8 @@ const val permissionsField = "permissions" const val typeField = "type" const val valueField = "value" const val creatorField = "creator" +const val subscriptionPeriodField = "subscription_period" +const val subscriptionPriceField = "subscription_price" const val pointField = "point" const val xShiftField = "x_shift"