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 f983b5d4e4..6fa55a23af 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 @@ -3,8 +3,7 @@ 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 -import dev.inmo.tgbotapi.webapps.popup.ClosePopupCallback -import dev.inmo.tgbotapi.webapps.popup.PopupParams +import dev.inmo.tgbotapi.webapps.popup.* external class WebApp { val version: String @@ -158,3 +157,32 @@ fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).ch initData, initDataUnsafe.hash ) + +fun WebApp.showPopup( + message: String, + title: String?, + buttons: Array, + callback: ClosePopupCallback? = null +) = showPopup( + PopupParams( + message, + title, + buttons + ), + callback +) + +fun WebApp.showPopup( + message: String, + title: String?, + firstButton: PopupButton, + vararg otherButtons: PopupButton, + callback: ClosePopupCallback? = null +) = showPopup( + PopupParams( + message, + title, + arrayOf(firstButton, *otherButtons) + ), + callback +) diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt index 65f2513ef1..992a70c1cb 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt @@ -1,15 +1,25 @@ package dev.inmo.tgbotapi.webapps.popup -external class PopupButton( - id: String, - type: String, - text: String? = definedExternally -) { +import kotlin.js.json + +external interface PopupButton { val id: String - val type: String + val type: PopupButtonType val text: String? } +fun PopupButton( + id: String, + type: PopupButtonType, + text: String? = null +) = json( + *listOfNotNull( + "id" to id, + "type" to type.typeName, + ("text" to text).takeIf { text != null } + ).toTypedArray() +).unsafeCast() + value class PopupButtonType( val typeName: String ) { @@ -25,21 +35,21 @@ value class PopupButtonType( fun DefaultPopupButton( id: String, text: String -) = PopupButton(id, PopupButtonType.Default.typeName, text) +) = PopupButton(id, PopupButtonType.Default, text) fun OkPopupButton( id: String -) = PopupButton(id, PopupButtonType.Ok.typeName) +) = PopupButton(id, PopupButtonType.Ok) fun ClosePopupButton( id: String -) = PopupButton(id, PopupButtonType.Close.typeName) +) = PopupButton(id, PopupButtonType.Close) fun CancelPopupButton( id: String -) = PopupButton(id, PopupButtonType.Cancel.typeName) +) = PopupButton(id, PopupButtonType.Cancel) fun DestructivePopupButton( id: String, text: String -) = PopupButton(id, PopupButtonType.Destructive.typeName, text) +) = PopupButton(id, PopupButtonType.Destructive, text) diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt index 42f00aac22..8fe40507cb 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt @@ -1,12 +1,24 @@ package dev.inmo.tgbotapi.webapps.popup -external class PopupParams( +import kotlin.js.json + +external interface PopupParams { + val message: String + val title: String? + val buttons: Array +} + +fun PopupParams( message: String, title: String?, buttons: Array -) { - val title: String? -} +) = json( + *listOfNotNull( + "message" to message, + "buttons" to buttons, + ("title" to title).takeIf { title != null } + ).toTypedArray() +).unsafeCast() fun PopupParams( message: String,