1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-24 19:18:44 +00:00

add support of Bot API 6.5 web apps

This commit is contained in:
InsanusMokrassar 2022-12-30 21:48:50 +06:00
parent bea056bba3
commit 40617ad9c8
7 changed files with 83 additions and 0 deletions

View File

@ -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

View File

@ -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")
}

View File

@ -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<OpenLinkParams>()

View File

@ -0,0 +1,3 @@
package dev.inmo.tgbotapi.webapps
typealias QRTextReceivedCallback = (String) -> Boolean

View File

@ -0,0 +1,3 @@
package dev.inmo.tgbotapi.webapps
typealias TextReceivedCallback = (String) -> Unit

View File

@ -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<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
)
}
/**
* @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,

View File

@ -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<ScanQrPopupParams>()