From 30f35e54885aae19f4b1f7240bf8c6a25dffbbdc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Feb 2021 11:07:30 +0600 Subject: [PATCH 1/2] start 0.32.3 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acd2a8d895..0cc342e3dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.32.3 + ## 0.32.2 * `Core`: diff --git a/gradle.properties b/gradle.properties index f5a20a376a..a8a2fe0fb5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,6 +17,6 @@ micro_utils_version=0.4.23 javax_activation_version=1.1.1 library_group=dev.inmo -library_version=0.32.2 +library_version=0.32.3 github_release_plugin_version=2.2.12 From e8a7ea9ce4ea6f6c37e78750c17695c76681161b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Feb 2021 11:17:51 +0600 Subject: [PATCH 2/2] inline qieries expectators and waiters --- CHANGELOG.md | 3 + .../expectations/WaitCallbackQuery.kt | 20 +++--- .../expectations/WaitInlineQuery.kt | 68 +++++++++++++++++++ .../triggers_handling/InlineQueryTriggers.kt | 57 ++++++++++++++++ 4 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cc342e3dd..838d6cac4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.32.3 +* `Behaviour Builder`: + * Add expectators and waiters for inline queries + ## 0.32.2 * `Core`: diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt index e9e70a84c6..dc0e8645c9 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt @@ -24,7 +24,7 @@ private suspend fun BehaviourContext.waitCallbackQueries( }.toList().toList() -private suspend inline fun BehaviourContext.waitEvents( +private suspend inline fun BehaviourContext.waitCallbacks( count: Int = 1, initRequest: Request<*>? = null, noinline errorFactory: NullableRequestBuilder<*> = { null }, @@ -51,52 +51,52 @@ suspend fun BehaviourContext.waitDataCallbackQuery( errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitGameShortNameCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitInlineMessageIdCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitInlineMessageIdDataCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitInlineMessageIdGameShortNameCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitMessageCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitMessageDataCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitMessageGameShortNameCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitUnknownCallbackQuery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, filter: CallbackQueryMapper? = null -) = waitEvents(count, initRequest, errorFactory, filter) +) = waitCallbacks(count, initRequest, errorFactory, filter) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt new file mode 100644 index 0000000000..009af3d38a --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt @@ -0,0 +1,68 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate +import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.types.CallbackQuery.* +import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery +import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery +import dev.inmo.tgbotapi.types.InlineQueries.query.LocationInlineQuery +import kotlinx.coroutines.flow.toList + +typealias InlineQueryMapper = T.() -> T? + +private suspend fun BehaviourContext.waitInlineQueries( + count: Int = 1, + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + mapper: suspend InlineQuery.() -> O? +): List = expectFlow( + initRequest, + count, + errorFactory +) { + it.asInlineQueryUpdate() ?.data ?.mapper().let(::listOfNotNull) +}.toList().toList() + + +private suspend inline fun BehaviourContext.waitInlines( + count: Int = 1, + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null }, + noinline filter: InlineQueryMapper? = null +) : List = waitInlineQueries( + count, + initRequest, + errorFactory +) { + if (this is T) { + if (filter == null) { + this + } else { + filter(this) + } + } else { + null + } +} + +suspend fun BehaviourContext.waitAnyInlineQuery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + count: Int = 1, + filter: InlineQueryMapper? = null +) = waitInlines(count, initRequest, errorFactory, filter) + +suspend fun BehaviourContext.waitBaseInlineQuery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + count: Int = 1, + filter: InlineQueryMapper? = null +) = waitInlines(count, initRequest, errorFactory, filter) +suspend fun BehaviourContext.waitLocationInlineQuery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + count: Int = 1, + filter: InlineQueryMapper? = null +) = waitInlines(count, initRequest, errorFactory, filter) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt new file mode 100644 index 0000000000..e85f2cee37 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt @@ -0,0 +1,57 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling + +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate +import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat +import dev.inmo.tgbotapi.types.CallbackQuery.DataCallbackQuery +import dev.inmo.tgbotapi.types.CallbackQuery.GameShortNameCallbackQuery +import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery +import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery +import dev.inmo.tgbotapi.types.InlineQueries.query.LocationInlineQuery + +internal suspend inline fun BehaviourContext.onInlineQuery( + includeFilterByChatInBehaviourSubContext: Boolean = true, + noinline additionalFilter: (suspend (T) -> Boolean)? = null, + noinline scenarioReceiver: BehaviourContextAndTypeReceiver +) = flowsUpdatesFilter.expectFlow(bot) { + it.asInlineQueryUpdate() ?.data ?.let { query -> + if (query is T) { + if (additionalFilter == null || additionalFilter(query)) query else null + } else { + null + } + }.let(::listOfNotNull) +}.subscribeSafelyWithoutExceptions(scope) { triggerQuery -> + doInSubContextWithUpdatesFilter( + updatesFilter = if (includeFilterByChatInBehaviourSubContext) { + { it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId } + } else { + null + } + ) { + scenarioReceiver(triggerQuery) + } +} + + +suspend fun BehaviourContext.onAnyInlineQuery( + includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: (suspend (InlineQuery) -> Boolean)? = null, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) + + +suspend fun BehaviourContext.onBaseInlineQuery( + includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: (suspend (BaseInlineQuery) -> Boolean)? = null, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) + + +suspend fun BehaviourContext.onLocationInlineQuery( + includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: (suspend (LocationInlineQuery) -> Boolean)? = null, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)