mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-09-06 00:29:59 +00:00
add support of data checking in webapp
This commit is contained in:
@@ -2,10 +2,8 @@ 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
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.content.TextContent
|
||||
import kotlinx.browser.document
|
||||
@@ -13,6 +11,7 @@ import kotlinx.browser.window
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.dom.appendElement
|
||||
import kotlinx.dom.appendText
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.w3c.dom.HTMLElement
|
||||
|
||||
fun HTMLElement.log(text: String) {
|
||||
@@ -22,14 +21,38 @@ fun HTMLElement.log(text: String) {
|
||||
|
||||
fun main() {
|
||||
console.log("Web app started")
|
||||
val client = HttpClient()
|
||||
val baseUrl = window.location.origin.removeSuffix("/")
|
||||
|
||||
window.onload = {
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
runCatching {
|
||||
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
val response = client.post("$baseUrl/check") {
|
||||
setBody(
|
||||
Json { }.encodeToString(
|
||||
WebAppDataWrapper.serializer(),
|
||||
WebAppDataWrapper(webApp.initData, webApp.initDataUnsafe.hash)
|
||||
)
|
||||
)
|
||||
}
|
||||
val dataIsSafe = response.bodyAsText().toBoolean()
|
||||
|
||||
document.body ?.appendElement("div") {
|
||||
textContent = if (dataIsSafe) {
|
||||
"Data is safe"
|
||||
} else {
|
||||
"Data is unsafe"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
document.body ?.appendElement("button") {
|
||||
addEventListener("click", {
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
handleResult({ "Clicked" }) {
|
||||
HttpClient().post("${window.location.origin.removeSuffix("/")}/inline") {
|
||||
client.post("${window.location.origin.removeSuffix("/")}/inline") {
|
||||
parameter(webAppQueryIdField, it)
|
||||
setBody(TextContent("Clicked", ContentType.Text.Plain))
|
||||
document.body ?.log(url.build().toString())
|
||||
|
Reference in New Issue
Block a user