1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-25 01:57:13 +00:00

add preview callbacks support

This commit is contained in:
InsanusMokrassar 2023-09-23 00:40:32 +06:00
parent 1b1da33882
commit fa4d264df4
3 changed files with 41 additions and 0 deletions

View File

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

View File

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

View File

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