This commit is contained in:
2022-05-04 13:27:39 +06:00
parent df778b4e93
commit 831b558724
4 changed files with 17 additions and 24 deletions

View File

@@ -2,6 +2,7 @@ import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.tgbotapi.types.webAppQueryIdField
import dev.inmo.tgbotapi.webapps.*
import io.ktor.client.HttpClient
import io.ktor.client.call.receive
import io.ktor.client.request.*
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.readText
@@ -14,41 +15,36 @@ import kotlinx.dom.appendElement
import kotlinx.dom.appendText
import org.w3c.dom.HTMLElement
fun HTMLElement.log(text: String) {
appendElement("p", {})
appendText(text)
}
fun main() {
console.log("Web app started")
window.onload = {
val scope = CoroutineScope(Dispatchers.Default)
runCatching {
document.body ?.appendElement("button") {
(this as HTMLElement).onclick = {
addEventListener("click", {
scope.launchSafelyWithoutExceptions {
HttpClient().post<HttpResponse>("${window.location.origin.removeSuffix("/")}/inline") {
parameter(webAppQueryIdField, webApp.initDataUnsafe.queryId)
body = "Clicked"
contentType(ContentType.Text.Plain)
}
handleResult(
{ "Clicked" }
) {
HttpClient().post<HttpResponse>(
"${window.location.origin}/inline"
) {
handleResult({ "Clicked" }) {
HttpClient().post<HttpResponse>("${window.location.origin.removeSuffix("/")}/inline") {
parameter(webAppQueryIdField, it)
body = "Clicked"
}
body = TextContent("Clicked", ContentType.Text.Plain)
document.body ?.log(url.build().toString())
}.coroutineContext.job.join()
}
}
}
})
appendText("Example button")
} ?: window.alert("Unable to load body")
webApp.apply {
onThemeChanged {
document.body ?.appendText("Theme changed: ${webApp.themeParams}")
document.body ?.appendElement("p", {})
document.body ?.log("Theme changed: ${webApp.themeParams}")
}
onViewportChanged {
document.body ?.appendText("Viewport changed: ${it.isStateStable}")
document.body ?.appendElement("p", {})
document.body ?.log("Viewport changed: ${it.isStateStable}")
}
}
webApp.ready()