mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
webapps
This commit is contained in:
parent
ffcc149c40
commit
705bcbe352
@ -6,6 +6,8 @@ __This update contains including of [Telegram Bot API 6.0](https://core.telegram
|
|||||||
|
|
||||||
* `Core`:
|
* `Core`:
|
||||||
* Constructor of `UnknownInlineKeyboardButton` is not internal and can be created with any `json`
|
* Constructor of `UnknownInlineKeyboardButton` is not internal and can be created with any `json`
|
||||||
|
* `WebApps`:
|
||||||
|
* Created 🎉
|
||||||
|
|
||||||
## 0.38.13
|
## 0.38.13
|
||||||
|
|
||||||
|
@ -21,4 +21,5 @@ include ":tgbotapi.extensions.utils"
|
|||||||
include ":tgbotapi.extensions.behaviour_builder"
|
include ":tgbotapi.extensions.behaviour_builder"
|
||||||
include ":tgbotapi.extensions.behaviour_builder.fsm"
|
include ":tgbotapi.extensions.behaviour_builder.fsm"
|
||||||
include ":tgbotapi"
|
include ":tgbotapi"
|
||||||
|
include ":tgbotapi.webapps"
|
||||||
include ":docs"
|
include ":docs"
|
||||||
|
49
tgbotapi.webapps/build.gradle
Normal file
49
tgbotapi.webapps/build.gradle
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
}
|
||||||
|
|
||||||
|
project.version = "$library_version"
|
||||||
|
project.group = "$library_group"
|
||||||
|
project.description = "Web App bindings for the Telegram Web Apps API"
|
||||||
|
|
||||||
|
apply from: "../publish.gradle"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
js(IR) {
|
||||||
|
browser()
|
||||||
|
nodejs()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
api project(":tgbotapi.core")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
enum class ColorScheme {
|
||||||
|
LIGHT,
|
||||||
|
DARK
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
typealias EventHandler = WebApp.() -> Unit
|
||||||
|
typealias ViewportChangedEventHandler = WebApp.(Boolean) -> Unit
|
@ -0,0 +1,7 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
sealed class EventType(val typeName: String) {
|
||||||
|
object ThemeChanged : EventType("themeChanged")
|
||||||
|
object ViewportChanged : EventType("viewportChanged")
|
||||||
|
object MainButtonClicked : EventType("mainButtonClicked")
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
import kotlin.js.Json
|
||||||
|
import kotlin.js.json
|
||||||
|
|
||||||
|
external class MainButton {
|
||||||
|
val text: String
|
||||||
|
fun setText(text: String): MainButton
|
||||||
|
|
||||||
|
var color: String
|
||||||
|
var textColor: String
|
||||||
|
|
||||||
|
val isVisible: Boolean
|
||||||
|
fun show(): MainButton
|
||||||
|
fun hide(): MainButton
|
||||||
|
|
||||||
|
val isActive: Boolean
|
||||||
|
fun enable(): MainButton
|
||||||
|
fun disable(): MainButton
|
||||||
|
|
||||||
|
val isProgressVisible: Boolean
|
||||||
|
fun showProgress(leaveActive: Boolean = definedExternally): MainButton
|
||||||
|
fun hideProgress(): MainButton
|
||||||
|
|
||||||
|
internal fun onClick(eventHandler: () -> Unit): MainButton
|
||||||
|
|
||||||
|
internal fun setParams(params: Json): MainButton
|
||||||
|
}
|
||||||
|
|
||||||
|
data class MainButtonParams(
|
||||||
|
val text: String? = null,
|
||||||
|
val color: String? = null,
|
||||||
|
val textColor: String? = null,
|
||||||
|
val isActive: Boolean? = null,
|
||||||
|
val isVisible: Boolean? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
fun MainButton.onClick(eventHandler: EventHandler) = onClick {
|
||||||
|
val that = js("this").unsafeCast<WebApp>()
|
||||||
|
that.eventHandler()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MainButton.setParams(params: MainButtonParams) = setParams(
|
||||||
|
json(
|
||||||
|
*listOfNotNull(
|
||||||
|
params.text ?.let { "text" to params.text },
|
||||||
|
params.color ?.let { "color" to params.color },
|
||||||
|
params.textColor ?.let { "text_color" to params.textColor },
|
||||||
|
params.isActive ?.let { "is_active" to params.isActive },
|
||||||
|
params.isVisible ?.let { "is_visible" to params.isVisible },
|
||||||
|
).toTypedArray()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
import org.w3c.dom.Window
|
||||||
|
|
||||||
|
external interface Telegram {
|
||||||
|
val WebApp: WebApp
|
||||||
|
}
|
||||||
|
|
||||||
|
val Window.Telegram
|
||||||
|
get() = asDynamic().Telegram.unsafeCast<Telegram>()
|
@ -0,0 +1,16 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
external interface ThemeParams {
|
||||||
|
@JsName("bg_color")
|
||||||
|
val backgroundColor: String?
|
||||||
|
@JsName("text_color")
|
||||||
|
val textColor: String?
|
||||||
|
@JsName("hint_color")
|
||||||
|
val hintColor: String?
|
||||||
|
@JsName("link_color")
|
||||||
|
val linkColor: String?
|
||||||
|
@JsName("button_color")
|
||||||
|
val buttonColor: String?
|
||||||
|
@JsName("button_text_color")
|
||||||
|
val buttonTextColor: String?
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
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")
|
||||||
|
internal fun onEventWithBoolean(type: String, callback: (Boolean) -> Unit)
|
||||||
|
|
||||||
|
fun offEvent(type: String, callback: () -> Unit)
|
||||||
|
@JsName("offEvent")
|
||||||
|
fun offEventWithBoolean(type: String, callback: (Boolean) -> Unit)
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
fun WebApp.onEvent(type: EventType.ViewportChanged, eventHandler: ViewportChangedEventHandler) = { it: Boolean ->
|
||||||
|
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)
|
@ -0,0 +1,19 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.types.MilliSeconds
|
||||||
|
import dev.inmo.tgbotapi.types.WebAppQueryId
|
||||||
|
|
||||||
|
external interface WebAppInitData {
|
||||||
|
val queryId: WebAppQueryId?
|
||||||
|
|
||||||
|
val user: WebAppUser?
|
||||||
|
val receiver: WebAppUser?
|
||||||
|
|
||||||
|
@JsName("start_param")
|
||||||
|
val startParam: String?
|
||||||
|
|
||||||
|
@JsName("auth_date")
|
||||||
|
val authDate: MilliSeconds
|
||||||
|
|
||||||
|
val hash: String
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
|
||||||
|
import dev.inmo.tgbotapi.types.*
|
||||||
|
|
||||||
|
external interface WebAppUser {
|
||||||
|
val id: Identifier
|
||||||
|
@JsName(isBotField)
|
||||||
|
val isBot: Boolean?
|
||||||
|
@JsName(firstNameField)
|
||||||
|
val firstName: String
|
||||||
|
@JsName(lastNameField)
|
||||||
|
val lastName: String?
|
||||||
|
@JsName(usernameField)
|
||||||
|
val username: String?
|
||||||
|
@JsName(languageCodeField)
|
||||||
|
val languageCode: String?
|
||||||
|
@JsName(photoUrlField)
|
||||||
|
val photoUrl: String?
|
||||||
|
}
|
||||||
|
|
||||||
|
fun WebAppUser.asUser() = if (isBot == true) {
|
||||||
|
CommonBot(
|
||||||
|
UserId(id),
|
||||||
|
username ?.let(::Username) ?: error("Username is absent for bot, but must exists"),
|
||||||
|
firstName,
|
||||||
|
lastName ?: ""
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
CommonUser(
|
||||||
|
UserId(id),
|
||||||
|
firstName,
|
||||||
|
lastName ?: "",
|
||||||
|
username ?.let(::Username),
|
||||||
|
languageCode ?.let(::IetfLanguageCode)
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user