1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt

83 lines
2.4 KiB
Kotlin
Raw Normal View History

2022-04-18 11:05:28 +00:00
package dev.inmo.tgbotapi.webapps
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
2022-04-18 11:26:09 +00:00
2022-04-18 11:05:28 +00:00
external class WebApp {
val initData: String
val initDataUnsafe: WebAppInitData
@JsName("colorScheme")
val colorSchemeRaw: String
val themeParams: ThemeParams
val isExpanded: Boolean
val viewportHeight: Float
val viewportStableHeight: Float
@JsName("MainButton")
val mainButton: MainButton
internal fun onEvent(type: String, callback: () -> Unit)
@JsName("onEvent")
2022-04-19 09:14:59 +00:00
internal fun onEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit)
2022-04-18 11:05:28 +00:00
fun offEvent(type: String, callback: () -> Unit)
@JsName("offEvent")
2022-04-19 09:14:59 +00:00
fun offEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit)
2022-04-18 11:05:28 +00:00
fun sendData(data: String)
fun ready()
fun expand()
fun close()
}
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 {
onEventWithBoolean(
type.typeName,
callback = it
)
}
/**
* @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-04-18 11:26:09 +00:00
2022-05-18 10:31:14 +00:00
fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData(
initData,
initDataUnsafe.hash
)