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 9796336908..ae62aaf2b9 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 @@ -8,3 +8,5 @@ typealias InvoiceClosedEventHandler = WebApp.(InvoiceClosedInfo) -> Unit typealias PopupClosedEventHandler = WebApp.(String?) -> Unit typealias QRTextReceivedEventHandler = WebApp.(String) -> Boolean typealias TextReceivedEventHandler = WebApp.(String) -> Unit +typealias WriteAccessRequestedHandler = WebApp.(Boolean) -> Unit +typealias ContactRequestedHandler = WebApp.(Boolean) -> 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 853d63f730..d40cd1cb85 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 @@ -10,4 +10,6 @@ sealed class EventType(val typeName: String) { object PopupClosed : EventType("popupClosed") object QRTextReceived : EventType("qrTextReceived") object ClipboardTextReceived : EventType("clipboardTextReceived") + object WriteAccessRequested : EventType("writeAccessRequested") + object ContactRequested : EventType("contactRequested") } 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 f5f09fc97c..44c4238bd7 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 @@ -64,6 +64,8 @@ external class WebApp { internal fun onEventWithQRTextInfo(type: String, callback: (String) -> Boolean) @JsName("onEvent") internal fun onEventWithTextInfo(type: String, callback: (String) -> Unit) + internal fun onEventWithWriteAccessRequested(type: String, callback: (Boolean) -> Unit) + internal fun onEventWithContactRequested(type: String, callback: (Boolean) -> Unit) fun offEvent(type: String, callback: () -> Unit) @JsName("offEvent") @@ -81,6 +83,9 @@ external class WebApp { fun openLink(url: String) fun openTelegramLink(url: String) fun openInvoice(url: String, callback: (InvoiceClosedInfo) -> Unit = definedExternally) + + fun requestWriteAccess(callback: ((Boolean) -> Unit)? = definedExternally) + fun requestContact(callback: ((Boolean) -> Unit)? = definedExternally) } val WebApp.colorScheme: ColorScheme @@ -162,6 +167,30 @@ fun WebApp.onEvent(type: EventType.ClipboardTextReceived, eventHandler: TextRece ) } +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onEvent(type: EventType.WriteAccessRequested, eventHandler: WriteAccessRequestedHandler) = { it: Boolean -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithWriteAccessRequested( + 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.ContactRequested, eventHandler: ContactRequestedHandler) = { it: Boolean -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithContactRequested( + type.typeName, + callback = it + ) +} + /** * @return The callback which should be used in case you want to turn off events handling */ @@ -198,6 +227,14 @@ fun WebApp.onQRTextReceived(eventHandler: QRTextReceivedEventHandler) = onEvent( * @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) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onWriteAccessRequested(eventHandler: WriteAccessRequestedHandler) = onEvent(EventType.WriteAccessRequested, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onContactRequested(eventHandler: ContactRequestedHandler) = onEvent(EventType.ContactRequested, eventHandler) fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData( initData,