1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

filters in waiters

This commit is contained in:
InsanusMokrassar 2021-08-06 10:32:31 +06:00
parent 9540797ffc
commit ed077ae1bc
7 changed files with 272 additions and 145 deletions

View File

@ -18,6 +18,7 @@
* New `TelegramBot#buildBehaviour` extension with `FlowUpdatesFilter` and `CoroutineScope` with * New `TelegramBot#buildBehaviour` extension with `FlowUpdatesFilter` and `CoroutineScope` with
default `CoroutineScope` default `CoroutineScope`
* New typealias `SimpleFilter` for unifying triggers filter signatures * New typealias `SimpleFilter` for unifying triggers filter signatures
* All waiters got real filters (`SimpleFilter`) and rename old filters as mappers
## 0.35.2 ## 0.35.2

View File

@ -3,6 +3,7 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.CallbackQuery.* import dev.inmo.tgbotapi.types.CallbackQuery.*
@ -14,13 +15,19 @@ private suspend fun <O> BehaviourContext.waitCallbackQueries(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CallbackQuery>? = null,
mapper: suspend CallbackQuery.() -> O? mapper: suspend CallbackQuery.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
it.asCallbackQueryUpdate() ?.data ?.mapper().let(::listOfNotNull) val data = it.asCallbackQueryUpdate() ?.data
if (data != null && (filter == null || filter(data))) {
data.mapper().let(::listOfNotNull)
} else {
emptyList()
}
}.toList().toList() }.toList().toList()
@ -28,11 +35,17 @@ private suspend inline fun <reified T : CallbackQuery> BehaviourContext.waitCall
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline filter: CallbackQueryMapper<T>? = null noinline filter: CallbackQueryMapper<T>? = null
) : List<T> = waitCallbackQueries<T>( ) : List<T> = waitCallbackQueries<T>(
count, count,
initRequest, initRequest,
errorFactory errorFactory,
filter ?.let {
{
(it as? T) ?.let(::filter)
}
}
) { ) {
if (this is T) { if (this is T) {
if (filter == null) { if (filter == null) {
@ -50,53 +63,62 @@ suspend fun BehaviourContext.waitDataCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<DataCallbackQuery>? = null filter: SimpleFilter<DataCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<DataCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitGameShortNameCallbackQuery( suspend fun BehaviourContext.waitGameShortNameCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<GameShortNameCallbackQuery>? = null filter: SimpleFilter<GameShortNameCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<GameShortNameCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitInlineMessageIdCallbackQuery( suspend fun BehaviourContext.waitInlineMessageIdCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<InlineMessageIdCallbackQuery>? = null filter: SimpleFilter<InlineMessageIdCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<InlineMessageIdCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitInlineMessageIdDataCallbackQuery( suspend fun BehaviourContext.waitInlineMessageIdDataCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<InlineMessageIdDataCallbackQuery>? = null filter: SimpleFilter<InlineMessageIdDataCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<InlineMessageIdDataCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitInlineMessageIdGameShortNameCallbackQuery( suspend fun BehaviourContext.waitInlineMessageIdGameShortNameCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<InlineMessageIdGameShortNameCallbackQuery>? = null filter: SimpleFilter<InlineMessageIdGameShortNameCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<InlineMessageIdGameShortNameCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMessageCallbackQuery( suspend fun BehaviourContext.waitMessageCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<MessageCallbackQuery>? = null filter: SimpleFilter<MessageCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<MessageCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMessageDataCallbackQuery( suspend fun BehaviourContext.waitMessageDataCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<MessageDataCallbackQuery>? = null filter: SimpleFilter<MessageDataCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<MessageDataCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMessageGameShortNameCallbackQuery( suspend fun BehaviourContext.waitMessageGameShortNameCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<MessageGameShortNameCallbackQuery>? = null filter: SimpleFilter<MessageGameShortNameCallbackQuery>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<MessageGameShortNameCallbackQuery>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitUnknownCallbackQuery( suspend fun BehaviourContext.waitUnknownCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CallbackQueryMapper<UnknownCallbackQueryType>? = null filter: SimpleFilter<UnknownCallbackQueryType>? = null,
) = waitCallbacks(count, initRequest, errorFactory, filter) mapper: CallbackQueryMapper<UnknownCallbackQueryType>? = null
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.ChatMemberUpdated import dev.inmo.tgbotapi.types.ChatMemberUpdated
import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate
@ -14,29 +15,37 @@ private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContex
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline mapper: ChatMemberUpdatedMapper<ChatMemberUpdated> noinline mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>
): List<ChatMemberUpdated> = expectFlow( ): List<ChatMemberUpdated> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
(it as? T) ?.data.let(::listOfNotNull) val data = (it as? T) ?.data ?: return@expectFlow emptyList()
if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull)
} else {
emptyList()
}
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContext.waitChatMemberUpdatedWithFilter( private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContext.waitChatMemberUpdatedWithFilter(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null filter: SimpleFilter<T>? = null,
noinline mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null
) : List<ChatMemberUpdated> = waitChatMemberUpdated<T>( ) : List<ChatMemberUpdated> = waitChatMemberUpdated<T>(
count, count,
initRequest, initRequest,
errorFactory errorFactory,
filter,
) { ) {
if (filter == null) { if (mapper == null) {
this this
} else { } else {
filter(this) mapper(this)
} }
} }
@ -44,19 +53,22 @@ suspend fun BehaviourContext.waitChatMemberUpdated(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null filter: SimpleFilter<ChatMemberUpdatedUpdate>? = null,
) = waitChatMemberUpdatedWithFilter<ChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter) mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null
) = waitChatMemberUpdatedWithFilter<ChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitCommonChatMemberUpdated( suspend fun BehaviourContext.waitCommonChatMemberUpdated(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null filter: SimpleFilter<CommonChatMemberUpdatedUpdate>? = null,
) = waitChatMemberUpdatedWithFilter<CommonChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter) mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null
) = waitChatMemberUpdatedWithFilter<CommonChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMyChatMemberUpdated( suspend fun BehaviourContext.waitMyChatMemberUpdated(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null filter: SimpleFilter<MyChatMemberUpdatedUpdate>? = null,
) = waitChatMemberUpdatedWithFilter<MyChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter) mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null
) = waitChatMemberUpdatedWithFilter<MyChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter, mapper)

View File

@ -4,6 +4,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
@ -11,6 +12,8 @@ import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.types.message.content.abstracts.* import dev.inmo.tgbotapi.types.message.content.abstracts.*
import dev.inmo.tgbotapi.types.message.content.media.* import dev.inmo.tgbotapi.types.message.content.media.*
import dev.inmo.tgbotapi.types.message.payments.InvoiceContent import dev.inmo.tgbotapi.types.message.payments.InvoiceContent
import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.SentMediaGroupUpdate
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
typealias CommonMessageToContentMapper<T> = suspend CommonMessage<T>.() -> T? typealias CommonMessageToContentMapper<T> = suspend CommonMessage<T>.() -> T?
@ -20,18 +23,32 @@ private suspend fun <O> BehaviourContext.waitCommonMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<MessageContent>>? = null,
mapper: suspend CommonMessage<MessageContent>.() -> O? mapper: suspend CommonMessage<MessageContent>.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val messages = when (it) {
is SentMediaGroupUpdate -> {
if (includeMediaGroups) { if (includeMediaGroups) {
it.asSentMediaGroupUpdate() ?.data ?.mapNotNull { it.data.map { it as CommonMessage<MessageContent> }
(it as CommonMessage<MessageContent>).mapper() } else {
} ?.let { return@expectFlow it } emptyList()
}
}
is BaseSentMessageUpdate -> listOf(it.data)
else -> return@expectFlow emptyList()
}
messages.mapNotNull { message ->
val asCommonMessage = message as CommonMessage<MessageContent>
if (filter == null || filter(asCommonMessage)) {
asCommonMessage.mapper()
} else {
null
}
} }
it.asBaseSentMessageUpdate() ?.data ?.asCommonMessage() ?.mapper().let(::listOfNotNull)
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : MessageContent> BehaviourContext.waitContent( private suspend inline fun <reified T : MessageContent> BehaviourContext.waitContent(
@ -39,20 +56,22 @@ private suspend inline fun <reified T : MessageContent> BehaviourContext.waitCon
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: CommonMessageToContentMapper<T>? = null filter: SimpleFilter<CommonMessage<T>>? = null,
noinline mapper: CommonMessageToContentMapper<T>? = null
) : List<T> = waitCommonMessage<T>( ) : List<T> = waitCommonMessage<T>(
count, count,
initRequest, initRequest,
includeMediaGroups, includeMediaGroups,
errorFactory errorFactory,
filter
) { ) {
if (content is T) { if (content is T) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val message = (this as CommonMessage<T>) val message = (this as CommonMessage<T>)
if (filter == null) { if (mapper == null) {
message.content message.content
} else { } else {
safelyWithoutExceptions { filter(message) } safelyWithoutExceptions { mapper(message) }
} }
} else { } else {
null null
@ -64,140 +83,162 @@ suspend fun BehaviourContext.waitContentMessage(
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
filter: CommonMessageToContentMapper<MessageContent>? = null filter: SimpleFilter<CommonMessage<MessageContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<MessageContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitContact( suspend fun BehaviourContext.waitContact(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<ContactContent>? = null filter: SimpleFilter<CommonMessage<ContactContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<ContactContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDice( suspend fun BehaviourContext.waitDice(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<DiceContent>? = null filter: SimpleFilter<CommonMessage<DiceContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<DiceContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitGame( suspend fun BehaviourContext.waitGame(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<GameContent>? = null filter: SimpleFilter<CommonMessage<GameContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<GameContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLocation( suspend fun BehaviourContext.waitLocation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<LocationContent>? = null filter: SimpleFilter<CommonMessage<LocationContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<LocationContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPoll( suspend fun BehaviourContext.waitPoll(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<PollContent>? = null filter: SimpleFilter<CommonMessage<PollContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<PollContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitText( suspend fun BehaviourContext.waitText(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<TextContent>? = null filter: SimpleFilter<CommonMessage<TextContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<TextContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVenue( suspend fun BehaviourContext.waitVenue(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<VenueContent>? = null filter: SimpleFilter<CommonMessage<VenueContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<VenueContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAudioMediaGroupContent( suspend fun BehaviourContext.waitAudioMediaGroupContent(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
filter: CommonMessageToContentMapper<AudioMediaGroupContent>? = null filter: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<AudioMediaGroupContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDocumentMediaGroupContent( suspend fun BehaviourContext.waitDocumentMediaGroupContent(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
filter: CommonMessageToContentMapper<DocumentMediaGroupContent>? = null filter: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<DocumentMediaGroupContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMedia( suspend fun BehaviourContext.waitMedia(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
filter: CommonMessageToContentMapper<MediaContent>? = null filter: SimpleFilter<CommonMessage<MediaContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<MediaContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAnyMediaGroupContent( suspend fun BehaviourContext.waitAnyMediaGroupContent(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
filter: CommonMessageToContentMapper<MediaGroupContent>? = null filter: SimpleFilter<CommonMessage<MediaGroupContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<MediaGroupContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVisualMediaGroupContent( suspend fun BehaviourContext.waitVisualMediaGroupContent(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
filter: CommonMessageToContentMapper<VisualMediaGroupContent>? = null filter: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<VisualMediaGroupContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAnimation( suspend fun BehaviourContext.waitAnimation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<AnimationContent>? = null filter: SimpleFilter<CommonMessage<AnimationContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<AnimationContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAudio( suspend fun BehaviourContext.waitAudio(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
filter: CommonMessageToContentMapper<AudioContent>? = null filter: SimpleFilter<CommonMessage<AudioContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<AudioContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDocument( suspend fun BehaviourContext.waitDocument(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
filter: CommonMessageToContentMapper<DocumentContent>? = null filter: SimpleFilter<CommonMessage<DocumentContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<DocumentContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPhoto( suspend fun BehaviourContext.waitPhoto(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
filter: CommonMessageToContentMapper<PhotoContent>? = null filter: SimpleFilter<CommonMessage<PhotoContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<PhotoContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitSticker( suspend fun BehaviourContext.waitSticker(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<StickerContent>? = null filter: SimpleFilter<CommonMessage<StickerContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<StickerContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideo( suspend fun BehaviourContext.waitVideo(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
filter: CommonMessageToContentMapper<VideoContent>? = null filter: SimpleFilter<CommonMessage<VideoContent>>? = null,
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter) mapper: CommonMessageToContentMapper<VideoContent>? = null
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoNote( suspend fun BehaviourContext.waitVideoNote(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<VideoNoteContent>? = null filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<VideoNoteContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoice( suspend fun BehaviourContext.waitVoice(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<VoiceContent>? = null filter: SimpleFilter<CommonMessage<VoiceContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<VoiceContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitInvoice( suspend fun BehaviourContext.waitInvoice(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: CommonMessageToContentMapper<InvoiceContent>? = null filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null,
) = waitContent(count, initRequest, false, errorFactory, filter) mapper: CommonMessageToContentMapper<InvoiceContent>? = null
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)

View File

@ -3,6 +3,7 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate
import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
@ -18,13 +19,19 @@ private suspend fun <O> BehaviourContext.waitEventMessages(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<ChatEvent>>? = null,
mapper: suspend ChatEventMessage<ChatEvent>.() -> O? mapper: suspend ChatEventMessage<ChatEvent>.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.mapper().let(::listOfNotNull) val data = it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage()
if (data != null && (filter == null || filter(data))) {
data.mapper().let(::listOfNotNull)
} else {
emptyList()
}
}.toList().toList() }.toList().toList()
@ -32,19 +39,21 @@ private suspend inline fun <reified T : ChatEvent> BehaviourContext.waitEvents(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: EventMessageToEventMapper<T>? = null filter: SimpleFilter<ChatEventMessage<T>>? = null,
noinline mapper: EventMessageToEventMapper<T>? = null
) : List<T> = waitEventMessages<T>( ) : List<T> = waitEventMessages<T>(
initRequest, initRequest,
errorFactory, errorFactory,
count count,
filter
) { ) {
if (chatEvent is T) { if (chatEvent is T) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val message = (this as ChatEventMessage<T>) val message = (this as ChatEventMessage<T>)
if (filter == null) { if (mapper == null) {
message.chatEvent message.chatEvent
} else { } else {
filter(message) mapper(message)
} }
} else { } else {
null null
@ -55,124 +64,144 @@ suspend fun BehaviourContext.waitChannelEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<ChannelEvent>? = null filter: SimpleFilter<ChatEventMessage<ChannelEvent>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<ChannelEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitChatEvents( suspend fun BehaviourContext.waitChatEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<ChatEvent>? = null filter: SimpleFilter<ChatEventMessage<ChatEvent>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<ChatEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoiceChatEvents( suspend fun BehaviourContext.waitVoiceChatEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<VoiceChatEvent>? = null filter: SimpleFilter<ChatEventMessage<VoiceChatEvent>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<VoiceChatEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoiceChatStartedEvents( suspend fun BehaviourContext.waitVoiceChatStartedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<VoiceChatStarted>? = null filter: SimpleFilter<ChatEventMessage<VoiceChatStarted>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<VoiceChatStarted>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoiceChatEndedEvents( suspend fun BehaviourContext.waitVoiceChatEndedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<VoiceChatEnded>? = null filter: SimpleFilter<ChatEventMessage<VoiceChatEnded>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<VoiceChatEnded>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoiceChatParticipantsInvitedEvents( suspend fun BehaviourContext.waitVoiceChatParticipantsInvitedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<VoiceChatParticipantsInvited>? = null filter: SimpleFilter<ChatEventMessage<VoiceChatParticipantsInvited>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<VoiceChatParticipantsInvited>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMessageAutoDeleteTimerChangedEvents( suspend fun BehaviourContext.waitMessageAutoDeleteTimerChangedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<MessageAutoDeleteTimerChanged>? = null filter: SimpleFilter<ChatEventMessage<MessageAutoDeleteTimerChanged>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<MessageAutoDeleteTimerChanged>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitCommonEvents( suspend fun BehaviourContext.waitCommonEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<CommonEvent>? = null filter: SimpleFilter<ChatEventMessage<CommonEvent>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<CommonEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitGroupEvents( suspend fun BehaviourContext.waitGroupEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<GroupEvent>? = null filter: SimpleFilter<ChatEventMessage<GroupEvent>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<GroupEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitSupergroupEvents( suspend fun BehaviourContext.waitSupergroupEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<SupergroupEvent>? = null filter: SimpleFilter<ChatEventMessage<SupergroupEvent>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<SupergroupEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitChannelChatCreatedEvents( suspend fun BehaviourContext.waitChannelChatCreatedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<ChannelChatCreated>? = null filter: SimpleFilter<ChatEventMessage<ChannelChatCreated>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<ChannelChatCreated>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDeleteChatPhotoEvents( suspend fun BehaviourContext.waitDeleteChatPhotoEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<DeleteChatPhoto>? = null filter: SimpleFilter<ChatEventMessage<DeleteChatPhoto>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<DeleteChatPhoto>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitGroupChatCreatedEvents( suspend fun BehaviourContext.waitGroupChatCreatedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<GroupChatCreated>? = null filter: SimpleFilter<ChatEventMessage<GroupChatCreated>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<GroupChatCreated>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLeftChatMemberEvents( suspend fun BehaviourContext.waitLeftChatMemberEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<LeftChatMember>? = null filter: SimpleFilter<ChatEventMessage<LeftChatMember>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<LeftChatMember>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitNewChatPhotoEvents( suspend fun BehaviourContext.waitNewChatPhotoEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<NewChatPhoto>? = null filter: SimpleFilter<ChatEventMessage<NewChatPhoto>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<NewChatPhoto>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitNewChatMembersEvents( suspend fun BehaviourContext.waitNewChatMembersEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<NewChatMembers>? = null filter: SimpleFilter<ChatEventMessage<NewChatMembers>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<NewChatMembers>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitNewChatTitleEvents( suspend fun BehaviourContext.waitNewChatTitleEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<NewChatTitle>? = null filter: SimpleFilter<ChatEventMessage<NewChatTitle>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<NewChatTitle>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPinnedMessageEvents( suspend fun BehaviourContext.waitPinnedMessageEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<PinnedMessage>? = null filter: SimpleFilter<ChatEventMessage<PinnedMessage>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<PinnedMessage>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitProximityAlertTriggeredEvents( suspend fun BehaviourContext.waitProximityAlertTriggeredEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<ProximityAlertTriggered>? = null filter: SimpleFilter<ChatEventMessage<ProximityAlertTriggered>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<ProximityAlertTriggered>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitSupergroupChatCreatedEvents( suspend fun BehaviourContext.waitSupergroupChatCreatedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: EventMessageToEventMapper<SupergroupChatCreated>? = null filter: SimpleFilter<ChatEventMessage<SupergroupChatCreated>>? = null,
) = waitEvents(count, initRequest, errorFactory, filter) mapper: EventMessageToEventMapper<SupergroupChatCreated>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.InlineQueries.query.* import dev.inmo.tgbotapi.types.InlineQueries.query.*
@ -12,13 +13,19 @@ private suspend fun <O> BehaviourContext.waitInlineQueries(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<InlineQuery>? = null,
mapper: suspend InlineQuery.() -> O? mapper: suspend InlineQuery.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
it.asInlineQueryUpdate() ?.data ?.mapper().let(::listOfNotNull) val data = it.asInlineQueryUpdate() ?.data
if (data != null && (filter == null || filter(data))) {
data.mapper().let(::listOfNotNull)
} else {
emptyList()
}
}.toList().toList() }.toList().toList()
@ -26,11 +33,13 @@ private suspend inline fun <reified T : InlineQuery> BehaviourContext.waitInline
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: InlineQueryMapper<T>? = null filter: SimpleFilter<InlineQuery>? = null,
noinline mapper: InlineQueryMapper<T>? = null
) : List<T> = waitInlineQueries<T>( ) : List<T> = waitInlineQueries<T>(
count, count,
initRequest, initRequest,
errorFactory errorFactory,
filter
) { ) {
if (this is T) { if (this is T) {
if (filter == null) { if (filter == null) {
@ -47,18 +56,21 @@ suspend fun BehaviourContext.waitAnyInlineQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: InlineQueryMapper<InlineQuery>? = null filter: SimpleFilter<InlineQuery>? = null,
) = waitInlines(count, initRequest, errorFactory, filter) mapper: InlineQueryMapper<InlineQuery>? = null
) = waitInlines(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitBaseInlineQuery( suspend fun BehaviourContext.waitBaseInlineQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: InlineQueryMapper<BaseInlineQuery>? = null filter: SimpleFilter<BaseInlineQuery>? = null,
) = waitInlines(count, initRequest, errorFactory, filter) mapper: InlineQueryMapper<BaseInlineQuery>? = null
) = waitInlines(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLocationInlineQuery( suspend fun BehaviourContext.waitLocationInlineQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: InlineQueryMapper<LocationInlineQuery>? = null filter: SimpleFilter<LocationInlineQuery>? = null,
) = waitInlines(count, initRequest, errorFactory, filter) mapper: InlineQueryMapper<LocationInlineQuery>? = null
) = waitInlines(count, initRequest, errorFactory, filter, mapper)

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate
import dev.inmo.tgbotapi.extensions.utils.asPassportMessage import dev.inmo.tgbotapi.extensions.utils.asPassportMessage
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
@ -17,30 +18,38 @@ suspend fun <O> BehaviourContext.waitPassportMessages(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<PassportMessage>? = null,
mapper: suspend PassportMessage.() -> O? mapper: suspend PassportMessage.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
it.asMessageUpdate() ?.data ?.asPassportMessage() ?.mapper().let(::listOfNotNull) val data = it.asMessageUpdate() ?.data ?.asPassportMessage() ?: return@expectFlow emptyList()
if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull)
} else {
emptyList()
}
}.toList().toList() }.toList().toList()
suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.waitPassportMessagesWith( suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.waitPassportMessagesWith(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: PassportMessageMapper? = null filter: SimpleFilter<PassportMessage>? = null,
noinline mapper: PassportMessageMapper? = null
) : List<PassportData> = waitPassportMessages( ) : List<PassportData> = waitPassportMessages(
initRequest, initRequest,
errorFactory, errorFactory,
count count,
filter
) { ) {
if (passportData.data.any { it is T }) { if (passportData.data.any { it is T }) {
if (filter == null) { if (mapper == null) {
passportData passportData
} else { } else {
filter(this) mapper(this)
} }
} else { } else {
null null
@ -51,5 +60,6 @@ suspend fun BehaviourContext.waitAnyPassportMessages(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: PassportMessageMapper? = null filter: SimpleFilter<PassportMessage>? = null,
) = waitPassportMessagesWith<EncryptedPassportElement>(count, initRequest, errorFactory, filter) mapper: PassportMessageMapper? = null
) = waitPassportMessagesWith<EncryptedPassportElement>(count, initRequest, errorFactory, filter, mapper)