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 f83d2fd7dc..9796336908 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 @@ -6,3 +6,5 @@ typealias EventHandler = WebApp.() -> Unit typealias ViewportChangedEventHandler = WebApp.(ViewportChangedData) -> Unit typealias InvoiceClosedEventHandler = WebApp.(InvoiceClosedInfo) -> Unit typealias PopupClosedEventHandler = WebApp.(String?) -> Unit +typealias QRTextReceivedEventHandler = WebApp.(String) -> Boolean +typealias TextReceivedEventHandler = WebApp.(String) -> 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 ff71329074..853d63f730 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 @@ -8,4 +8,6 @@ sealed class EventType(val typeName: String) { object SettingsButtonClicked : EventType("settingsButtonClicked") object InvoiceClosed : EventType("invoiceClosed") object PopupClosed : EventType("popupClosed") + object QRTextReceived : EventType("qrTextReceived") + object ClipboardTextReceived : EventType("clipboardTextReceived") } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/OpenLinkParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/OpenLinkParams.kt new file mode 100644 index 0000000000..42578ad329 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/OpenLinkParams.kt @@ -0,0 +1,16 @@ +package dev.inmo.tgbotapi.webapps + +import kotlin.js.json + +external interface OpenLinkParams { + @JsName("try_instant_view") + val tryInstantView: Boolean +} + +fun OpenLinkParams( + tryInstantView: Boolean +) = json( + *listOfNotNull( + "try_instant_view" to tryInstantView + ).toTypedArray() +).unsafeCast() diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/QRTextReceivedCallback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/QRTextReceivedCallback.kt new file mode 100644 index 0000000000..0a82244698 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/QRTextReceivedCallback.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps + +typealias QRTextReceivedCallback = (String) -> Boolean diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/TextReceivedCallback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/TextReceivedCallback.kt new file mode 100644 index 0000000000..3ba259b773 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/TextReceivedCallback.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps + +typealias TextReceivedCallback = (String) -> Unit 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 d31d1d6cf9..b17a6a456c 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 @@ -8,6 +8,8 @@ import dev.inmo.tgbotapi.webapps.popup.* external class WebApp { val version: String + val platform: String + val initData: String val initDataUnsafe: WebAppInitData @@ -33,6 +35,9 @@ external class WebApp { fun showPopup(params: PopupParams, callback: ClosePopupCallback? = definedExternally) fun showAlert(message: String, callback: AlertCallback? = definedExternally) fun showConfirm(message: String, callback: ConfirmCallback? = definedExternally) + fun showScanQrPopup(params: ScanQrPopupParams, callback: QRTextReceivedCallback? = definedExternally) + fun closeScanQrPopup() + fun readTextFromClipboard(callback: TextReceivedCallback? = definedExternally) @JsName("MainButton") val mainButton: MainButton @@ -50,6 +55,10 @@ external class WebApp { internal fun onEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit) @JsName("onEvent") internal fun onEventWithPopupClosedInfo(type: String, callback: (String?) -> Unit) + @JsName("onEvent") + internal fun onEventWithQRTextInfo(type: String, callback: (String) -> Boolean) + @JsName("onEvent") + internal fun onEventWithTextInfo(type: String, callback: (String) -> Unit) fun offEvent(type: String, callback: () -> Unit) @JsName("offEvent") @@ -124,6 +133,30 @@ fun WebApp.onEvent(type: EventType.PopupClosed, eventHandler: PopupClosedEventHa ) } +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onEvent(type: EventType.QRTextReceived, eventHandler: QRTextReceivedEventHandler) = { it: String -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithQRTextInfo( + 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.ClipboardTextReceived, eventHandler: TextReceivedEventHandler) = { it: String -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithTextInfo( + type.typeName, + callback = it + ) +} + /** * @return The callback which should be used in case you want to turn off events handling */ @@ -152,6 +185,14 @@ fun WebApp.onInvoiceClosed(eventHandler: InvoiceClosedEventHandler) = onEvent(Ev * @return The callback which should be used in case you want to turn off events handling */ fun WebApp.onPopupClosed(eventHandler: PopupClosedEventHandler) = onEvent(EventType.PopupClosed, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onQRTextReceived(eventHandler: QRTextReceivedEventHandler) = onEvent(EventType.QRTextReceived, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onClipboardTextReceived(eventHandler: TextReceivedEventHandler) = onEvent(EventType.ClipboardTextReceived, eventHandler) fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData( initData, diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/ScanQrPopupParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/ScanQrPopupParams.kt new file mode 100644 index 0000000000..3a16e1c2f2 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/ScanQrPopupParams.kt @@ -0,0 +1,16 @@ +package dev.inmo.tgbotapi.webapps.popup + +import kotlin.js.json + + +external interface ScanQrPopupParams { + val text: String? +} + +fun ScanQrPopupParams( + text: String? = null +) = json( + *listOfNotNull( + ("text" to text).takeIf { text != null } + ).toTypedArray() +).unsafeCast()