1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

add support of Bot API 6.2 in webapp parts

This commit is contained in:
InsanusMokrassar 2022-08-14 21:52:26 +06:00
parent ad453afba2
commit d4fe4e09fc
9 changed files with 125 additions and 0 deletions

View File

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

View File

@ -0,0 +1,3 @@
package dev.inmo.tgbotapi.webapps
typealias ConfirmCallback = (confirmed: Boolean) -> Unit

View File

@ -5,3 +5,4 @@ import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo
typealias EventHandler = WebApp.() -> Unit
typealias ViewportChangedEventHandler = WebApp.(ViewportChangedData) -> Unit
typealias InvoiceClosedEventHandler = WebApp.(InvoiceClosedInfo) -> Unit
typealias PopupClosedEventHandler = WebApp.(String?) -> Unit

View File

@ -7,4 +7,5 @@ sealed class EventType(val typeName: String) {
object BackButtonClicked : EventType("backButtonClicked")
object SettingsButtonClicked : EventType("settingsButtonClicked")
object InvoiceClosed : EventType("invoiceClosed")
object PopupClosed : EventType("popupClosed")
}

View File

@ -3,6 +3,8 @@ package dev.inmo.tgbotapi.webapps
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback
import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo
import dev.inmo.tgbotapi.webapps.popup.ClosePopupCallback
import dev.inmo.tgbotapi.webapps.popup.PopupParams
external class WebApp {
val version: String
@ -24,6 +26,15 @@ external class WebApp {
val viewportHeight: Float
val viewportStableHeight: Float
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)
@JsName("MainButton")
val mainButton: MainButton
@ -38,6 +49,8 @@ external class WebApp {
internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit)
@JsName("onEvent")
internal fun onEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit)
@JsName("onEvent")
internal fun onEventWithPopupClosedInfo(type: String, callback: (String?) -> Unit)
fun offEvent(type: String, callback: () -> Unit)
@JsName("offEvent")
@ -100,6 +113,18 @@ fun WebApp.onEvent(type: EventType.InvoiceClosed, eventHandler: InvoiceClosedEve
)
}
/**
* @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
)
}
/**
* @return The callback which should be used in case you want to turn off events handling
*/
@ -124,6 +149,10 @@ fun WebApp.onSettingsButtonClicked(eventHandler: EventHandler) = onEvent(EventTy
* @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)
/**
* @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)
fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData(
initData,

View File

@ -17,10 +17,14 @@ external interface WebAppUser {
val username: String?
@JsName(languageCodeField)
val languageCode: String?
val is_premium: Boolean?
@JsName(photoUrlField)
val photoUrl: String?
}
val WebAppUser.isPremium
get() = is_premium == true
fun WebAppUser.asUser() = if (isBot == true) {
CommonBot(
UserId(id),

View File

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

View File

@ -0,0 +1,45 @@
package dev.inmo.tgbotapi.webapps.popup
external class PopupButton(
id: String,
type: String,
text: String? = definedExternally
) {
val id: String
val type: String
val text: String?
}
value class PopupButtonType(
val typeName: String
) {
companion object {
val Default = PopupButtonType("default")
val Ok = PopupButtonType("ok")
val Close = PopupButtonType("close")
val Cancel = PopupButtonType("cancel")
val Destructive = PopupButtonType("destructive")
}
}
fun DefaultPopupButton(
id: String,
text: String
) = PopupButton(id, PopupButtonType.Default.typeName, text)
fun OkPopupButton(
id: String
) = PopupButton(id, PopupButtonType.Ok.typeName)
fun ClosePopupButton(
id: String
) = PopupButton(id, PopupButtonType.Close.typeName)
fun CancelPopupButton(
id: String
) = PopupButton(id, PopupButtonType.Cancel.typeName)
fun DestructivePopupButton(
id: String,
text: String
) = PopupButton(id, PopupButtonType.Destructive.typeName, text)

View File

@ -0,0 +1,36 @@
package dev.inmo.tgbotapi.webapps.popup
external class PopupParams(
message: String,
title: String?,
buttons: Array<PopupButton>
) {
val title: String?
}
fun PopupParams(
message: String,
firstButton: PopupButton,
vararg otherButtons: PopupButton
) = PopupParams(
message,
null,
arrayOf(
firstButton,
*otherButtons
)
)
fun PopupParams(
title: String,
message: String,
firstButton: PopupButton,
vararg otherButtons: PopupButton
) = PopupParams(
message,
title,
arrayOf(
firstButton,
*otherButtons
)
)