1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt

65 lines
2.1 KiB
Kotlin
Raw Normal View History

2021-02-02 05:17:51 +00:00
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate
import dev.inmo.tgbotapi.requests.abstracts.Request
2021-05-29 09:51:45 +00:00
import dev.inmo.tgbotapi.types.InlineQueries.query.*
2021-02-02 05:17:51 +00:00
import kotlinx.coroutines.flow.toList
2021-04-03 08:51:21 +00:00
typealias InlineQueryMapper<T> = suspend T.() -> T?
2021-02-02 05:17:51 +00:00
private suspend fun <O> BehaviourContext.waitInlineQueries(
count: Int = 1,
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
2021-06-03 17:34:11 +00:00
mapper: suspend InlineQuery.() -> O?
2021-02-02 05:17:51 +00:00
): List<O> = expectFlow(
initRequest,
count,
errorFactory
) {
it.asInlineQueryUpdate() ?.data ?.mapper().let(::listOfNotNull)
}.toList().toList()
2021-06-03 17:34:11 +00:00
private suspend inline fun <reified T : InlineQuery> BehaviourContext.waitInlines(
2021-02-02 05:17:51 +00:00
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,
2021-05-29 09:51:45 +00:00
filter: InlineQueryMapper<InlineQuery>? = null
2021-02-02 05:17:51 +00:00
) = 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)