From 1b5b3af45bb333b10df530b0440886ebc077f5c1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 4 May 2022 11:01:26 +0600 Subject: [PATCH] add handleResult --- CHANGELOG.md | 1 + .../requests/answers/AnswerWebAppQuery.kt | 3 +++ .../dev/inmo/tgbotapi/webapps/SendData.kt | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/SendData.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index c0df72cb80..7390ed2954 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Hotfixes * `WebApps`: * New extension `TelegramBot#answerWebAppQuery` + * New function `handleResult` ## 0.38.19 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerWebAppQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerWebAppQuery.kt index ceacda665f..859959aebb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerWebAppQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerWebAppQuery.kt @@ -6,6 +6,9 @@ import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQ import dev.inmo.tgbotapi.types.webapps.query.SentWebAppMessage import kotlinx.serialization.* +/** + * @param webAppQueryId [dev.inmo.tgbotapi.webapps.WebAppInitData.queryId] + */ @Serializable data class AnswerWebAppQuery( @SerialName(webAppQueryIdField) diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/SendData.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/SendData.kt new file mode 100644 index 0000000000..5f469cfd0f --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/SendData.kt @@ -0,0 +1,18 @@ +package dev.inmo.tgbotapi.webapps + +import dev.inmo.tgbotapi.types.WebAppQueryId + +/** + * @param onSendData Should return the data which must be used in [WebApp.sendData]. If returns null, data will not be sent + * @param onAnswerWebAppQuery In case if [WebAppInitData.queryId] is presented in [WebApp.initDataUnsafe], will be called + * that callback. Before and after calling of this callback will not be used any method of answering to the telegram + * system, so, you must use something like [answerWebAppQuery] by yourself to send the result + */ +inline fun handleResult( + onSendData: () -> String?, + onAnswerWebAppQuery: (WebAppQueryId) -> Unit +) { + webApp.initDataUnsafe.queryId ?.let { + onAnswerWebAppQuery(it) + } ?: webApp.sendData(onSendData() ?: return) +}