From df778b4e9311497e2b0e6b28e2d2ca7efe70d770 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 4 May 2022 12:22:49 +0600 Subject: [PATCH 1/2] start migration onto 0.38.20 --- WebApp/src/jsMain/kotlin/main.kt | 31 +++++++++++---- WebApp/src/jvmMain/kotlin/WebAppServer.kt | 46 ++++++++++++++++++++--- gradle.properties | 2 +- 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/WebApp/src/jsMain/kotlin/main.kt b/WebApp/src/jsMain/kotlin/main.kt index cb4c5bc..e157e65 100644 --- a/WebApp/src/jsMain/kotlin/main.kt +++ b/WebApp/src/jsMain/kotlin/main.kt @@ -1,14 +1,18 @@ +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.request.get +import io.ktor.client.request.* import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.readText -import io.ktor.http.encodeURLPath +import io.ktor.http.* +import io.ktor.http.content.TextContent import kotlinx.browser.document import kotlinx.browser.window import kotlinx.coroutines.* import kotlinx.dom.appendElement import kotlinx.dom.appendText +import org.w3c.dom.HTMLElement fun main() { console.log("Web app started") @@ -16,12 +20,25 @@ fun main() { val scope = CoroutineScope(Dispatchers.Default) runCatching { document.body ?.appendElement("button") { - addEventListener( - "click", - { - webApp.sendData("Clicked") + (this as HTMLElement).onclick = { + scope.launchSafelyWithoutExceptions { + HttpClient().post("${window.location.origin.removeSuffix("/")}/inline") { + parameter(webAppQueryIdField, webApp.initDataUnsafe.queryId) + body = "Clicked" + contentType(ContentType.Text.Plain) + } + handleResult( + { "Clicked" } + ) { + HttpClient().post( + "${window.location.origin}/inline" + ) { + parameter(webAppQueryIdField, it) + body = "Clicked" + } + } } - ) + } appendText("Example button") } ?: window.alert("Unable to load body") webApp.apply { diff --git a/WebApp/src/jvmMain/kotlin/WebAppServer.kt b/WebApp/src/jvmMain/kotlin/WebAppServer.kt index bd05342..04b5174 100644 --- a/WebApp/src/jvmMain/kotlin/WebAppServer.kt +++ b/WebApp/src/jvmMain/kotlin/WebAppServer.kt @@ -1,14 +1,26 @@ import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.ktor.server.createKtorServer +import dev.inmo.tgbotapi.extensions.api.answers.answer import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands import dev.inmo.tgbotapi.extensions.api.send.* +import dev.inmo.tgbotapi.extensions.api.telegramBot import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.utils.types.buttons.* +import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle +import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent +import dev.inmo.tgbotapi.types.webAppQueryIdField import dev.inmo.tgbotapi.types.webapps.WebAppInfo +import io.ktor.application.call +import io.ktor.http.HttpStatusCode import io.ktor.http.content.* +import io.ktor.request.receiveText +import io.ktor.response.respond import io.ktor.routing.* import io.ktor.server.tomcat.Tomcat +import io.ktor.utils.io.readRemaining import kotlinx.coroutines.Dispatchers import java.io.File @@ -21,6 +33,7 @@ import java.io.File * Will start the server to share the static (index.html and WebApp.js) on 0.0.0.0:8080 */ suspend fun main(vararg args: String) { + val bot = telegramBot(args.first(), testServer = args.any { it == "testServer" }) createKtorServer( Tomcat, "0.0.0.0", @@ -32,19 +45,26 @@ suspend fun main(vararg args: String) { routing { static { files(File("WebApp/build/distributions")) + default("WebApp/build/distributions/index.html") + } + post("inline") { + val requestBody = call.receiveText() + val queryId = call.parameters[webAppQueryIdField] ?: error("$webAppQueryIdField should be presented") + + bot.answer(queryId, InlineQueryResultArticle(queryId, "Result", InputTextMessageContent(requestBody))) + call.respond(HttpStatusCode.OK) } } }.start(false) - telegramBotWithBehaviourAndLongPolling( - args.first(), + bot.buildBehaviourWithLongPolling( defaultExceptionsHandler = { it.printStackTrace() } ) { - onCommand("start") { + onCommand("reply_markup") { reply( it, "Button", - replyMarkup = replyKeyboard { + replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) { row { webAppButton("Open WebApp", WebAppInfo(args[1])) } @@ -52,9 +72,25 @@ suspend fun main(vararg args: String) { ) } + onCommand("inline") { + reply( + it, + "Button", + replyMarkup = inlineKeyboard { + row { + webAppButton("Open WebApp", WebAppInfo(args[1])) + } + } + + ) + } + setMyCommands( + BotCommand("reply_markup", "Use to get reply markup keyboard with web app trigger"), + BotCommand("inline", "Use to get inline keyboard with web app trigger"), + ) allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) } println(getMe()) - }.second.join() + }.join() } diff --git a/gradle.properties b/gradle.properties index 0188a16..4c84427 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,6 @@ org.gradle.parallel=true kotlin_version=1.6.10 -telegram_bot_api_version=0.38.19 +telegram_bot_api_version=0.38.20 micro_utils_version=0.9.24 ktor_version=1.6.8 From 831b558724058211f2dbb3cf2a72bf4cef6c34ec Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 4 May 2022 13:27:39 +0600 Subject: [PATCH 2/2] fixes --- WebApp/build.gradle | 2 +- WebApp/src/jsMain/kotlin/main.kt | 34 ++++++++++------------- WebApp/src/jvmMain/kotlin/WebAppServer.kt | 3 -- gradle.properties | 2 +- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/WebApp/build.gradle b/WebApp/build.gradle index 7f73e5f..334217c 100644 --- a/WebApp/build.gradle +++ b/WebApp/build.gradle @@ -38,7 +38,7 @@ kotlin { dependencies { implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version" - implementation "io.ktor:ktor-server-tomcat:$ktor_version" + implementation "io.ktor:ktor-server-cio:$ktor_version" } } } diff --git a/WebApp/src/jsMain/kotlin/main.kt b/WebApp/src/jsMain/kotlin/main.kt index e157e65..ec54816 100644 --- a/WebApp/src/jsMain/kotlin/main.kt +++ b/WebApp/src/jsMain/kotlin/main.kt @@ -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("${window.location.origin.removeSuffix("/")}/inline") { - parameter(webAppQueryIdField, webApp.initDataUnsafe.queryId) - body = "Clicked" - contentType(ContentType.Text.Plain) - } - handleResult( - { "Clicked" } - ) { - HttpClient().post( - "${window.location.origin}/inline" - ) { + handleResult({ "Clicked" }) { + HttpClient().post("${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() diff --git a/WebApp/src/jvmMain/kotlin/WebAppServer.kt b/WebApp/src/jvmMain/kotlin/WebAppServer.kt index 04b5174..f97fe17 100644 --- a/WebApp/src/jvmMain/kotlin/WebAppServer.kt +++ b/WebApp/src/jvmMain/kotlin/WebAppServer.kt @@ -19,8 +19,6 @@ import io.ktor.http.content.* import io.ktor.request.receiveText import io.ktor.response.respond import io.ktor.routing.* -import io.ktor.server.tomcat.Tomcat -import io.ktor.utils.io.readRemaining import kotlinx.coroutines.Dispatchers import java.io.File @@ -35,7 +33,6 @@ import java.io.File suspend fun main(vararg args: String) { val bot = telegramBot(args.first(), testServer = args.any { it == "testServer" }) createKtorServer( - Tomcat, "0.0.0.0", 8080, additionalEngineEnvironmentConfigurator = { diff --git a/gradle.properties b/gradle.properties index 4c84427..8b4971b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,6 @@ org.gradle.parallel=true kotlin_version=1.6.10 -telegram_bot_api_version=0.38.20 +telegram_bot_api_version=0.38.21 micro_utils_version=0.9.24 ktor_version=1.6.8