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

cleanup waiters in behaviour builder and add withEvent

This commit is contained in:
InsanusMokrassar 2022-05-21 23:38:42 +06:00
parent 126a07cdc7
commit 507cb2af26
18 changed files with 385 additions and 797 deletions

View File

@ -3,7 +3,10 @@
## 1.2.0 ## 1.2.0
* `Behaviour Builder`: * `Behaviour Builder`:
* Improvements in updates passing inside of subcontexts * Mappers have been removed from waiters extensions
* Triggers extensions now will use filtering inside of context receiver instead of passing the filters into `BehaviourContext`. That means that in the subcontext will not be used preinstalled filters for their `BehaviourContext` and filter of trigger will not be used in subcontext
* `Utils`:
* Add opportunity to get event messages with specific `ChatEvent` type using `withEvent`/`requireWithEvent` (by analog with `withEvent` and `requireWithEvent`)
## 1.1.3 ## 1.1.3

View File

@ -11,114 +11,76 @@ import kotlinx.coroutines.flow.toList
typealias CallbackQueryMapper<T> = suspend T.() -> T? typealias CallbackQueryMapper<T> = suspend T.() -> T?
private suspend fun <O> BehaviourContext.waitCallbackQueries( private suspend inline fun <reified O> BehaviourContext.waitCallbackQueries(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CallbackQuery>? = null, filter: SimpleFilter<O>? = null
mapper: suspend CallbackQuery.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asCallbackQueryUpdate() ?.data ?: return@expectFlow emptyList() val data = it.asCallbackQueryUpdate() ?.data ?: return@expectFlow emptyList()
if (filter == null || filter(data)) { if (data is O && (filter == null || filter(data))) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : CallbackQuery> BehaviourContext.waitCallbacks(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline mapper: CallbackQueryMapper<T>? = null
) : List<T> = waitCallbackQueries<T>(
count,
initRequest,
errorFactory,
filter ?.let {
{
(it as? T) ?.let { filter(it) } == true
}
}
) {
if (this is T) {
if (mapper == null) {
this
} else {
mapper(this)
}
} else {
null
}
}
suspend fun BehaviourContext.waitDataCallbackQuery( suspend fun BehaviourContext.waitDataCallbackQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<DataCallbackQuery>? = null, filter: SimpleFilter<DataCallbackQuery>? = null
mapper: CallbackQueryMapper<DataCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<GameShortNameCallbackQuery>? = null, filter: SimpleFilter<GameShortNameCallbackQuery>? = null
mapper: CallbackQueryMapper<GameShortNameCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<InlineMessageIdCallbackQuery>? = null, filter: SimpleFilter<InlineMessageIdCallbackQuery>? = null
mapper: CallbackQueryMapper<InlineMessageIdCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<InlineMessageIdDataCallbackQuery>? = null, filter: SimpleFilter<InlineMessageIdDataCallbackQuery>? = null
mapper: CallbackQueryMapper<InlineMessageIdDataCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<InlineMessageIdGameShortNameCallbackQuery>? = null, filter: SimpleFilter<InlineMessageIdGameShortNameCallbackQuery>? = null
mapper: CallbackQueryMapper<InlineMessageIdGameShortNameCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<MessageCallbackQuery>? = null, filter: SimpleFilter<MessageCallbackQuery>? = null
mapper: CallbackQueryMapper<MessageCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<MessageDataCallbackQuery>? = null, filter: SimpleFilter<MessageDataCallbackQuery>? = null
mapper: CallbackQueryMapper<MessageDataCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<MessageGameShortNameCallbackQuery>? = null, filter: SimpleFilter<MessageGameShortNameCallbackQuery>? = null
mapper: CallbackQueryMapper<MessageGameShortNameCallbackQuery>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<UnknownCallbackQueryType>? = null, filter: SimpleFilter<UnknownCallbackQueryType>? = null
mapper: CallbackQueryMapper<UnknownCallbackQueryType>? = null ) = waitCallbackQueries(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter, mapper)

View File

@ -9,20 +9,19 @@ import kotlinx.coroutines.flow.toList
typealias ChatJoinRequestsMapper = suspend ChatJoinRequest.() -> ChatJoinRequest? typealias ChatJoinRequestsMapper = suspend ChatJoinRequest.() -> ChatJoinRequest?
private suspend fun <O> BehaviourContext.waitChatJoinRequests( private suspend inline fun <reified O> BehaviourContext.internalWaitChatJoinRequests(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<ChatJoinRequest>? = null, filter: SimpleFilter<O>? = null
mapper: suspend ChatJoinRequest.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asChatJoinRequestUpdate() ?.data val data = it.asChatJoinRequestUpdate() ?.data as? O ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
@ -33,17 +32,10 @@ suspend fun BehaviourContext.waitChatJoinRequests(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatJoinRequest>? = null, filter: SimpleFilter<ChatJoinRequest>? = null
mapper: ChatJoinRequestsMapper? = null ) : List<ChatJoinRequest> = internalWaitChatJoinRequests(
) : List<ChatJoinRequest> = waitChatJoinRequests(
count, count,
initRequest, initRequest,
errorFactory, errorFactory,
filter filter
) { )
if (mapper == null) {
this
} else {
mapper(this)
}
}

View File

@ -11,12 +11,11 @@ import kotlinx.coroutines.flow.toList
typealias ChatMemberUpdatedMapper<T> = suspend T.() -> T? typealias ChatMemberUpdatedMapper<T> = suspend T.() -> T?
private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContext.waitChatMemberUpdated( 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 },
filter: SimpleFilter<T>? = null, filter: SimpleFilter<T>? = null
noinline mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>
): List<ChatMemberUpdated> = expectFlow( ): List<ChatMemberUpdated> = expectFlow(
initRequest, initRequest,
count, count,
@ -24,51 +23,29 @@ private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContex
) { ) {
val casted = (it as? T) ?: return@expectFlow emptyList() val casted = (it as? T) ?: return@expectFlow emptyList()
if (filter == null || filter(casted)) { if (filter == null || filter(casted)) {
casted.data.mapper().let(::listOfNotNull) listOf(casted.data)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContext.waitChatMemberUpdatedWithFilter(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null
) : List<ChatMemberUpdated> = waitChatMemberUpdated<T>(
count,
initRequest,
errorFactory,
filter,
) {
if (mapper == null) {
this
} else {
mapper(this)
}
}
suspend fun BehaviourContext.waitChatMemberUpdated( suspend fun BehaviourContext.waitChatMemberUpdated(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatMemberUpdatedUpdate>? = null, filter: SimpleFilter<ChatMemberUpdatedUpdate>? = null
mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null ) = waitChatMemberUpdatedWithFilter(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<CommonChatMemberUpdatedUpdate>? = null, filter: SimpleFilter<CommonChatMemberUpdatedUpdate>? = null
mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null ) = waitChatMemberUpdatedWithFilter(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<MyChatMemberUpdatedUpdate>? = null, filter: SimpleFilter<MyChatMemberUpdatedUpdate>? = null
mapper: ChatMemberUpdatedMapper<ChatMemberUpdated>? = null ) = waitChatMemberUpdatedWithFilter(count, initRequest, errorFactory, filter)
) = waitChatMemberUpdatedWithFilter<MyChatMemberUpdatedUpdate>(count, initRequest, errorFactory, filter, mapper)

View File

@ -9,73 +9,41 @@ import kotlinx.coroutines.flow.toList
typealias ChosenInlineResultMapper<T> = suspend T.() -> T? typealias ChosenInlineResultMapper<T> = suspend T.() -> T?
private suspend fun <O> BehaviourContext.waitChosenInlineResultsUpdates( private suspend inline fun <reified O> BehaviourContext.waitChosenInlineResults(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<ChosenInlineResult>? = null, filter: SimpleFilter<O>? = null
mapper: suspend ChosenInlineResult.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asChosenInlineResultUpdate() ?.data val data = it.asChosenInlineResultUpdate() ?.data as? O ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : ChosenInlineResult> BehaviourContext.waitChosenInlineResults(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline mapper: ChosenInlineResultMapper<T>? = null
) : List<T> = this@waitChosenInlineResults.waitChosenInlineResultsUpdates<T>(
count,
initRequest,
errorFactory,
filter ?.let {
{
(it as? T) ?.let { filter(it) } == true
}
}
) {
if (this is T) {
if (mapper == null) {
this
} else {
mapper(this)
}
} else {
null
}
}
suspend fun BehaviourContext.waitChosenInlineResult( suspend fun BehaviourContext.waitChosenInlineResult(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChosenInlineResult>? = null, filter: SimpleFilter<ChosenInlineResult>? = null
mapper: ChosenInlineResultMapper<ChosenInlineResult>? = null ) = waitChosenInlineResults(count, initRequest, errorFactory, filter)
) = waitChosenInlineResults(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLocationChosenInlineResult( suspend fun BehaviourContext.waitLocationChosenInlineResult(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<LocationChosenInlineResult>? = null, filter: SimpleFilter<LocationChosenInlineResult>? = null
mapper: PollMapper<LocationChosenInlineResult>? = null ) = waitChosenInlineResults(count, initRequest, errorFactory, filter)
) = waitChosenInlineResults(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitBaseChosenInlineResult( suspend fun BehaviourContext.waitBaseChosenInlineResult(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<BaseChosenInlineResult>? = null, filter: SimpleFilter<BaseChosenInlineResult>? = null
mapper: PollMapper<BaseChosenInlineResult>? = null ) = waitChosenInlineResults(count, initRequest, errorFactory, filter)
) = waitChosenInlineResults(count, initRequest, errorFactory, filter, mapper)

View File

@ -2,7 +2,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
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.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
@ -17,17 +16,15 @@ import dev.inmo.tgbotapi.types.message.content.InvoiceContent
import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
typealias CommonMessageToContentMapper<T> = suspend CommonMessage<T>.() -> T? typealias CommonMessageToContentMapper<T> = suspend CommonMessage<T>.() -> T?
private suspend fun <O> BehaviourContext.waitCommonContent( private suspend inline fun <reified O : MessageContent> BehaviourContext.waitContent(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<MessageContent>>? = null, filter: SimpleFilter<CommonMessage<O>>? = null
mapper: suspend CommonMessage<MessageContent>.() -> O?
): Flow<O> = expectFlow( ): Flow<O> = expectFlow(
initRequest, initRequest,
count, count,
@ -36,7 +33,7 @@ private suspend fun <O> BehaviourContext.waitCommonContent(
val messages = when (it) { val messages = when (it) {
is SentMediaGroupUpdate -> { is SentMediaGroupUpdate -> {
if (includeMediaGroups) { if (includeMediaGroups) {
it.data.map { it as CommonMessage<MessageContent> } it.data
} else { } else {
emptyList() emptyList()
} }
@ -45,233 +42,174 @@ private suspend fun <O> BehaviourContext.waitCommonContent(
else -> return@expectFlow emptyList() else -> return@expectFlow emptyList()
} }
messages.mapNotNull { message -> messages.mapNotNull { message ->
val asCommonMessage = message as CommonMessage<MessageContent> val asCommonMessage = (message as CommonMessage<*>).withContent<O>() ?: return@mapNotNull null
if (filter == null || filter(asCommonMessage)) { if (filter == null || filter(asCommonMessage)) {
asCommonMessage.mapper() asCommonMessage.content
} else { } else {
null null
} }
} }
} }
internal inline fun <reified T : MessageContent> contentConverter(
noinline mapper: CommonMessageToContentMapper<T>? = null
): suspend CommonMessage<MessageContent>.() -> T? = mapper ?.let {
{
if (content is T) {
@Suppress("UNCHECKED_CAST")
val message = (this as CommonMessage<T>)
safelyWithoutExceptions { mapper(message) }
} else {
null
}
}
} ?: { content as? T }
private suspend inline fun <reified T : MessageContent> BehaviourContext.waitContent(
count: Int = 1,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<T>>? = null,
noinline mapper: CommonMessageToContentMapper<T>? = null
) : List<T> = waitCommonContent<T>(
count,
initRequest,
includeMediaGroups,
errorFactory,
filter ?.let {
{
it.withContent<T>() ?.let { filter(it) } == true
}
},
contentConverter(mapper)
).toList()
suspend fun BehaviourContext.waitContent( suspend fun BehaviourContext.waitContent(
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: SimpleFilter<CommonMessage<MessageContent>>? = null, filter: SimpleFilter<CommonMessage<MessageContent>>? = null
mapper: CommonMessageToContentMapper<MessageContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<ContactContent>>? = null, filter: SimpleFilter<CommonMessage<ContactContent>>? = null
mapper: CommonMessageToContentMapper<ContactContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<DiceContent>>? = null, filter: SimpleFilter<CommonMessage<DiceContent>>? = null
mapper: CommonMessageToContentMapper<DiceContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<GameContent>>? = null, filter: SimpleFilter<CommonMessage<GameContent>>? = null
mapper: CommonMessageToContentMapper<GameContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<LocationContent>>? = null, filter: SimpleFilter<CommonMessage<LocationContent>>? = null
mapper: CommonMessageToContentMapper<LocationContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLiveLocation( suspend fun BehaviourContext.waitLiveLocation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null, filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null
mapper: CommonMessageToContentMapper<LiveLocationContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitStaticLocation( suspend fun BehaviourContext.waitStaticLocation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null, filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null
mapper: CommonMessageToContentMapper<StaticLocationContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<PollContent>>? = null, filter: SimpleFilter<CommonMessage<PollContent>>? = null
mapper: CommonMessageToContentMapper<PollContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<TextContent>>? = null, filter: SimpleFilter<CommonMessage<TextContent>>? = null
mapper: CommonMessageToContentMapper<TextContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<VenueContent>>? = null, filter: SimpleFilter<CommonMessage<VenueContent>>? = null
mapper: CommonMessageToContentMapper<VenueContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<AudioMediaGroupContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<DocumentMediaGroupContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<MediaContent>>? = null, filter: SimpleFilter<CommonMessage<MediaContent>>? = null
mapper: CommonMessageToContentMapper<MediaContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<MediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<MediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<MediaGroupContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<VisualMediaGroupContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitTextedMediaContent( suspend fun BehaviourContext.waitTextedMediaContent(
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: SimpleFilter<CommonMessage<TextedMediaContent>>? = null, filter: SimpleFilter<CommonMessage<TextedMediaContent>>? = null
mapper: CommonMessageToContentMapper<TextedMediaContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<AnimationContent>>? = null, filter: SimpleFilter<CommonMessage<AnimationContent>>? = null
mapper: CommonMessageToContentMapper<AnimationContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<AudioContent>>? = null, filter: SimpleFilter<CommonMessage<AudioContent>>? = null
mapper: CommonMessageToContentMapper<AudioContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<DocumentContent>>? = null, filter: SimpleFilter<CommonMessage<DocumentContent>>? = null
mapper: CommonMessageToContentMapper<DocumentContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<PhotoContent>>? = null, filter: SimpleFilter<CommonMessage<PhotoContent>>? = null
mapper: CommonMessageToContentMapper<PhotoContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<StickerContent>>? = null, filter: SimpleFilter<CommonMessage<StickerContent>>? = null
mapper: CommonMessageToContentMapper<StickerContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<VideoContent>>? = null, filter: SimpleFilter<CommonMessage<VideoContent>>? = null
mapper: CommonMessageToContentMapper<VideoContent>? = null ) = waitContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<VideoNoteContent>>? = null, filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null
mapper: CommonMessageToContentMapper<VideoNoteContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<VoiceContent>>? = null, filter: SimpleFilter<CommonMessage<VoiceContent>>? = null
mapper: CommonMessageToContentMapper<VoiceContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = 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: SimpleFilter<CommonMessage<InvoiceContent>>? = null, filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null
mapper: CommonMessageToContentMapper<InvoiceContent>? = null ) = waitContent(count, initRequest, false, errorFactory, filter)
) = waitContent(count, initRequest, false, errorFactory, filter, mapper)

View File

@ -17,17 +17,15 @@ import dev.inmo.tgbotapi.types.message.content.InvoiceContent
import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
typealias CommonMessageToCommonMessageMapper<T> = suspend CommonMessage<T>.() -> CommonMessage<T>? typealias CommonMessageToCommonMessageMapper<T> = suspend CommonMessage<T>.() -> CommonMessage<T>?
internal suspend fun <O : MessageContent> BehaviourContext.waitCommonMessage( internal suspend inline fun <reified O : MessageContent> BehaviourContext.waitContentMessage(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<MessageContent>>? = null, filter: SimpleFilter<CommonMessage<O>>? = null
mapper: suspend CommonMessage<MessageContent>.() -> CommonMessage<O>?
): Flow<CommonMessage<O>> = expectFlow( ): Flow<CommonMessage<O>> = expectFlow(
initRequest, initRequest,
count, count,
@ -46,9 +44,9 @@ internal suspend fun <O : MessageContent> BehaviourContext.waitCommonMessage(
} }
messages.mapNotNull { message -> messages.mapNotNull { message ->
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val asCommonMessage = message as? CommonMessage<MessageContent> ?: return@mapNotNull null val asCommonMessage = (message as? CommonMessage<*>) ?.withContent<O>() ?: return@mapNotNull null
if (filter == null || filter(asCommonMessage)) { if (filter == null || filter(asCommonMessage)) {
asCommonMessage.mapper() asCommonMessage
} else { } else {
null null
} }
@ -72,209 +70,164 @@ internal inline fun <reified T : MessageContent> contentMessageConverter(
if (content is T) this as CommonMessage<T> else null if (content is T) this as CommonMessage<T> else null
} }
private suspend inline fun <reified T : MessageContent> BehaviourContext.waitContentMessage(
count: Int = 1,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<T>>? = null,
noinline mapper: CommonMessageToCommonMessageMapper<T>? = null
) : List<CommonMessage<T>> = waitCommonMessage<T>(
count,
initRequest,
includeMediaGroups,
errorFactory,
filter ?.let {
{
it.withContent<T>() ?.let { filter(it) } == true
}
},
contentMessageConverter(mapper)
).toList()
suspend fun BehaviourContext.waitContentMessage( suspend fun BehaviourContext.waitContentMessage(
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: SimpleFilter<CommonMessage<MessageContent>>? = null, filter: SimpleFilter<CommonMessage<MessageContent>>? = null
mapper: CommonMessageToCommonMessageMapper<MessageContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitContactMessage( suspend fun BehaviourContext.waitContactMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<ContactContent>>? = null, filter: SimpleFilter<CommonMessage<ContactContent>>? = null
mapper: CommonMessageToCommonMessageMapper<ContactContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDiceMessage( suspend fun BehaviourContext.waitDiceMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<DiceContent>>? = null, filter: SimpleFilter<CommonMessage<DiceContent>>? = null
mapper: CommonMessageToCommonMessageMapper<DiceContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitGameMessage( suspend fun BehaviourContext.waitGameMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<GameContent>>? = null, filter: SimpleFilter<CommonMessage<GameContent>>? = null
mapper: CommonMessageToCommonMessageMapper<GameContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLocationMessage( suspend fun BehaviourContext.waitLocationMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<LocationContent>>? = null, filter: SimpleFilter<CommonMessage<LocationContent>>? = null
mapper: CommonMessageToCommonMessageMapper<LocationContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLiveLocationMessage( suspend fun BehaviourContext.waitLiveLocationMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null, filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null
mapper: CommonMessageToCommonMessageMapper<LiveLocationContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitStaticLocationMessage( suspend fun BehaviourContext.waitStaticLocationMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null, filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null
mapper: CommonMessageToCommonMessageMapper<StaticLocationContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPollMessage( suspend fun BehaviourContext.waitPollMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<PollContent>>? = null, filter: SimpleFilter<CommonMessage<PollContent>>? = null
mapper: CommonMessageToCommonMessageMapper<PollContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitTextMessage( suspend fun BehaviourContext.waitTextMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<TextContent>>? = null, filter: SimpleFilter<CommonMessage<TextContent>>? = null
mapper: CommonMessageToCommonMessageMapper<TextContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVenueMessage( suspend fun BehaviourContext.waitVenueMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<VenueContent>>? = null, filter: SimpleFilter<CommonMessage<VenueContent>>? = null
mapper: CommonMessageToCommonMessageMapper<VenueContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAudioMediaGroupContentMessage( suspend fun BehaviourContext.waitAudioMediaGroupContentMessage(
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: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null
mapper: CommonMessageToCommonMessageMapper<AudioMediaGroupContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDocumentMediaGroupContentMessage( suspend fun BehaviourContext.waitDocumentMediaGroupContentMessage(
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: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null
mapper: CommonMessageToCommonMessageMapper<DocumentMediaGroupContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMediaMessage( suspend fun BehaviourContext.waitMediaMessage(
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: SimpleFilter<CommonMessage<MediaContent>>? = null, filter: SimpleFilter<CommonMessage<MediaContent>>? = null
mapper: CommonMessageToCommonMessageMapper<MediaContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAnyMediaGroupContentMessage( suspend fun BehaviourContext.waitAnyMediaGroupContentMessage(
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: SimpleFilter<CommonMessage<MediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<MediaGroupContent>>? = null
mapper: CommonMessageToCommonMessageMapper<MediaGroupContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVisualMediaGroupContentMessage( suspend fun BehaviourContext.waitVisualMediaGroupContentMessage(
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: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null
mapper: CommonMessageToCommonMessageMapper<VisualMediaGroupContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitTextedMediaContentMessage( suspend fun BehaviourContext.waitTextedMediaContentMessage(
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: SimpleFilter<CommonMessage<TextedMediaContent>>? = null, filter: SimpleFilter<CommonMessage<TextedMediaContent>>? = null
mapper: CommonMessageToCommonMessageMapper<TextedMediaContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAnimationMessage( suspend fun BehaviourContext.waitAnimationMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<AnimationContent>>? = null, filter: SimpleFilter<CommonMessage<AnimationContent>>? = null
mapper: CommonMessageToCommonMessageMapper<AnimationContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAudioMessage( suspend fun BehaviourContext.waitAudioMessage(
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: SimpleFilter<CommonMessage<AudioContent>>? = null, filter: SimpleFilter<CommonMessage<AudioContent>>? = null
mapper: CommonMessageToCommonMessageMapper<AudioContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDocumentMessage( suspend fun BehaviourContext.waitDocumentMessage(
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: SimpleFilter<CommonMessage<DocumentContent>>? = null, filter: SimpleFilter<CommonMessage<DocumentContent>>? = null
mapper: CommonMessageToCommonMessageMapper<DocumentContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPhotoMessage( suspend fun BehaviourContext.waitPhotoMessage(
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: SimpleFilter<CommonMessage<PhotoContent>>? = null, filter: SimpleFilter<CommonMessage<PhotoContent>>? = null
mapper: CommonMessageToCommonMessageMapper<PhotoContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitStickerMessage( suspend fun BehaviourContext.waitStickerMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<StickerContent>>? = null, filter: SimpleFilter<CommonMessage<StickerContent>>? = null
mapper: CommonMessageToCommonMessageMapper<StickerContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoMessage( suspend fun BehaviourContext.waitVideoMessage(
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: SimpleFilter<CommonMessage<VideoContent>>? = null, filter: SimpleFilter<CommonMessage<VideoContent>>? = null
mapper: CommonMessageToCommonMessageMapper<VideoContent>? = null ) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoNoteMessage( suspend fun BehaviourContext.waitVideoNoteMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null, filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null
mapper: CommonMessageToCommonMessageMapper<VideoNoteContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoiceMessage( suspend fun BehaviourContext.waitVoiceMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<VoiceContent>>? = null, filter: SimpleFilter<CommonMessage<VoiceContent>>? = null
mapper: CommonMessageToCommonMessageMapper<VoiceContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitInvoiceMessage( suspend fun BehaviourContext.waitInvoiceMessage(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null, filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null
mapper: CommonMessageToCommonMessageMapper<InvoiceContent>? = null ) = waitContentMessage(count, initRequest, false, errorFactory, filter)
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)

View File

@ -18,13 +18,12 @@ import dev.inmo.tgbotapi.types.message.content.InvoiceContent
import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
private suspend fun <O> BehaviourContext.waitEditedCommonMessage( private suspend inline fun <reified O : MessageContent> BehaviourContext.waitEditedContent(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<MessageContent>>? = null, filter: SimpleFilter<CommonMessage<O>>? = null
mapper: suspend CommonMessage<MessageContent>.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
@ -32,7 +31,7 @@ private suspend fun <O> BehaviourContext.waitEditedCommonMessage(
) { ) {
val messages = when (it) { val messages = when (it) {
is BaseEditMessageUpdate -> { is BaseEditMessageUpdate -> {
val commonMessage = it.data.asCommonMessage() val commonMessage = it.data.asCommonMessage() ?: return@expectFlow emptyList()
if (commonMessage !is MediaGroupMessage<*> || includeMediaGroups) { if (commonMessage !is MediaGroupMessage<*> || includeMediaGroups) {
listOf(commonMessage) listOf(commonMessage)
} else { } else {
@ -42,211 +41,167 @@ private suspend fun <O> BehaviourContext.waitEditedCommonMessage(
else -> return@expectFlow emptyList() else -> return@expectFlow emptyList()
} }
messages.mapNotNull { message -> messages.mapNotNull { message ->
val asCommonMessage = message as CommonMessage<MessageContent> val asCommonMessage = (message as CommonMessage<*>).withContent<O>() ?: return@mapNotNull null
if (filter == null || filter(asCommonMessage)) { if (filter == null || filter(asCommonMessage)) {
asCommonMessage.mapper() asCommonMessage.content
} else { } else {
null null
} }
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : MessageContent> BehaviourContext.waitEditedContent(
count: Int = 1,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<T>>? = null,
noinline mapper: CommonMessageToContentMapper<T>? = null
) : List<T> = waitEditedCommonMessage<T>(
count,
initRequest,
includeMediaGroups,
errorFactory,
filter ?.let {
{
it.withContent<T>() ?.let { filter(it) } == true
}
},
contentConverter(mapper)
)
suspend fun BehaviourContext.waitEditedContentMessage( suspend fun BehaviourContext.waitEditedContentMessage(
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: SimpleFilter<CommonMessage<MessageContent>>? = null, filter: SimpleFilter<CommonMessage<MessageContent>>? = null
mapper: CommonMessageToContentMapper<MessageContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedContact( suspend fun BehaviourContext.waitEditedContact(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<ContactContent>>? = null, filter: SimpleFilter<CommonMessage<ContactContent>>? = null
mapper: CommonMessageToContentMapper<ContactContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedDice( suspend fun BehaviourContext.waitEditedDice(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<DiceContent>>? = null, filter: SimpleFilter<CommonMessage<DiceContent>>? = null
mapper: CommonMessageToContentMapper<DiceContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedGame( suspend fun BehaviourContext.waitEditedGame(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<GameContent>>? = null, filter: SimpleFilter<CommonMessage<GameContent>>? = null
mapper: CommonMessageToContentMapper<GameContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedLocation( suspend fun BehaviourContext.waitEditedLocation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<LocationContent>>? = null, filter: SimpleFilter<CommonMessage<LocationContent>>? = null
mapper: CommonMessageToContentMapper<LocationContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedLiveLocation( suspend fun BehaviourContext.waitEditedLiveLocation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null, filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null
mapper: CommonMessageToContentMapper<LiveLocationContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedStaticLocation( suspend fun BehaviourContext.waitEditedStaticLocation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null, filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null
mapper: CommonMessageToContentMapper<StaticLocationContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedText( suspend fun BehaviourContext.waitEditedText(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<TextContent>>? = null, filter: SimpleFilter<CommonMessage<TextContent>>? = null
mapper: CommonMessageToContentMapper<TextContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedVenue( suspend fun BehaviourContext.waitEditedVenue(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<VenueContent>>? = null, filter: SimpleFilter<CommonMessage<VenueContent>>? = null
mapper: CommonMessageToContentMapper<VenueContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedAudioMediaGroupContent( suspend fun BehaviourContext.waitEditedAudioMediaGroupContent(
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: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<AudioMediaGroupContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedDocumentMediaGroupContent( suspend fun BehaviourContext.waitEditedDocumentMediaGroupContent(
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: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<DocumentMediaGroupContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedMedia( suspend fun BehaviourContext.waitEditedMedia(
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: SimpleFilter<CommonMessage<MediaContent>>? = null, filter: SimpleFilter<CommonMessage<MediaContent>>? = null
mapper: CommonMessageToContentMapper<MediaContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedAnyMediaGroupContent( suspend fun BehaviourContext.waitEditedAnyMediaGroupContent(
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: SimpleFilter<CommonMessage<MediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<MediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<MediaGroupContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedVisualMediaGroupContent( suspend fun BehaviourContext.waitEditedVisualMediaGroupContent(
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: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null, filter: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null
mapper: CommonMessageToContentMapper<VisualMediaGroupContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedTextedMediaContent( suspend fun BehaviourContext.waitEditedTextedMediaContent(
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: SimpleFilter<CommonMessage<TextedMediaContent>>? = null, filter: SimpleFilter<CommonMessage<TextedMediaContent>>? = null
mapper: CommonMessageToContentMapper<TextedMediaContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedAnimation( suspend fun BehaviourContext.waitEditedAnimation(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<AnimationContent>>? = null, filter: SimpleFilter<CommonMessage<AnimationContent>>? = null
mapper: CommonMessageToContentMapper<AnimationContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedAudio( suspend fun BehaviourContext.waitEditedAudio(
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: SimpleFilter<CommonMessage<AudioContent>>? = null, filter: SimpleFilter<CommonMessage<AudioContent>>? = null
mapper: CommonMessageToContentMapper<AudioContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedDocument( suspend fun BehaviourContext.waitEditedDocument(
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: SimpleFilter<CommonMessage<DocumentContent>>? = null, filter: SimpleFilter<CommonMessage<DocumentContent>>? = null
mapper: CommonMessageToContentMapper<DocumentContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedPhoto( suspend fun BehaviourContext.waitEditedPhoto(
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: SimpleFilter<CommonMessage<PhotoContent>>? = null, filter: SimpleFilter<CommonMessage<PhotoContent>>? = null
mapper: CommonMessageToContentMapper<PhotoContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedSticker( suspend fun BehaviourContext.waitEditedSticker(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<StickerContent>>? = null, filter: SimpleFilter<CommonMessage<StickerContent>>? = null
mapper: CommonMessageToContentMapper<StickerContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedVideo( suspend fun BehaviourContext.waitEditedVideo(
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: SimpleFilter<CommonMessage<VideoContent>>? = null, filter: SimpleFilter<CommonMessage<VideoContent>>? = null
mapper: CommonMessageToContentMapper<VideoContent>? = null ) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter)
) = waitEditedContent(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedVideoNote( suspend fun BehaviourContext.waitEditedVideoNote(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null, filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null
mapper: CommonMessageToContentMapper<VideoNoteContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedVoice( suspend fun BehaviourContext.waitEditedVoice(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<VoiceContent>>? = null, filter: SimpleFilter<CommonMessage<VoiceContent>>? = null
mapper: CommonMessageToContentMapper<VoiceContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitEditedInvoice( suspend fun BehaviourContext.waitEditedInvoice(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null, filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null
mapper: CommonMessageToContentMapper<InvoiceContent>? = null ) = waitEditedContent(count, initRequest, false, errorFactory, filter)
) = waitEditedContent(count, initRequest, false, errorFactory, filter, mapper)

View File

@ -4,8 +4,7 @@ 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.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.ChatEvents.* import dev.inmo.tgbotapi.types.message.ChatEvents.*
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.* import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
@ -17,268 +16,208 @@ import kotlinx.coroutines.flow.toList
typealias EventMessageToEventMapper<T> = suspend ChatEventMessage<T>.() -> T? typealias EventMessageToEventMapper<T> = suspend ChatEventMessage<T>.() -> T?
private suspend fun <O> BehaviourContext.waitEventMessages( private suspend inline fun <reified O : ChatEvent> BehaviourContext.waitEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<ChatEvent>>? = null, filter: SimpleFilter<ChatEventMessage<O>>? = null
mapper: suspend ChatEventMessage<ChatEvent>.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() val data = it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.withEvent<O>() ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data.chatEvent)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : ChatEvent> BehaviourContext.waitEvents(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<ChatEventMessage<T>>? = null,
noinline mapper: EventMessageToEventMapper<T>? = null
) : List<T> = waitEventMessages<T>(
initRequest,
errorFactory,
count,
filter ?.let {
{
(it.chatEvent as? T) ?.let { filter(it as ChatEventMessage<T>) } == true
}
}
) {
if (chatEvent is T) {
@Suppress("UNCHECKED_CAST")
val message = (this as ChatEventMessage<T>)
if (mapper == null) {
message.chatEvent
} else {
mapper(message)
}
} else {
null
}
}
suspend fun BehaviourContext.waitChannelEvents( suspend fun BehaviourContext.waitChannelEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<ChannelEvent>>? = null, filter: SimpleFilter<ChatEventMessage<ChannelEvent>>? = null
mapper: EventMessageToEventMapper<ChannelEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPrivateEvents( suspend fun BehaviourContext.waitPrivateEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<PrivateEvent>>? = null, filter: SimpleFilter<ChatEventMessage<PrivateEvent>>? = null
mapper: EventMessageToEventMapper<PrivateEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<ChatEvent>>? = null, filter: SimpleFilter<ChatEventMessage<ChatEvent>>? = null
mapper: EventMessageToEventMapper<ChatEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice") @Deprecated("Renamed as Video instead of Voice")
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: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null
mapper: EventMessageToEventMapper<VideoChatEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice") @Deprecated("Renamed as Video instead of Voice")
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: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null
mapper: EventMessageToEventMapper<VideoChatStarted>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice") @Deprecated("Renamed as Video instead of Voice")
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: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null
mapper: EventMessageToEventMapper<VideoChatEnded>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice") @Deprecated("Renamed as Video instead of Voice")
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: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null
mapper: EventMessageToEventMapper<VideoChatParticipantsInvited>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatEvents( suspend fun BehaviourContext.waitVideoChatEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null
mapper: EventMessageToEventMapper<VideoChatEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatStartedEvents( suspend fun BehaviourContext.waitVideoChatStartedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null
mapper: EventMessageToEventMapper<VideoChatStarted>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatEndedEvents( suspend fun BehaviourContext.waitVideoChatEndedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null
mapper: EventMessageToEventMapper<VideoChatEnded>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatParticipantsInvitedEvents( suspend fun BehaviourContext.waitVideoChatParticipantsInvitedEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null, filter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null
mapper: EventMessageToEventMapper<VideoChatParticipantsInvited>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<MessageAutoDeleteTimerChanged>>? = null, filter: SimpleFilter<ChatEventMessage<MessageAutoDeleteTimerChanged>>? = null
mapper: EventMessageToEventMapper<MessageAutoDeleteTimerChanged>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPublicChatEvents( suspend fun BehaviourContext.waitPublicChatEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<PublicChatEvent>>? = null, filter: SimpleFilter<ChatEventMessage<PublicChatEvent>>? = null
mapper: EventMessageToEventMapper<PublicChatEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<CommonEvent>>? = null, filter: SimpleFilter<ChatEventMessage<CommonEvent>>? = null
mapper: EventMessageToEventMapper<CommonEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<GroupEvent>>? = null, filter: SimpleFilter<ChatEventMessage<GroupEvent>>? = null
mapper: EventMessageToEventMapper<GroupEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<SupergroupEvent>>? = null, filter: SimpleFilter<ChatEventMessage<SupergroupEvent>>? = null
mapper: EventMessageToEventMapper<SupergroupEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<ChannelChatCreated>>? = null, filter: SimpleFilter<ChatEventMessage<ChannelChatCreated>>? = null
mapper: EventMessageToEventMapper<ChannelChatCreated>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<DeleteChatPhoto>>? = null, filter: SimpleFilter<ChatEventMessage<DeleteChatPhoto>>? = null
mapper: EventMessageToEventMapper<DeleteChatPhoto>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<GroupChatCreated>>? = null, filter: SimpleFilter<ChatEventMessage<GroupChatCreated>>? = null
mapper: EventMessageToEventMapper<GroupChatCreated>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<LeftChatMember>>? = null, filter: SimpleFilter<ChatEventMessage<LeftChatMember>>? = null
mapper: EventMessageToEventMapper<LeftChatMember>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<NewChatPhoto>>? = null, filter: SimpleFilter<ChatEventMessage<NewChatPhoto>>? = null
mapper: EventMessageToEventMapper<NewChatPhoto>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<NewChatMembers>>? = null, filter: SimpleFilter<ChatEventMessage<NewChatMembers>>? = null
mapper: EventMessageToEventMapper<NewChatMembers>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<NewChatTitle>>? = null, filter: SimpleFilter<ChatEventMessage<NewChatTitle>>? = null
mapper: EventMessageToEventMapper<NewChatTitle>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<PinnedMessage>>? = null, filter: SimpleFilter<ChatEventMessage<PinnedMessage>>? = null
mapper: EventMessageToEventMapper<PinnedMessage>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<ProximityAlertTriggered>>? = null, filter: SimpleFilter<ChatEventMessage<ProximityAlertTriggered>>? = null
mapper: EventMessageToEventMapper<ProximityAlertTriggered>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = 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: SimpleFilter<ChatEventMessage<SupergroupChatCreated>>? = null, filter: SimpleFilter<ChatEventMessage<SupergroupChatCreated>>? = null
mapper: EventMessageToEventMapper<SupergroupChatCreated>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitSuccessfulPaymentEvents( suspend fun BehaviourContext.waitSuccessfulPaymentEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<SuccessfulPaymentEvent>>? = null, filter: SimpleFilter<ChatEventMessage<SuccessfulPaymentEvent>>? = null
mapper: EventMessageToEventMapper<SuccessfulPaymentEvent>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitUserLoggedInEvents( suspend fun BehaviourContext.waitUserLoggedInEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ChatEventMessage<UserLoggedIn>>? = null, filter: SimpleFilter<ChatEventMessage<UserLoggedIn>>? = null
mapper: EventMessageToEventMapper<UserLoggedIn>? = null ) = waitEvents(initRequest, errorFactory, count, filter)
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitWebAppDataEvents( suspend fun BehaviourContext.waitWebAppDataEvents(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<PrivateEventMessage<WebAppData>>? = null, filter: SimpleFilter<PrivateEventMessage<WebAppData>>? = null
mapper: EventMessageToEventMapper<WebAppData>? = null ) = waitEvents(initRequest, errorFactory, count, filter ?.let { SimpleFilter<ChatEventMessage<WebAppData>> { it is PrivateEventMessage && filter(it) } })
) = waitEvents(count, initRequest, errorFactory, filter ?.let { { it is PrivateEventMessage && filter(it) } }, mapper)

View File

@ -9,72 +9,40 @@ import kotlinx.coroutines.flow.toList
typealias InlineQueryMapper<T> = suspend T.() -> T? typealias InlineQueryMapper<T> = suspend T.() -> T?
private suspend fun <O> BehaviourContext.waitInlineQueries( private suspend inline fun <reified O : InlineQuery> BehaviourContext.waitInlineQueries(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<InlineQuery>? = null, filter: SimpleFilter<O>? = null
mapper: suspend InlineQuery.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asInlineQueryUpdate() ?.data val data = (it.asInlineQueryUpdate() ?.data as? O) ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : InlineQuery> BehaviourContext.waitInlines(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline mapper: InlineQueryMapper<T>? = null
) : List<T> = waitInlineQueries<T>(
count,
initRequest,
errorFactory,
filter ?.let {
{
(it as? T) ?.let { casted -> filter(casted) } == true
}
}
) {
if (this is T) {
if (mapper == null) {
this
} else {
mapper(this)
}
} else {
null
}
}
suspend fun BehaviourContext.waitAnyInlineQuery( suspend fun BehaviourContext.waitAnyInlineQuery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<InlineQuery>? = null, filter: SimpleFilter<InlineQuery>? = null
mapper: InlineQueryMapper<InlineQuery>? = null ) = waitInlineQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<BaseInlineQuery>? = null, filter: SimpleFilter<BaseInlineQuery>? = null
mapper: InlineQueryMapper<BaseInlineQuery>? = null ) = waitInlineQueries(count, initRequest, errorFactory, filter)
) = 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: SimpleFilter<LocationInlineQuery>? = null, filter: SimpleFilter<LocationInlineQuery>? = null
mapper: InlineQueryMapper<LocationInlineQuery>? = null ) = waitInlineQueries(count, initRequest, errorFactory, filter)
) = waitInlines(count, initRequest, errorFactory, filter, mapper)

View File

@ -1,7 +1,9 @@
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.asSentMediaGroupUpdate import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate
import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
@ -9,20 +11,20 @@ import dev.inmo.tgbotapi.types.message.content.AudioMediaGroupContent
import dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupContent import dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupContent
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent
import dev.inmo.tgbotapi.utils.PreviewFeature
import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
typealias MediaGroupFilter<T> = suspend List<MediaGroupMessage<T>>.() -> Boolean typealias MediaGroupFilter<T> = SimpleFilter<List<MediaGroupMessage<T>>>
internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupWaiter( internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupWaiter(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: MediaGroupFilter<T>? = null filter: SimpleFilter<List<MediaGroupMessage<T>>>? = null
) = flowsUpdatesFilter.expectFlow(bot, initRequest, count, errorFactory) { update -> ) = flowsUpdatesFilter.expectFlow(bot, initRequest, count, errorFactory) { update ->
update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup -> update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup ->
if (mediaGroup.all { message -> message.content is T } && (filter == null || filter(mediaGroup as List<MediaGroupMessage<T>>))) { val mapped = mediaGroup.mapNotNull { it.withContent<T>() }
if (mediaGroup.all { message -> message.content is T } && (filter == null || filter(mapped))) {
listOf( listOf(
mediaGroup.map { it.content as T } mediaGroup.map { it.content as T }
) )
@ -36,35 +38,35 @@ suspend fun BehaviourContext.waitMediaGroup(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: MediaGroupFilter<MediaGroupContent>? = null filter: SimpleFilter<List<MediaGroupMessage<MediaGroupContent>>>? = null
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitPlaylist( suspend fun BehaviourContext.waitPlaylist(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: MediaGroupFilter<AudioMediaGroupContent>? = null filter: SimpleFilter<List<MediaGroupMessage<AudioMediaGroupContent>>>? = null
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitDocumentsGroup( suspend fun BehaviourContext.waitDocumentsGroup(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: MediaGroupFilter<DocumentMediaGroupContent>? = null filter: SimpleFilter<List<MediaGroupMessage<DocumentMediaGroupContent>>>? = null
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitVisualGallery( suspend fun BehaviourContext.waitVisualGallery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: MediaGroupFilter<VisualMediaGroupContent>? = null filter: SimpleFilter<List<MediaGroupMessage<VisualMediaGroupContent>>>? = null
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitPhotoGallery( suspend fun BehaviourContext.waitPhotoGallery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: MediaGroupFilter<PhotoContent>? = null filter: SimpleFilter<List<MediaGroupMessage<PhotoContent>>>? = null
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitVideoGallery( suspend fun BehaviourContext.waitVideoGallery(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: MediaGroupFilter<VideoContent>? = null filter: SimpleFilter<List<MediaGroupMessage<VideoContent>>>? = null
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)

View File

@ -14,12 +14,11 @@ import kotlinx.coroutines.flow.toList
typealias PassportMessageMapper = suspend PassportMessage.() -> PassportData typealias PassportMessageMapper = suspend PassportMessage.() -> PassportData
@RiskFeature("Do not use this message directly, use waitPassportMessagesWith or waitAnyPassportMessages instead") @RiskFeature("Do not use this message directly, use waitPassportMessagesWith or waitAnyPassportMessages instead")
suspend fun <O> BehaviourContext.waitPassportMessages( suspend inline fun <reified O : EncryptedPassportElement> BehaviourContext.waitPassportMessagesWith(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<PassportMessage>? = null, initRequest: Request<*>? = null,
mapper: suspend PassportMessage.() -> O? noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<PassportMessage>? = null
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
@ -27,39 +26,15 @@ suspend fun <O> BehaviourContext.waitPassportMessages(
) { ) {
val data = it.asMessageUpdate() ?.data ?.asPassportMessage() ?: return@expectFlow emptyList() val data = it.asMessageUpdate() ?.data ?.asPassportMessage() ?: return@expectFlow emptyList()
if (filter == null || filter(data)) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) data.passportData.data.filterIsInstance<O>()
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.waitPassportMessagesWith(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<PassportMessage>? = null,
noinline mapper: PassportMessageMapper? = null
) : List<PassportData> = waitPassportMessages(
initRequest,
errorFactory,
count,
filter
) {
if (passportData.data.any { it is T }) {
if (mapper == null) {
passportData
} else {
mapper(this)
}
} else {
null
}
}
suspend fun BehaviourContext.waitAnyPassportMessages( suspend fun BehaviourContext.waitAnyPassportMessages(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<PassportMessage>? = null, filter: SimpleFilter<PassportMessage>? = null
mapper: PassportMessageMapper? = null ) = waitPassportMessagesWith<EncryptedPassportElement>(count, initRequest, errorFactory, filter)
) = waitPassportMessagesWith<EncryptedPassportElement>(count, initRequest, errorFactory, filter, mapper)

View File

@ -9,49 +9,24 @@ import kotlinx.coroutines.flow.toList
typealias PollAnswerMapper = suspend PollAnswer.() -> PollAnswer? typealias PollAnswerMapper = suspend PollAnswer.() -> PollAnswer?
private suspend fun <O> BehaviourContext.waitPollsAnswers( private suspend inline fun <reified O : PollAnswer> BehaviourContext.waitPollAnswers(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<PollAnswer>? = null, filter: SimpleFilter<O>? = null
mapper: suspend PollAnswer.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asPollAnswerUpdate() ?.data val data = it.asPollAnswerUpdate() ?.data as? O ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun BehaviourContext.waitPollAnswers(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<PollAnswer>? = null,
noinline mapper: PollAnswerMapper? = null
) : List<PollAnswer> = this@waitPollAnswers.waitPollsAnswers<PollAnswer>(
count,
initRequest,
errorFactory,
filter ?.let {
{
(it as? PollAnswer) ?.let { filter(it) } == true
}
}
) {
if (mapper == null) {
this
} else {
mapper(this)
}
}
/** /**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot * This wait will be triggered only for stopped polls and polls, which are sent by the bot
*/ */
@ -59,6 +34,5 @@ suspend fun BehaviourContext.waitPollAnswers(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<PollAnswer>? = null, filter: SimpleFilter<PollAnswer>? = null
mapper: PollAnswerMapper? = null ) = waitPollAnswers(count, initRequest, errorFactory, filter)
) = waitPollAnswers(count, initRequest, errorFactory, filter, mapper)

View File

@ -9,53 +9,24 @@ import kotlinx.coroutines.flow.toList
typealias PollMapper<T> = suspend T.() -> T? typealias PollMapper<T> = suspend T.() -> T?
private suspend fun <O> BehaviourContext.waitPollsUpdates( private suspend inline fun <reified O> BehaviourContext.waitPolls(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<Poll>? = null, filter: SimpleFilter<O>? = null
mapper: suspend Poll.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asPollUpdate() ?.data val data = it.asPollUpdate() ?.data as? O ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
}.toList().toList() }.toList().toList()
private suspend inline fun <reified T : Poll> BehaviourContext.waitPolls(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<T>? = null,
noinline mapper: PollMapper<T>? = null
) : List<T> = this@waitPolls.waitPollsUpdates<T>(
count,
initRequest,
errorFactory,
filter ?.let {
{
(it as? T) ?.let { filter(it) } == true
}
}
) {
if (this is T) {
if (mapper == null) {
this
} else {
mapper(this)
}
} else {
null
}
}
/** /**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot * This wait will be triggered only for stopped polls and polls, which are sent by the bot
*/ */
@ -63,9 +34,8 @@ suspend fun BehaviourContext.waitPollUpdates(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<Poll>? = null, filter: SimpleFilter<Poll>? = null
mapper: PollMapper<Poll>? = null ) = waitPolls(count, initRequest, errorFactory, filter)
) = waitPolls(count, initRequest, errorFactory, filter, mapper)
/** /**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot * This wait will be triggered only for stopped polls and polls, which are sent by the bot
@ -74,9 +44,8 @@ suspend fun BehaviourContext.waitQuizPollUpdates(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<QuizPoll>? = null, filter: SimpleFilter<QuizPoll>? = null
mapper: PollMapper<QuizPoll>? = null ) = waitPolls(count, initRequest, errorFactory, filter)
) = waitPolls(count, initRequest, errorFactory, filter, mapper)
/** /**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot * This wait will be triggered only for stopped polls and polls, which are sent by the bot
@ -85,6 +54,5 @@ suspend fun BehaviourContext.waitRegularPollUpdates(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<RegularPoll>? = null, filter: SimpleFilter<RegularPoll>? = null
mapper: PollMapper<RegularPoll>? = null ) = waitPolls(count, initRequest, errorFactory, filter)
) = waitPolls(count, initRequest, errorFactory, filter, mapper)

View File

@ -9,20 +9,19 @@ import kotlinx.coroutines.flow.toList
typealias PreCheckoutQueryMapper = suspend PreCheckoutQuery.() -> PreCheckoutQuery? typealias PreCheckoutQueryMapper = suspend PreCheckoutQuery.() -> PreCheckoutQuery?
private suspend fun <O> BehaviourContext.waitPreCheckoutQueries( private suspend inline fun <reified O : PreCheckoutQuery> BehaviourContext.waitPreCheckoutQueries(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<PreCheckoutQuery>? = null, filter: SimpleFilter<O>? = null
mapper: suspend PreCheckoutQuery.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asPreCheckoutQueryUpdate() ?.data val data = it.asPreCheckoutQueryUpdate() ?.data as? O ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
@ -33,17 +32,10 @@ suspend fun BehaviourContext.waitPreCheckoutQueries(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<PreCheckoutQuery>? = null, filter: SimpleFilter<PreCheckoutQuery>? = null
mapper: PreCheckoutQueryMapper? = null
) : List<PreCheckoutQuery> = waitPreCheckoutQueries( ) : List<PreCheckoutQuery> = waitPreCheckoutQueries(
count, count,
initRequest, initRequest,
errorFactory, errorFactory,
filter filter
) { )
if (mapper == null) {
this
} else {
mapper(this)
}
}

View File

@ -9,20 +9,19 @@ import kotlinx.coroutines.flow.toList
typealias ShippingQueryMapper = suspend ShippingQuery.() -> ShippingQuery? typealias ShippingQueryMapper = suspend ShippingQuery.() -> ShippingQuery?
private suspend fun <O> BehaviourContext.waitShippingQueries( private suspend inline fun <reified O : ShippingQuery> BehaviourContext.waitShippingQueries(
count: Int = 1, count: Int = 1,
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, noinline errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<ShippingQuery>? = null, filter: SimpleFilter<O>? = null
mapper: suspend ShippingQuery.() -> O?
): List<O> = expectFlow( ): List<O> = expectFlow(
initRequest, initRequest,
count, count,
errorFactory errorFactory
) { ) {
val data = it.asShippingQueryUpdate() ?.data val data = it.asShippingQueryUpdate() ?.data as? O ?: return@expectFlow emptyList()
if (data != null && (filter == null || filter(data))) { if (filter == null || filter(data)) {
data.mapper().let(::listOfNotNull) listOf(data)
} else { } else {
emptyList() emptyList()
} }
@ -33,17 +32,10 @@ suspend fun BehaviourContext.waitShippingQueries(
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }, errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1, count: Int = 1,
filter: SimpleFilter<ShippingQuery>? = null, filter: SimpleFilter<ShippingQuery>? = null
mapper: ShippingQueryMapper? = null
) : List<ShippingQuery> = waitShippingQueries( ) : List<ShippingQuery> = waitShippingQueries(
count, count,
initRequest, initRequest,
errorFactory, errorFactory,
filter filter
) { )
if (mapper == null) {
this
} else {
mapper(this)
}
}

View File

@ -34,7 +34,6 @@ import dev.inmo.tgbotapi.types.message.content.MediaContent
import dev.inmo.tgbotapi.types.message.content.MessageContent import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.PreviewFeature
internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent> BC.onEditedContent( internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent> BC.onEditedContent(
initialFilter: CommonMessageFilter<T>? = null, initialFilter: CommonMessageFilter<T>? = null,

View File

@ -0,0 +1,31 @@
@file:Suppress("UNCHECKED_CAST")
package dev.inmo.tgbotapi.extensions.utils
import dev.inmo.tgbotapi.types.message.*
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
inline fun <reified T : ChatEvent> ChatEventMessage<*>.withEvent() = if (chatEvent is T) { this as ChatEventMessage<T> } else { null }
inline fun <reified T : ChatEvent> ChatEventMessage<*>.requireWithEvent() = withEvent<T>()!!
inline fun <reified T : GroupEvent> GroupEventMessage<*>.withEvent() = if (chatEvent is T) { this as GroupEventMessage<T> } else { null }
inline fun <reified T : GroupEvent> GroupEventMessage<*>.requireWithEvent() = withEvent<T>()!!
inline fun <reified T : SupergroupEvent> SupergroupEventMessage<*>.withEvent() = if (chatEvent is T) { this as SupergroupEventMessage<T> } else { null }
inline fun <reified T : SupergroupEvent> SupergroupEventMessage<*>.requireWithEvent() = withEvent<T>()!!
inline fun <reified T : PrivateEvent> PrivateEventMessage<*>.withEvent() = if (chatEvent is T) { this as PrivateEventMessage<T> } else { null }
inline fun <reified T : PrivateEvent> PrivateEventMessage<*>.requireWithEvent() = withEvent<T>()!!
inline fun <reified T : ChannelEvent> ChannelEventMessage<*>.withEvent() = if (chatEvent is T) { this as ChannelEventMessage<T> } else { null }
inline fun <reified T : ChannelEvent> ChannelEventMessage<*>.requireWithEvent() = withEvent<T>()!!
inline fun <reified T : GroupEvent> CommonGroupEventMessage<*>.withEvent() = if (chatEvent is T) { this as CommonGroupEventMessage<T> } else { null }
inline fun <reified T : GroupEvent> CommonGroupEventMessage<*>.requireWithEvent() = withEvent<T>()!!
inline fun <reified T : SupergroupEvent> CommonSupergroupEventMessage<*>.withEvent() = if (chatEvent is T) { this as CommonSupergroupEventMessage<T> } else { null }
inline fun <reified T : SupergroupEvent> CommonSupergroupEventMessage<*>.requireWithEvent() = withEvent<T>()!!