diff --git a/CHANGELOG.md b/CHANGELOG.md index ececa0b868..7390ed2954 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # TelegramBotAPI changelog +## 0.38.20 + +* `BehaviourBuilder FSM`: + * Hotfixes +* `WebApps`: + * New extension `TelegramBot#answerWebAppQuery` + * New function `handleResult` + ## 0.38.19 * `BehaviourBuilder`: diff --git a/gradle.properties b/gradle.properties index 0401f4f412..f9b76a73b9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,6 +20,6 @@ javax_activation_version=1.1.1 dokka_version=1.6.10 library_group=dev.inmo -library_version=0.38.19 +library_version=0.38.20 github_release_plugin_version=2.3.7 diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt index 6212a4134a..2bb6b3ea89 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt @@ -28,7 +28,9 @@ interface BehaviourContextWithFSM : BehaviourContext, StatesMachine> ): T? { return handlers.firstOrNull { it.checkHandleable(state) } ?.run { - handleState(contextUpdatesFlow, state) + doInSubContext(updatesUpstreamFlow = contextUpdatesFlow) { + handleState(state) + } } } diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourWithFSMStateHandlerHolder.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourWithFSMStateHandlerHolder.kt index 788c1cf3c1..fa3bdbd0e2 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourWithFSMStateHandlerHolder.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourWithFSMStateHandlerHolder.kt @@ -34,20 +34,11 @@ class BehaviourWithFSMStateHandlerHolder( /** * Handling of state :) - * - * @param contextUpdatesFlow This [Flow] will be used as source of updates. By contract, this [Flow] must be common - * for all [State]s of incoming [state] [State.context] and for the whole chain inside of [BehaviourContextWithFSM] */ suspend fun BehaviourContextWithFSM.handleState( - contextUpdatesFlow: Flow, state: O - ): O? { - val subscope = scope.LinkedSupervisorScope() - return with(copy(scope = subscope, upstreamUpdatesFlow = contextUpdatesFlow)) { - with(delegateTo) { - handleState(state as I) - } - } + ): O? = with(delegateTo) { + handleState(state as I) } } 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/AnswerWebAppQuery.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/AnswerWebAppQuery.kt new file mode 100644 index 0000000000..c77aa0b763 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/AnswerWebAppQuery.kt @@ -0,0 +1,11 @@ +package dev.inmo.tgbotapi.webapps + +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.answers.AnswerWebAppQuery +import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQueryResult + +suspend fun TelegramBot.answerWebAppQuery( + result: InlineQueryResult +) = webApp.initDataUnsafe.queryId ?.let { + execute(AnswerWebAppQuery(it, result)) +} 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) +}