From f2e9bf6bd8dc225615bc7afbec2248a887da2a5c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 16:27:53 +0600 Subject: [PATCH] update webapps support --- CHANGELOG.md | 2 + .../tgbotapi/types/chat/ChatSerializers.kt | 11 +++- .../dev/inmo/tgbotapi/types/chat/Extended.kt | 4 +- .../tgbotapi/types/chat/UnknownChatType.kt | 5 +- .../dev/inmo/tgbotapi/webapps/BackButton.kt | 11 ++++ .../dev/inmo/tgbotapi/webapps/Colors.kt | 20 +++++++ .../dev/inmo/tgbotapi/webapps/EventHandler.kt | 3 + .../dev/inmo/tgbotapi/webapps/EventType.kt | 3 + .../dev/inmo/tgbotapi/webapps/HEXColor.kt | 3 + .../dev/inmo/tgbotapi/webapps/MainButton.kt | 1 + .../dev/inmo/tgbotapi/webapps/ThemeParams.kt | 29 ++++++++-- .../dev/inmo/tgbotapi/webapps/WebApp.kt | 55 ++++++++++++++++++- .../dev/inmo/tgbotapi/webapps/WebAppChat.kt | 14 +++++ .../inmo/tgbotapi/webapps/WebAppInitData.kt | 5 ++ .../tgbotapi/webapps/haptic/HapticFeedback.kt | 7 +++ .../webapps/haptic/HapticFeedbackStyle.kt | 17 ++++++ .../webapps/haptic/HapticFeedbackType.kt | 15 +++++ .../webapps/invoice/InvoiceClosedInfo.kt | 16 ++++++ .../tgbotapi/webapps/invoice/InvoiceStatus.kt | 10 ++++ 19 files changed, 217 insertions(+), 14 deletions(-) create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 20aeea7539..979a83195a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 2.1.0 +* Add support of functionality for `WebApp`s from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) + ## 2.0.3 * `Core`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt index 30adce6407..3d887f8ffe 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt @@ -65,7 +65,8 @@ object PreviewChatSerializer : KSerializer { ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) is ChatType.UnknownChatType -> UnknownChatType( formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), - decodedJson.toString() + decodedJson.toString(), + decodedJson ) } } @@ -77,6 +78,10 @@ object PreviewChatSerializer : KSerializer { is GroupChatImpl -> GroupChatImpl.serializer().serialize(encoder, value) is SupergroupChatImpl -> SupergroupChatImpl.serializer().serialize(encoder, value) is ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, value) + is CommonBot -> CommonBot.serializer().serialize(encoder, value) + is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, value) + is CommonUser -> CommonUser.serializer().serialize(encoder, value) + is UnknownChatType -> JsonObject.serializer().serialize(encoder, value.rawJson) } } } @@ -99,7 +104,8 @@ object ExtendedChatSerializer : KSerializer { ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson) is ChatType.UnknownChatType -> UnknownExtendedChat( formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), - decodedJson.toString() + decodedJson.toString(), + decodedJson ) } } @@ -110,6 +116,7 @@ object ExtendedChatSerializer : KSerializer { is ExtendedGroupChatImpl -> ExtendedGroupChatImpl.serializer().serialize(encoder, value) is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value) is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.serializer().serialize(encoder, value) + is UnknownExtendedChat -> JsonObject.serializer().serialize(encoder, value.rawJson) } } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 0dc5c4a1ee..484300e31f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject @Serializable data class ExtendedChannelChatImpl( @@ -119,7 +120,8 @@ data class ExtendedBot( data class UnknownExtendedChat( override val id: ChatId, - val raw: String + val raw: String, + val rawJson: JsonObject ) : ExtendedChat { override val chatPhoto: ChatPhoto? = null } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt index 01f146c96c..5e9e9f4f01 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt @@ -1,9 +1,10 @@ package dev.inmo.tgbotapi.types.chat import dev.inmo.tgbotapi.types.ChatId -import dev.inmo.tgbotapi.types.chat.Chat +import kotlinx.serialization.json.JsonObject data class UnknownChatType( override val id: ChatId, - val raw: String + val raw: String, + val rawJson: JsonObject ) : Chat diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt new file mode 100644 index 0000000000..ccc12b1408 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt @@ -0,0 +1,11 @@ +package dev.inmo.tgbotapi.webapps + +external interface BackButton { + val isVisible: Boolean + + fun onClick(callback: () -> Unit) + fun offClick(callback: () -> Unit) + + fun show() + fun hide() +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt new file mode 100644 index 0000000000..7c61897267 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt @@ -0,0 +1,20 @@ +package dev.inmo.tgbotapi.webapps + +import kotlinx.serialization.Serializable + +sealed interface Color { + val value: String + @Serializable + value class BackgroundColor(override val value: String) : Color + + @Serializable + value class Hex(override val value: String) : Color + + companion object { + val BackgroundColor = BackgroundColor("bg_color") + val SecondaryBackgroundColor = BackgroundColor("secondary_bg_color") + + @Suppress("NOTHING_TO_INLINE") + inline operator fun invoke(value: String) = Hex(value) + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt index 4d4e82a125..e0e5dc7f37 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt @@ -1,4 +1,7 @@ package dev.inmo.tgbotapi.webapps +import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo + typealias EventHandler = WebApp.() -> Unit typealias ViewportChangedEventHandler = WebApp.(ViewportChangedData) -> Unit +typealias InvoiceClosedEventHandler = WebApp.(InvoiceClosedInfo) -> Unit diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt index 1771966db3..a75b20218c 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt @@ -4,4 +4,7 @@ sealed class EventType(val typeName: String) { object ThemeChanged : EventType("themeChanged") object ViewportChanged : EventType("viewportChanged") object MainButtonClicked : EventType("mainButtonClicked") + object BackButtonClicked : EventType("backButtonClicked") + object SettingsButtonClicked : EventType("settingsButtonClicked") + object InvoiceClosed : EventType("invoiceClosed") } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt new file mode 100644 index 0000000000..d4d6e9bf56 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps + +typealias HEXColor = String diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt index 3fabb5a2e7..2cb4ab06dc 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt @@ -23,6 +23,7 @@ external class MainButton { fun hideProgress(): MainButton internal fun onClick(eventHandler: () -> Unit): MainButton + internal fun offClick(eventHandler: () -> Unit): MainButton internal fun setParams(params: Json): MainButton } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt index a8db758c55..b72c7b5f40 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt @@ -2,15 +2,32 @@ package dev.inmo.tgbotapi.webapps external interface ThemeParams { @JsName("bg_color") - val backgroundColor: String? + val backgroundColor: HEXColor? + @JsName("secondary_bg_color") + val secondaryBackgroundColor: HEXColor? @JsName("text_color") - val textColor: String? + val textColor: HEXColor? @JsName("hint_color") - val hintColor: String? + val hintColor: HEXColor? @JsName("link_color") - val linkColor: String? + val linkColor: HEXColor? @JsName("button_color") - val buttonColor: String? + val buttonColor: HEXColor? @JsName("button_text_color") - val buttonTextColor: String? + val buttonTextColor: HEXColor? + + @JsName("bg_color") + val backgroundColorHex: Color.Hex? + @JsName("secondary_bg_color") + val secondaryBackgroundColorHex: Color.Hex? + @JsName("text_color") + val textColorHex: Color.Hex? + @JsName("hint_color") + val hintColorHex: Color.Hex? + @JsName("link_color") + val linkColorHex: Color.Hex? + @JsName("button_color") + val buttonColorHex: Color.Hex? + @JsName("button_text_color") + val buttonTextColorHex: Color.Hex? } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt index 356282cf93..365d07af4b 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt @@ -1,11 +1,21 @@ package dev.inmo.tgbotapi.webapps import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper +import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback +import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo external class WebApp { + val version: String + val initData: String val initDataUnsafe: WebAppInitData + val headerColor: HEXColor? + fun setHeaderColor(color: Color.BackgroundColor) + val backgroundColor: HEXColor? + fun setBackgroundColor(color: Color.Hex) + fun setBackgroundColor(color: Color.BackgroundColor) + @JsName("colorScheme") val colorSchemeRaw: String val themeParams: ThemeParams @@ -17,19 +27,34 @@ external class WebApp { @JsName("MainButton") val mainButton: MainButton + @JsName("BackButton") + val backButton: BackButton + + @JsName("HapticFeedback") + val hapticFeedback: HapticFeedback + internal fun onEvent(type: String, callback: () -> Unit) @JsName("onEvent") - internal fun onEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit) + internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit) + @JsName("onEvent") + internal fun onEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit) fun offEvent(type: String, callback: () -> Unit) @JsName("offEvent") - fun offEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit) + fun offEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit) + @JsName("offEvent") + fun offEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit) fun sendData(data: String) fun ready() fun expand() fun close() + + fun isVersionAtLeast(version: String): Boolean + fun openLink(url: String) + fun openTelegramLink(url: String) + fun openInvoice(url: String, callback: (InvoiceClosedInfo) -> Unit = definedExternally) } val WebApp.colorScheme: ColorScheme @@ -57,7 +82,19 @@ fun WebApp.onEvent(type: EventType, eventHandler: EventHandler) = { fun WebApp.onEvent(type: EventType.ViewportChanged, eventHandler: ViewportChangedEventHandler) = { it: ViewportChangedData -> eventHandler(js("this").unsafeCast(), it) }.also { - onEventWithBoolean( + onEventWithViewportChangedData( + type.typeName, + callback = it + ) +} + +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onEvent(type: EventType.InvoiceClosed, eventHandler: InvoiceClosedEventHandler) = { it: InvoiceClosedInfo -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithInvoiceClosedInfo( type.typeName, callback = it ) @@ -75,6 +112,18 @@ fun WebApp.onMainButtonClicked(eventHandler: EventHandler) = onEvent(EventType.M * @return The callback which should be used in case you want to turn off events handling */ fun WebApp.onViewportChanged(eventHandler: ViewportChangedEventHandler) = onEvent(EventType.ViewportChanged, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onBackButtonClicked(eventHandler: EventHandler) = onEvent(EventType.BackButtonClicked, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onSettingsButtonClicked(eventHandler: EventHandler) = onEvent(EventType.SettingsButtonClicked, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onInvoiceClosed(eventHandler: InvoiceClosedEventHandler) = onEvent(EventType.InvoiceClosed, eventHandler) fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData( initData, diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt new file mode 100644 index 0000000000..e0dfa35de9 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt @@ -0,0 +1,14 @@ +package dev.inmo.tgbotapi.webapps + +import dev.inmo.tgbotapi.types.ChatIdentifier +import dev.inmo.tgbotapi.types.Username +import dev.inmo.tgbotapi.types.chat.PublicChat + +external interface WebAppChat { + val id: ChatIdentifier + val type: String + val title: String + val username: Username? + @JsName("photo_url") + val photoUrl: String? +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt index a561cfb89a..78c7528233 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt @@ -15,5 +15,10 @@ external interface WebAppInitData { @JsName("auth_date") val authDate: MilliSeconds + @JsName("can_send_after") + val canSendAfter: MilliSeconds + + val chat: WebAppChat + val hash: String } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt new file mode 100644 index 0000000000..dad3bae8af --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt @@ -0,0 +1,7 @@ +package dev.inmo.tgbotapi.webapps.haptic + +external interface HapticFeedback { + fun impactOccurred(style: HapticFeedbackStyle) + fun notificationOccurred(type: HapticFeedbackType) + fun selectionChanged() +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt new file mode 100644 index 0000000000..bee1ea0c2f --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt @@ -0,0 +1,17 @@ +package dev.inmo.tgbotapi.webapps.haptic + +import dev.inmo.micro_utils.common.Warning +import kotlinx.serialization.Serializable + +@Serializable +value class HapticFeedbackStyle @Warning("Do not use this constructor if available objects from companion cover your needs") constructor( + val name: String +) { + companion object { + val Light = HapticFeedbackStyle("light") + val Medium = HapticFeedbackStyle("medium") + val Heavy = HapticFeedbackStyle("heavy") + val Rigid = HapticFeedbackStyle("rigid") + val Soft = HapticFeedbackStyle("soft") + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt new file mode 100644 index 0000000000..940b20d518 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt @@ -0,0 +1,15 @@ +package dev.inmo.tgbotapi.webapps.haptic + +import dev.inmo.micro_utils.common.Warning +import kotlinx.serialization.Serializable + +@Serializable +value class HapticFeedbackType @Warning("Do not use this constructor if available objects from companion cover your needs") constructor( + val name: String +) { + companion object { + val Error = HapticFeedbackType("error") + val Success = HapticFeedbackType("success") + val Warning = HapticFeedbackType("warning") + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt new file mode 100644 index 0000000000..39feb3087a --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt @@ -0,0 +1,16 @@ +package dev.inmo.tgbotapi.webapps.invoice + +external interface InvoiceClosedInfo { + val url: String + val status: String +} + +val InvoiceClosedInfo.statusTyped + get() = when (status) { + InvoiceStatus.Paid.name -> InvoiceStatus.Paid + InvoiceStatus.Cancelled.name -> InvoiceStatus.Cancelled + InvoiceStatus.Failed.name -> InvoiceStatus.Failed + InvoiceStatus.Pending.name -> InvoiceStatus.Pending + else -> InvoiceStatus.Unknown(status) + } + diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt new file mode 100644 index 0000000000..6f685021d7 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt @@ -0,0 +1,10 @@ +package dev.inmo.tgbotapi.webapps.invoice + +sealed interface InvoiceStatus { + val name: String + object Paid : InvoiceStatus { override val name: String = "paid" } + object Cancelled : InvoiceStatus { override val name: String = "cancelled" } + object Failed : InvoiceStatus { override val name: String = "failed" } + object Pending : InvoiceStatus { override val name: String = "pending" } + value class Unknown(override val name: String) : InvoiceStatus +}