diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt index 9cd4c510cd..b24ae9b25d 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt @@ -5,6 +5,8 @@ import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.chat.invite_links.CreateChatInviteLink import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.PublicChat +import korlibs.time.TimeSpan +import korlibs.time.days public suspend fun TelegramBot.createChatInviteLinkUnlimited( chatId: ChatIdentifier, @@ -81,3 +83,38 @@ public suspend fun TelegramBot.createChatInviteLinkWithJoinRequest( expiration: DateTime, name: String? = null, ): ChatInviteLinkWithJoinRequest = createChatInviteLinkWithJoinRequest(chat.id, name, expiration.toTelegramDate()) + + +// Subscriptions + +public suspend fun TelegramBot.createChatSubscriptionInviteLink( + chatId: ChatIdentifier, + subscriptionPrice: UInt, + subscriptionPeriod: TimeSpan = 30.days, + name: String? = null, + expirationUnixTimeStamp: TelegramDate? = null, +): ChatInviteLinkUnlimited = execute(CreateChatInviteLink.subscription(chatId, subscriptionPrice, subscriptionPeriod, name, expirationUnixTimeStamp)) + +public suspend fun TelegramBot.createChatSubscriptionInviteLink( + chat: PublicChat, + subscriptionPrice: UInt, + subscriptionPeriod: TimeSpan = 30.days, + name: String? = null, + expirationUnixTimeStamp: TelegramDate? = null, +): ChatInviteLinkUnlimited = createChatSubscriptionInviteLink(chat.id, subscriptionPrice, subscriptionPeriod, name, expirationUnixTimeStamp) + +public suspend fun TelegramBot.createChatSubscriptionInviteLink( + chatId: ChatIdentifier, + subscriptionPrice: UInt, + subscriptionPeriod: TimeSpan = 30.days, + name: String? = null, + expirationUnixTimeStamp: DateTime, +): ChatInviteLinkUnlimited = createChatSubscriptionInviteLink(chatId, subscriptionPrice, subscriptionPeriod, name, expirationUnixTimeStamp.toTelegramDate()) + +public suspend fun TelegramBot.createChatSubscriptionInviteLink( + chat: PublicChat, + subscriptionPrice: UInt, + subscriptionPeriod: TimeSpan = 30.days, + name: String? = null, + expirationUnixTimeStamp: DateTime, +): ChatInviteLinkUnlimited = createChatSubscriptionInviteLink(chat.id, subscriptionPrice, subscriptionPeriod, name, expirationUnixTimeStamp) \ No newline at end of file diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt index f4f9585d76..034b3642ae 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt @@ -181,3 +181,30 @@ public suspend fun TelegramBot.editChatInviteLinkWithJoinRequest( expiration: DateTime, name: String? = null, ): ChatInviteLinkWithJoinRequest = editChatInviteLinkWithJoinRequest(chat.id, previousLink, name , expiration.toTelegramDate()) + + +// Subscriptions + +public suspend fun TelegramBot.editChatSubscriptionInviteLink( + chatId: ChatIdentifier, + previousLink: String, + name: String, +): ChatInviteLinkUnlimited = execute(EditChatInviteLink.subscription(chatId, previousLink, name)) + +public suspend fun TelegramBot.editChatSubscriptionInviteLink( + chatId: ChatIdentifier, + previousLink: ChatInviteLink, + name: String, +): ChatInviteLinkUnlimited = editChatSubscriptionInviteLink(chatId, previousLink.inviteLink, name) + +public suspend fun TelegramBot.editChatSubscriptionInviteLink( + chat: PublicChat, + previousLink: String, + name: String, +): ChatInviteLinkUnlimited = editChatSubscriptionInviteLink(chat.id, previousLink, name) + +public suspend fun TelegramBot.editChatSubscriptionInviteLink( + chat: PublicChat, + previousLink: ChatInviteLink, + name: String, +): ChatInviteLinkUnlimited = editChatSubscriptionInviteLink(chat.id, previousLink, name) 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 78084d2ea7..84619cb79a 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 @@ -1,9 +1,9 @@ package dev.inmo.tgbotapi.requests.chat.invite_links import korlibs.time.DateTime -import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest import dev.inmo.tgbotapi.requests.chat.abstracts.* import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.utils.TimeSpanAsSecondsSerializer import korlibs.time.TimeSpan import korlibs.time.days import kotlinx.serialization.* @@ -12,11 +12,14 @@ sealed interface CreateChatInviteLink : EditChatInv val expirationUnixTimeStamp: TelegramDate? override val expireDate: DateTime? get() = expirationUnixTimeStamp ?.asDate + override fun method(): String = "createChatInviteLink" sealed interface Subscription : CreateChatInviteLink { val subscriptionPeriod: TimeSpan val subscriptionPrice: UInt + + override fun method(): String = "createChatSubscriptionInviteLink" } companion object { @@ -52,7 +55,7 @@ sealed interface CreateChatInviteLink : EditChatInv expiration: DateTime, name: String? = null, ) = withJoinRequest(chatId, name, expiration.toTelegramDate()) - fun paid( + fun subscription( chatId: ChatIdentifier, subscriptionPrice: UInt, subscriptionPeriod: TimeSpan = 30.days, @@ -63,7 +66,7 @@ sealed interface CreateChatInviteLink : EditChatInv } /** - * Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITHOUT `member_limit` + * Represent [request](https://core.telegram.org/bots/api#createchatinvitelink) WITHOUT `member_limit` * and `creates_join_request` * * @see CreateChatInviteLink.unlimited @@ -86,7 +89,7 @@ data class CreateChatInviteLinkUnlimited( } /** - * Represent [https://core.telegram.org/bots/api#createchatsubscriptioninvitelink] request + * Represent [request](https://core.telegram.org/bots/api#createchatsubscriptioninvitelink) * * @see CreateChatInviteLink.unlimited * @see CreateChatInviteLinkWithLimitedMembers @@ -102,6 +105,7 @@ data class CreateChatSubscriptionInviteLink( override val name: String? = null, @SerialName(subscriptionPeriodField) @EncodeDefault + @Serializable(TimeSpanAsSecondsSerializer::class) override val subscriptionPeriod: TimeSpan = 30.days, @SerialName(expireDateField) override val expirationUnixTimeStamp: TelegramDate? = null, @@ -113,7 +117,7 @@ data class CreateChatSubscriptionInviteLink( } /** - * Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITH `member_limit` + * Represent [request](https://core.telegram.org/bots/api#createchatinvitelink) WITH `member_limit` * and WITHOUT `creates_join_request` * * @see CreateChatInviteLink.withLimitedMembers @@ -136,7 +140,7 @@ data class CreateChatInviteLinkWithLimitedMembers( } /** - * Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITHOUT `member_limit` + * Represent [request](https://core.telegram.org/bots/api#createchatinvitelink) WITHOUT `member_limit` * and WITH `creates_join_request` * * @see CreateChatInviteLink.withJoinRequest diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt index 7ef53689d7..48b95960b2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt @@ -12,6 +12,10 @@ sealed interface EditChatInviteLink : EditChatInvit get() = expirationUnixTimeStamp ?.asDate override fun method(): String = "editChatInviteLink" + sealed interface Subscription : EditChatInviteLink { + override fun method(): String = "editChatSubscriptionInviteLink" + } + companion object { fun unlimited( chatId: ChatIdentifier, @@ -51,6 +55,11 @@ sealed interface EditChatInviteLink : EditChatInvit expiration: DateTime, name: String? = null, ) = withJoinRequest(chatId, inviteLink, name, expiration.toTelegramDate()) + fun subscription( + chatId: ChatIdentifier, + inviteLink: String, + name: String, + ) = EditChatSubscriptionInviteLink(chatId, inviteLink, name) } } @@ -79,6 +88,28 @@ data class EditChatInviteLinkUnlimited( get() = ChatInviteLinkUnlimited.serializer() } +/** + * Represent [request](https://core.telegram.org/bots/api#editchatsubscriptioninvitelink) + * + * @see EditChatInviteLink.subscription + */ +@Serializable +data class EditChatSubscriptionInviteLink( + @SerialName(chatIdField) + override val chatId: ChatIdentifier, + @SerialName(inviteLinkField) + override val inviteLink: String, + @SerialName(nameField) + override val name: String? = null, +) : EditChatInviteLink.Subscription { + override val expirationUnixTimeStamp: TelegramDate? + get() = null + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + override val resultDeserializer: DeserializationStrategy + get() = ChatInviteLinkUnlimited.serializer() +} + /** * Represent [https://core.telegram.org/bots/api#editchatinvitelink] request WITH `member_limit` * and WITHOUT `creates_join_request` diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TimeSpanAsSecondsSerializer.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TimeSpanAsSecondsSerializer.kt new file mode 100644 index 0000000000..166a5114b0 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TimeSpanAsSecondsSerializer.kt @@ -0,0 +1,22 @@ +package dev.inmo.tgbotapi.utils + +import korlibs.time.TimeSpan +import korlibs.time.seconds +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 TimeSpanAsSecondsSerializer : KSerializer { + override val descriptor: SerialDescriptor + get() = Long.serializer().descriptor + + override fun deserialize(decoder: Decoder): TimeSpan { + return decoder.decodeLong().seconds + } + + override fun serialize(encoder: Encoder, value: TimeSpan) { + encoder.encodeLong(value.inWholeSeconds) + } +} \ No newline at end of file