2022-04-18 11:05:28 +00:00
|
|
|
package dev.inmo.tgbotapi.webapps
|
|
|
|
|
2022-04-29 13:11:38 +00:00
|
|
|
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
2023-09-22 18:15:58 +00:00
|
|
|
import dev.inmo.tgbotapi.webapps.cloud.CloudStorage
|
2022-06-21 10:27:53 +00:00
|
|
|
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback
|
|
|
|
import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo
|
2022-08-14 19:20:29 +00:00
|
|
|
import dev.inmo.tgbotapi.webapps.popup.*
|
2022-04-18 11:26:09 +00:00
|
|
|
|
2022-04-18 11:05:28 +00:00
|
|
|
external class WebApp {
|
2022-06-21 10:27:53 +00:00
|
|
|
val version: String
|
|
|
|
|
2022-12-30 15:48:50 +00:00
|
|
|
val platform: String
|
|
|
|
|
2022-04-18 11:05:28 +00:00
|
|
|
val initData: String
|
|
|
|
val initDataUnsafe: WebAppInitData
|
|
|
|
|
2022-06-21 10:27:53 +00:00
|
|
|
val headerColor: HEXColor?
|
|
|
|
fun setHeaderColor(color: Color.BackgroundColor)
|
2023-09-22 15:45:28 +00:00
|
|
|
fun setHeaderColor(color: Color.Hex)
|
2022-06-21 10:27:53 +00:00
|
|
|
val backgroundColor: HEXColor?
|
|
|
|
fun setBackgroundColor(color: Color.Hex)
|
|
|
|
fun setBackgroundColor(color: Color.BackgroundColor)
|
|
|
|
|
2022-04-18 11:05:28 +00:00
|
|
|
@JsName("colorScheme")
|
|
|
|
val colorSchemeRaw: String
|
|
|
|
val themeParams: ThemeParams
|
|
|
|
|
|
|
|
val isExpanded: Boolean
|
|
|
|
val viewportHeight: Float
|
|
|
|
val viewportStableHeight: Float
|
|
|
|
|
2022-08-14 15:52:26 +00:00
|
|
|
|
|
|
|
val isClosingConfirmationEnabled: Boolean
|
|
|
|
fun enableClosingConfirmation()
|
|
|
|
fun disableClosingConfirmation()
|
|
|
|
|
|
|
|
fun showPopup(params: PopupParams, callback: ClosePopupCallback? = definedExternally)
|
|
|
|
fun showAlert(message: String, callback: AlertCallback? = definedExternally)
|
|
|
|
fun showConfirm(message: String, callback: ConfirmCallback? = definedExternally)
|
2022-12-30 15:48:50 +00:00
|
|
|
fun showScanQrPopup(params: ScanQrPopupParams, callback: QRTextReceivedCallback? = definedExternally)
|
|
|
|
fun closeScanQrPopup()
|
|
|
|
fun readTextFromClipboard(callback: TextReceivedCallback? = definedExternally)
|
2022-08-14 15:52:26 +00:00
|
|
|
|
2022-04-18 11:05:28 +00:00
|
|
|
@JsName("MainButton")
|
|
|
|
val mainButton: MainButton
|
|
|
|
|
2022-06-21 10:27:53 +00:00
|
|
|
@JsName("BackButton")
|
|
|
|
val backButton: BackButton
|
|
|
|
|
|
|
|
@JsName("HapticFeedback")
|
|
|
|
val hapticFeedback: HapticFeedback
|
|
|
|
|
2023-09-22 18:15:58 +00:00
|
|
|
@JsName("CloudStorage")
|
|
|
|
val cloudStorage: CloudStorage
|
|
|
|
|
2022-04-18 11:05:28 +00:00
|
|
|
internal fun onEvent(type: String, callback: () -> Unit)
|
|
|
|
@JsName("onEvent")
|
2022-06-21 10:27:53 +00:00
|
|
|
internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit)
|
|
|
|
@JsName("onEvent")
|
|
|
|
internal fun onEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit)
|
2022-08-14 15:52:26 +00:00
|
|
|
@JsName("onEvent")
|
|
|
|
internal fun onEventWithPopupClosedInfo(type: String, callback: (String?) -> Unit)
|
2022-12-30 15:48:50 +00:00
|
|
|
@JsName("onEvent")
|
|
|
|
internal fun onEventWithQRTextInfo(type: String, callback: (String) -> Boolean)
|
|
|
|
@JsName("onEvent")
|
|
|
|
internal fun onEventWithTextInfo(type: String, callback: (String) -> Unit)
|
2023-09-22 18:40:32 +00:00
|
|
|
internal fun onEventWithWriteAccessRequested(type: String, callback: (Boolean) -> Unit)
|
|
|
|
internal fun onEventWithContactRequested(type: String, callback: (Boolean) -> Unit)
|
2022-04-18 11:05:28 +00:00
|
|
|
|
|
|
|
fun offEvent(type: String, callback: () -> Unit)
|
|
|
|
@JsName("offEvent")
|
2022-06-21 10:27:53 +00:00
|
|
|
fun offEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit)
|
|
|
|
@JsName("offEvent")
|
|
|
|
fun offEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit)
|
2022-04-18 11:05:28 +00:00
|
|
|
|
|
|
|
fun sendData(data: String)
|
|
|
|
|
|
|
|
fun ready()
|
|
|
|
fun expand()
|
|
|
|
fun close()
|
2022-06-21 10:27:53 +00:00
|
|
|
|
|
|
|
fun isVersionAtLeast(version: String): Boolean
|
|
|
|
fun openLink(url: String)
|
|
|
|
fun openTelegramLink(url: String)
|
|
|
|
fun openInvoice(url: String, callback: (InvoiceClosedInfo) -> Unit = definedExternally)
|
2023-09-22 18:40:32 +00:00
|
|
|
|
|
|
|
fun requestWriteAccess(callback: ((Boolean) -> Unit)? = definedExternally)
|
|
|
|
fun requestContact(callback: ((Boolean) -> Unit)? = definedExternally)
|
2022-04-18 11:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val WebApp.colorScheme: ColorScheme
|
|
|
|
get() = when (colorSchemeRaw) {
|
|
|
|
"light" -> ColorScheme.LIGHT
|
|
|
|
"dark" -> ColorScheme.DARK
|
|
|
|
else -> ColorScheme.LIGHT
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The callback which should be used in case you want to turn off events handling
|
|
|
|
*/
|
|
|
|
fun WebApp.onEvent(type: EventType, eventHandler: EventHandler) = {
|
|
|
|
eventHandler(js("this").unsafeCast<WebApp>())
|
|
|
|
}.also {
|
|
|
|
onEvent(
|
|
|
|
type.typeName,
|
|
|
|
callback = it
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The callback which should be used in case you want to turn off events handling
|
|
|
|
*/
|
2022-04-19 09:14:59 +00:00
|
|
|
fun WebApp.onEvent(type: EventType.ViewportChanged, eventHandler: ViewportChangedEventHandler) = { it: ViewportChangedData ->
|
2022-04-18 11:05:28 +00:00
|
|
|
eventHandler(js("this").unsafeCast<WebApp>(), it)
|
|
|
|
}.also {
|
2022-06-21 10:27:53 +00:00
|
|
|
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<WebApp>(), it)
|
|
|
|
}.also {
|
|
|
|
onEventWithInvoiceClosedInfo(
|
2022-04-18 11:05:28 +00:00
|
|
|
type.typeName,
|
|
|
|
callback = it
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-14 15:52:26 +00:00
|
|
|
/**
|
|
|
|
* @return The callback which should be used in case you want to turn off events handling
|
|
|
|
*/
|
|
|
|
fun WebApp.onEvent(type: EventType.PopupClosed, eventHandler: PopupClosedEventHandler) = { it: String? ->
|
|
|
|
eventHandler(js("this").unsafeCast<WebApp>(), it)
|
|
|
|
}.also {
|
|
|
|
onEventWithPopupClosedInfo(
|
|
|
|
type.typeName,
|
|
|
|
callback = it
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-30 15:48:50 +00:00
|
|
|
/**
|
|
|
|
* @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<WebApp>(), 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<WebApp>(), it)
|
|
|
|
}.also {
|
|
|
|
onEventWithTextInfo(
|
|
|
|
type.typeName,
|
|
|
|
callback = it
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-22 18:40:32 +00:00
|
|
|
/**
|
|
|
|
* @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<WebApp>(), 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<WebApp>(), it)
|
|
|
|
}.also {
|
|
|
|
onEventWithContactRequested(
|
|
|
|
type.typeName,
|
|
|
|
callback = it
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-18 11:05:28 +00:00
|
|
|
/**
|
|
|
|
* @return The callback which should be used in case you want to turn off events handling
|
|
|
|
*/
|
|
|
|
fun WebApp.onThemeChanged(eventHandler: EventHandler) = onEvent(EventType.ThemeChanged, eventHandler)
|
|
|
|
/**
|
|
|
|
* @return The callback which should be used in case you want to turn off events handling
|
|
|
|
*/
|
|
|
|
fun WebApp.onMainButtonClicked(eventHandler: EventHandler) = onEvent(EventType.MainButtonClicked, eventHandler)
|
|
|
|
/**
|
|
|
|
* @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)
|
2022-06-21 10:27:53 +00:00
|
|
|
/**
|
|
|
|
* @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)
|
2022-08-14 15:52:26 +00:00
|
|
|
/**
|
|
|
|
* @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)
|
2022-12-30 15:48:50 +00:00
|
|
|
/**
|
|
|
|
* @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)
|
2023-09-22 18:40:32 +00:00
|
|
|
/**
|
|
|
|
* @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)
|
2022-04-18 11:26:09 +00:00
|
|
|
|
2022-05-18 10:31:14 +00:00
|
|
|
fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData(
|
2022-04-29 13:11:38 +00:00
|
|
|
initData,
|
|
|
|
initDataUnsafe.hash
|
|
|
|
)
|
2022-08-14 19:20:29 +00:00
|
|
|
|
|
|
|
fun WebApp.showPopup(
|
|
|
|
message: String,
|
|
|
|
title: String?,
|
|
|
|
buttons: Array<PopupButton>,
|
|
|
|
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
|
|
|
|
)
|
2022-08-14 19:29:24 +00:00
|
|
|
|
|
|
|
var WebApp.requireClosingConfirmation
|
|
|
|
get() = isClosingConfirmationEnabled
|
|
|
|
set(value) {
|
|
|
|
if (value) {
|
|
|
|
enableClosingConfirmation()
|
|
|
|
} else {
|
|
|
|
disableClosingConfirmation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun WebApp.toggleClosingConfirmation() {
|
|
|
|
requireClosingConfirmation = !requireClosingConfirmation
|
|
|
|
}
|