mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-25 19:48:43 +00:00
inline qieries expectators and waiters
This commit is contained in:
parent
30f35e5488
commit
e8a7ea9ce4
@ -2,6 +2,9 @@
|
||||
|
||||
## 0.32.3
|
||||
|
||||
* `Behaviour Builder`:
|
||||
* Add expectators and waiters for inline queries
|
||||
|
||||
## 0.32.2
|
||||
|
||||
* `Core`:
|
||||
|
@ -24,7 +24,7 @@ private suspend fun <O> BehaviourContext.waitCallbackQueries(
|
||||
}.toList().toList()
|
||||
|
||||
|
||||
private suspend inline fun <reified T : CallbackQuery> BehaviourContext.waitEvents(
|
||||
private suspend inline fun <reified T : CallbackQuery> 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<DataCallbackQuery>? = 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<GameShortNameCallbackQuery>? = 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<InlineMessageIdCallbackQuery>? = 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<InlineMessageIdDataCallbackQuery>? = 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<InlineMessageIdGameShortNameCallbackQuery>? = 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<MessageCallbackQuery>? = 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<MessageDataCallbackQuery>? = 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<MessageGameShortNameCallbackQuery>? = 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<UnknownCallbackQueryType>? = null
|
||||
) = waitEvents(count, initRequest, errorFactory, filter)
|
||||
) = waitCallbacks(count, initRequest, errorFactory, filter)
|
||||
|
@ -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.() -> T?
|
||||
|
||||
private suspend fun <O> BehaviourContext.waitInlineQueries(
|
||||
count: Int = 1,
|
||||
initRequest: Request<*>? = null,
|
||||
errorFactory: NullableRequestBuilder<*> = { null },
|
||||
mapper: suspend InlineQuery.() -> O?
|
||||
): List<O> = expectFlow(
|
||||
initRequest,
|
||||
count,
|
||||
errorFactory
|
||||
) {
|
||||
it.asInlineQueryUpdate() ?.data ?.mapper().let(::listOfNotNull)
|
||||
}.toList().toList()
|
||||
|
||||
|
||||
private suspend inline fun <reified T : InlineQuery> BehaviourContext.waitInlines(
|
||||
count: Int = 1,
|
||||
initRequest: Request<*>? = null,
|
||||
noinline errorFactory: NullableRequestBuilder<*> = { null },
|
||||
noinline filter: InlineQueryMapper<T>? = null
|
||||
) : List<T> = waitInlineQueries<T>(
|
||||
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<InlineQuery>? = null
|
||||
) = waitInlines(count, initRequest, errorFactory, filter)
|
||||
|
||||
suspend fun BehaviourContext.waitBaseInlineQuery(
|
||||
initRequest: Request<*>? = null,
|
||||
errorFactory: NullableRequestBuilder<*> = { null },
|
||||
count: Int = 1,
|
||||
filter: InlineQueryMapper<BaseInlineQuery>? = null
|
||||
) = waitInlines(count, initRequest, errorFactory, filter)
|
||||
suspend fun BehaviourContext.waitLocationInlineQuery(
|
||||
initRequest: Request<*>? = null,
|
||||
errorFactory: NullableRequestBuilder<*> = { null },
|
||||
count: Int = 1,
|
||||
filter: InlineQueryMapper<LocationInlineQuery>? = null
|
||||
) = waitInlines(count, initRequest, errorFactory, filter)
|
@ -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 <reified T : InlineQuery> BehaviourContext.onInlineQuery(
|
||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||
noinline additionalFilter: (suspend (T) -> Boolean)? = null,
|
||||
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T>
|
||||
) = 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<Unit, InlineQuery>
|
||||
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
|
||||
|
||||
|
||||
suspend fun BehaviourContext.onBaseInlineQuery(
|
||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||
additionalFilter: (suspend (BaseInlineQuery) -> Boolean)? = null,
|
||||
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, BaseInlineQuery>
|
||||
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
|
||||
|
||||
|
||||
suspend fun BehaviourContext.onLocationInlineQuery(
|
||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||
additionalFilter: (suspend (LocationInlineQuery) -> Boolean)? = null,
|
||||
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, LocationInlineQuery>
|
||||
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
|
Loading…
Reference in New Issue
Block a user