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

add performInParallel for behaviour builder

This commit is contained in:
InsanusMokrassar 2021-06-25 12:45:20 +06:00
parent 85270dd12d
commit bf28a8b0a6
10 changed files with 271 additions and 136 deletions

View File

@ -7,6 +7,8 @@
* `MicroUtils`: `0.5.6` -> `0.5.13` * `MicroUtils`: `0.5.6` -> `0.5.13`
* `Behaviour Builder`: * `Behaviour Builder`:
* New extensions `telegramBotWithBehaviour` * New extensions `telegramBotWithBehaviour`
* All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter
`performInParallel`, by default `true`)
* `API`: * `API`:
* All `reply` and subsequent extension have been replaced in send package * All `reply` and subsequent extension have been replaced in send package

View File

@ -4,13 +4,17 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.types.CallbackQuery.* import dev.inmo.tgbotapi.types.CallbackQuery.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallbackQuery( internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (T) -> Boolean)? = null, noinline additionalFilter: (suspend (T) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
it.asCallbackQueryUpdate() ?.data ?.let { query -> it.asCallbackQueryUpdate() ?.data ?.let { query ->
@ -20,7 +24,9 @@ internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallb
null null
} }
}.let(::listOfNotNull) }.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerQuery -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerQuery ->
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId }
@ -31,52 +37,62 @@ internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallb
) { ) {
scenarioReceiver(triggerQuery) scenarioReceiver(triggerQuery)
} }
} }
)
suspend fun BehaviourContext.onDataCallbackQuery( suspend fun BehaviourContext.onDataCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (DataCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (DataCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, DataCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, DataCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGameShortNameCallbackQuery( suspend fun BehaviourContext.onGameShortNameCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (GameShortNameCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (GameShortNameCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, GameShortNameCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, GameShortNameCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInlineMessageIdCallbackQuery( suspend fun BehaviourContext.onInlineMessageIdCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineMessageIdCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (InlineMessageIdCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInlineMessageIdDataCallbackQuery( suspend fun BehaviourContext.onInlineMessageIdDataCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineMessageIdDataCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (InlineMessageIdDataCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdDataCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdDataCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInlineMessageIdGameShortNameCallbackQuery( suspend fun BehaviourContext.onInlineMessageIdGameShortNameCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineMessageIdGameShortNameCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (InlineMessageIdGameShortNameCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdGameShortNameCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdGameShortNameCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageCallbackQuery( suspend fun BehaviourContext.onMessageCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (MessageCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (MessageCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageDataCallbackQuery( suspend fun BehaviourContext.onMessageDataCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (MessageDataCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (MessageDataCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageDataCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageDataCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageGameShortNameCallbackQuery( suspend fun BehaviourContext.onMessageGameShortNameCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (MessageGameShortNameCallbackQuery) -> Boolean)? = null, additionalFilter: (suspend (MessageGameShortNameCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageGameShortNameCallbackQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageGameShortNameCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onUnknownCallbackQueryType( suspend fun BehaviourContext.onUnknownCallbackQueryType(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (UnknownCallbackQueryType) -> Boolean)? = null, additionalFilter: (suspend (UnknownCallbackQueryType) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, UnknownCallbackQueryType> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, UnknownCallbackQueryType>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)

View File

@ -3,21 +3,27 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.types.ChatMemberUpdated import dev.inmo.tgbotapi.types.ChatMemberUpdated
import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate
import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate
import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
internal suspend inline fun <reified U : ChatMemberUpdatedUpdate> BehaviourContext.onChatMemberUpdatedInternal( internal suspend inline fun <reified U : ChatMemberUpdatedUpdate> BehaviourContext.onChatMemberUpdatedInternal(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, noinline additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
(it as? U) ?.data ?.let { chatMemberUpdated -> (it as? U) ?.data ?.let { chatMemberUpdated ->
if (additionalFilter == null || additionalFilter(chatMemberUpdated)) chatMemberUpdated else null if (additionalFilter == null || additionalFilter(chatMemberUpdated)) chatMemberUpdated else null
}.let(::listOfNotNull) }.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerChatMemberUpdated -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerChatMemberUpdated ->
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId }
@ -28,34 +34,41 @@ internal suspend inline fun <reified U : ChatMemberUpdatedUpdate> BehaviourConte
) { ) {
scenarioReceiver(triggerChatMemberUpdated) scenarioReceiver(triggerChatMemberUpdated)
} }
} }
)
suspend fun BehaviourContext.onChatMemberUpdated( suspend fun BehaviourContext.onChatMemberUpdated(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = onChatMemberUpdatedInternal<ChatMemberUpdatedUpdate>( ) = onChatMemberUpdatedInternal<ChatMemberUpdatedUpdate>(
includeFilterByChatInBehaviourSubContext, includeFilterByChatInBehaviourSubContext,
additionalFilter, additionalFilter,
performInParallel,
scenarioReceiver scenarioReceiver
) )
suspend fun BehaviourContext.onCommonChatMemberUpdated( suspend fun BehaviourContext.onCommonChatMemberUpdated(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = onChatMemberUpdatedInternal<CommonChatMemberUpdatedUpdate>( ) = onChatMemberUpdatedInternal<CommonChatMemberUpdatedUpdate>(
includeFilterByChatInBehaviourSubContext, includeFilterByChatInBehaviourSubContext,
additionalFilter, additionalFilter,
performInParallel,
scenarioReceiver scenarioReceiver
) )
suspend fun BehaviourContext.onMyChatMemberUpdated( suspend fun BehaviourContext.onMyChatMemberUpdated(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = onChatMemberUpdatedInternal<MyChatMemberUpdatedUpdate>( ) = onChatMemberUpdatedInternal<MyChatMemberUpdatedUpdate>(
includeFilterByChatInBehaviourSubContext, includeFilterByChatInBehaviourSubContext,
additionalFilter, additionalFilter,
performInParallel,
scenarioReceiver scenarioReceiver
) )

View File

@ -12,6 +12,7 @@ suspend fun BehaviourContext.command(
requireOnlyCommandInMessage: Boolean = true, requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<TextContent>? = null, additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
): Job = onText( ): Job = onText(
includeFilterByChatInBehaviourSubContext, includeFilterByChatInBehaviourSubContext,
@ -27,6 +28,7 @@ suspend fun BehaviourContext.command(
commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false)
} && (additionalFilter ?.invoke(message) != false) } && (additionalFilter ?.invoke(message) != false)
}, },
performInParallel,
scenarioReceiver scenarioReceiver
) )
suspend fun BehaviourContext.command( suspend fun BehaviourContext.command(
@ -34,21 +36,24 @@ suspend fun BehaviourContext.command(
requireOnlyCommandInMessage: Boolean = true, requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<TextContent>? = null, additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
) = command(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = command(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend inline fun BehaviourContext.onCommand( suspend inline fun BehaviourContext.onCommand(
commandRegex: Regex, commandRegex: Regex,
requireOnlyCommandInMessage: Boolean = true, requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: CommonMessageFilter<TextContent>? = null, noinline additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend inline fun BehaviourContext.onCommand( suspend inline fun BehaviourContext.onCommand(
command: String, command: String,
requireOnlyCommandInMessage: Boolean = true, requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: CommonMessageFilter<TextContent>? = null, noinline additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)

View File

@ -5,6 +5,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
@ -22,6 +23,7 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
noinline additionalFilter: CommonMessageFilter<T>? = null, noinline additionalFilter: CommonMessageFilter<T>? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<T>> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<T>>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
if (includeMediaGroups) { if (includeMediaGroups) {
@ -44,7 +46,9 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
null null
} }
}.let(::listOfNotNull) }.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerMessage ->
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
@ -55,117 +59,139 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
) { ) {
scenarioReceiver(triggerMessage) scenarioReceiver(triggerMessage)
} }
} }
)
suspend fun BehaviourContext.onContentMessage( suspend fun BehaviourContext.onContentMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<MessageContent>? = null, additionalFilter: CommonMessageFilter<MessageContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MessageContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MessageContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onContact( suspend fun BehaviourContext.onContact(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<ContactContent>? = null, additionalFilter: CommonMessageFilter<ContactContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<ContactContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<ContactContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDice( suspend fun BehaviourContext.onDice(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<DiceContent>? = null, additionalFilter: CommonMessageFilter<DiceContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DiceContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DiceContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGame( suspend fun BehaviourContext.onGame(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<GameContent>? = null, additionalFilter: CommonMessageFilter<GameContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<GameContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<GameContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onLocation( suspend fun BehaviourContext.onLocation(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<LocationContent>? = null, additionalFilter: CommonMessageFilter<LocationContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<LocationContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<LocationContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPoll( suspend fun BehaviourContext.onPoll(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<PollContent>? = null, additionalFilter: CommonMessageFilter<PollContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<PollContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<PollContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onText( suspend fun BehaviourContext.onText(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<TextContent>? = null, additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVenue( suspend fun BehaviourContext.onVenue(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<VenueContent>? = null, additionalFilter: CommonMessageFilter<VenueContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VenueContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VenueContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onAudioMediaGroup( suspend fun BehaviourContext.onAudioMediaGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<AudioMediaGroupContent>? = null, additionalFilter: CommonMessageFilter<AudioMediaGroupContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AudioMediaGroupContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AudioMediaGroupContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDocumentMediaGroupContent( suspend fun BehaviourContext.onDocumentMediaGroupContent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
additionalFilter: CommonMessageFilter<DocumentMediaGroupContent>? = null, additionalFilter: CommonMessageFilter<DocumentMediaGroupContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DocumentMediaGroupContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DocumentMediaGroupContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMediaCollection( suspend fun BehaviourContext.onMediaCollection(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
additionalFilter: (suspend (CommonMessage<MediaCollectionContent<TelegramMediaFile>>) -> Boolean)? = null, additionalFilter: (suspend (CommonMessage<MediaCollectionContent<TelegramMediaFile>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MediaCollectionContent<TelegramMediaFile>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MediaCollectionContent<TelegramMediaFile>>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMedia( suspend fun BehaviourContext.onMedia(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = true, includeMediaGroups: Boolean = true,
additionalFilter: CommonMessageFilter<MediaContent>? = null, additionalFilter: CommonMessageFilter<MediaContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MediaContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MediaContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onAnimation( suspend fun BehaviourContext.onAnimation(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<AnimationContent>? = null, additionalFilter: CommonMessageFilter<AnimationContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AnimationContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AnimationContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onAudio( suspend fun BehaviourContext.onAudio(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<AudioContent>? = null, additionalFilter: CommonMessageFilter<AudioContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AudioContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AudioContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDocument( suspend fun BehaviourContext.onDocument(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<DocumentContent>? = null, additionalFilter: CommonMessageFilter<DocumentContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DocumentContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DocumentContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPhoto( suspend fun BehaviourContext.onPhoto(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<PhotoContent>? = null, additionalFilter: CommonMessageFilter<PhotoContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<PhotoContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<PhotoContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onSticker( suspend fun BehaviourContext.onSticker(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<StickerContent>? = null, additionalFilter: CommonMessageFilter<StickerContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<StickerContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<StickerContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVideo( suspend fun BehaviourContext.onVideo(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false, includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<VideoContent>? = null, additionalFilter: CommonMessageFilter<VideoContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VideoContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VideoContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVideoNote( suspend fun BehaviourContext.onVideoNote(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<VideoNoteContent>? = null, additionalFilter: CommonMessageFilter<VideoNoteContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VideoNoteContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VideoNoteContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoice( suspend fun BehaviourContext.onVoice(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<VoiceContent>? = null, additionalFilter: CommonMessageFilter<VoiceContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VoiceContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VoiceContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInvoice( suspend fun BehaviourContext.onInvoice(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<InvoiceContent>? = null, additionalFilter: CommonMessageFilter<InvoiceContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<InvoiceContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<InvoiceContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)

View File

@ -4,6 +4,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate
import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
@ -15,6 +16,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage
internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent( internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (ChatEventMessage<T>) -> Boolean)? = null, noinline additionalFilter: (suspend (ChatEventMessage<T>) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<T>> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<T>>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.let { message -> it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.let { message ->
@ -25,7 +27,9 @@ internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
null null
} }
}.let(::listOfNotNull) }.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerMessage ->
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
@ -34,106 +38,127 @@ internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
) { ) {
scenarioReceiver(triggerMessage) scenarioReceiver(triggerMessage)
} }
} }
)
suspend fun BehaviourContext.onChannelEvent( suspend fun BehaviourContext.onChannelEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ChannelEvent>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<ChannelEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChannelEvent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChannelEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onChatEvent( suspend fun BehaviourContext.onChatEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ChatEvent>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<ChatEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChatEvent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChatEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatEvent( suspend fun BehaviourContext.onVoiceChatEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatEvent>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<VoiceChatEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatEvent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatStartedEvent( suspend fun BehaviourContext.onVoiceChatStartedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatStarted>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<VoiceChatStarted>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatStarted>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatStarted>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatEndedEvent( suspend fun BehaviourContext.onVoiceChatEndedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatEnded>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<VoiceChatEnded>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatEnded>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatEnded>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatParticipantsInvitedEvent( suspend fun BehaviourContext.onVoiceChatParticipantsInvitedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatParticipantsInvited>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<VoiceChatParticipantsInvited>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatParticipantsInvited>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatParticipantsInvited>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageAutoDeleteTimerChangedEvent( suspend fun BehaviourContext.onMessageAutoDeleteTimerChangedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<MessageAutoDeleteTimerChanged>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<MessageAutoDeleteTimerChanged>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<MessageAutoDeleteTimerChanged>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<MessageAutoDeleteTimerChanged>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onCommonEvent( suspend fun BehaviourContext.onCommonEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<CommonEvent>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<CommonEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<CommonEvent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<CommonEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGroupEvent( suspend fun BehaviourContext.onGroupEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<GroupEvent>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<GroupEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<GroupEvent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<GroupEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onSupergroupEvent( suspend fun BehaviourContext.onSupergroupEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<SupergroupEvent>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<SupergroupEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<SupergroupEvent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<SupergroupEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onChannelChatCreated( suspend fun BehaviourContext.onChannelChatCreated(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ChannelChatCreated>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<ChannelChatCreated>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChannelChatCreated>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChannelChatCreated>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDeleteChatPhoto( suspend fun BehaviourContext.onDeleteChatPhoto(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<DeleteChatPhoto>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<DeleteChatPhoto>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<DeleteChatPhoto>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<DeleteChatPhoto>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGroupChatCreated( suspend fun BehaviourContext.onGroupChatCreated(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<GroupChatCreated>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<GroupChatCreated>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<GroupChatCreated>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<GroupChatCreated>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onLeftChatMember( suspend fun BehaviourContext.onLeftChatMember(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<LeftChatMember>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<LeftChatMember>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<LeftChatMember>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<LeftChatMember>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onNewChatMembers( suspend fun BehaviourContext.onNewChatMembers(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<NewChatMembers>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<NewChatMembers>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatMembers>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatMembers>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onNewChatPhoto( suspend fun BehaviourContext.onNewChatPhoto(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<NewChatPhoto>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<NewChatPhoto>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatPhoto>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatPhoto>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onNewChatTitle( suspend fun BehaviourContext.onNewChatTitle(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<NewChatTitle>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<NewChatTitle>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatTitle>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatTitle>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPinnedMessage( suspend fun BehaviourContext.onPinnedMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<PinnedMessage>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<PinnedMessage>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<PinnedMessage>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<PinnedMessage>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onProximityAlertTriggered( suspend fun BehaviourContext.onProximityAlertTriggered(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ProximityAlertTriggered>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<ProximityAlertTriggered>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ProximityAlertTriggered>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ProximityAlertTriggered>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onSupergroupChatCreated( suspend fun BehaviourContext.onSupergroupChatCreated(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<SupergroupChatCreated>) -> Boolean)? = null, additionalFilter: (suspend (ChatEventMessage<SupergroupChatCreated>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<SupergroupChatCreated>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<SupergroupChatCreated>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)

View File

@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.types.InlineQueries.query.* import dev.inmo.tgbotapi.types.InlineQueries.query.*
@ -10,6 +11,7 @@ import dev.inmo.tgbotapi.types.InlineQueries.query.*
internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQuery( internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (T) -> Boolean)? = null, noinline additionalFilter: (suspend (T) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
it.asInlineQueryUpdate() ?.data ?.let { query -> it.asInlineQueryUpdate() ?.data ?.let { query ->
@ -19,7 +21,9 @@ internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQ
null null
} }
}.let(::listOfNotNull) }.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerQuery -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerQuery ->
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId }
@ -30,25 +34,29 @@ internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQ
) { ) {
scenarioReceiver(triggerQuery) scenarioReceiver(triggerQuery)
} }
} }
)
suspend fun BehaviourContext.onAnyInlineQuery( suspend fun BehaviourContext.onAnyInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineQuery) -> Boolean)? = null, additionalFilter: (suspend (InlineQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineQuery>
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onBaseInlineQuery( suspend fun BehaviourContext.onBaseInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (BaseInlineQuery) -> Boolean)? = null, additionalFilter: (suspend (BaseInlineQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, BaseInlineQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, BaseInlineQuery>
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onLocationInlineQuery( suspend fun BehaviourContext.onLocationInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (LocationInlineQuery) -> Boolean)? = null, additionalFilter: (suspend (LocationInlineQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, LocationInlineQuery> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, LocationInlineQuery>
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)

View File

@ -5,6 +5,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
@ -18,6 +19,7 @@ import dev.inmo.tgbotapi.utils.PreviewFeature
internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupTrigger( internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupTrigger(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (List<MediaGroupMessage<T>>) -> Boolean)? = null, noinline additionalFilter: (suspend (List<MediaGroupMessage<T>>) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<T>>> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<T>>>
) = flowsUpdatesFilter.expectFlow(bot) { update -> ) = flowsUpdatesFilter.expectFlow(bot) { update ->
update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup -> update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup ->
@ -27,7 +29,9 @@ internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.bui
null null
} }
} ?: emptyList() } ?: emptyList()
}.subscribeSafelyWithoutExceptions(scope) { mediaGroup -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { mediaGroup ->
val mediaGroupChat = mediaGroup.chat!! val mediaGroupChat = mediaGroup.chat!!
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
@ -37,41 +41,49 @@ internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.bui
) { ) {
scenarioReceiver(mediaGroup) scenarioReceiver(mediaGroup)
} }
} }
)
suspend fun BehaviourContext.onMediaGroup( suspend fun BehaviourContext.onMediaGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<MediaGroupContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<MediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<MediaGroupContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<MediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPlaylist( suspend fun BehaviourContext.onPlaylist(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<AudioMediaGroupContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<AudioMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<AudioMediaGroupContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<AudioMediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDocumentsGroup( suspend fun BehaviourContext.onDocumentsGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<DocumentMediaGroupContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<DocumentMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<DocumentMediaGroupContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<DocumentMediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVisualGallery( suspend fun BehaviourContext.onVisualGallery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VisualMediaGroupContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VisualMediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVisualMediaGroup( suspend fun BehaviourContext.onVisualMediaGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VisualMediaGroupContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VisualMediaGroupContent>>>
) = onVisualGallery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = onVisualGallery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPhotoGallery( suspend fun BehaviourContext.onPhotoGallery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<PhotoContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<PhotoContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<PhotoContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<PhotoContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVideoGallery( suspend fun BehaviourContext.onVideoGallery(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<VideoContent>>) -> Boolean)? = null, additionalFilter: (suspend (List<MediaGroupMessage<VideoContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VideoContent>>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VideoContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) ) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)

View File

@ -3,15 +3,17 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWrapWithLaunch
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate
import dev.inmo.tgbotapi.extensions.utils.asPassportMessage import dev.inmo.tgbotapi.extensions.utils.asPassportMessage
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.types.message.PassportMessage import dev.inmo.tgbotapi.types.message.PassportMessage
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement
suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPassportMessageWith( internal suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPassportMessageWith(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (PassportMessage) -> Boolean)? = null, noinline additionalFilter: (suspend (PassportMessage) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, PassportMessage> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, PassportMessage>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
it.asMessageUpdate() ?.data ?.asPassportMessage() ?.let { message -> it.asMessageUpdate() ?.data ?.asPassportMessage() ?.let { message ->
@ -21,7 +23,9 @@ suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPas
null null
} }
}.let(::listOfNotNull) }.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage -> }.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerMessage ->
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
@ -30,15 +34,18 @@ suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPas
) { ) {
scenarioReceiver(triggerMessage) scenarioReceiver(triggerMessage)
} }
} }
)
suspend fun BehaviourContext.onPassportMessage( suspend fun BehaviourContext.onPassportMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (PassportMessage) -> Boolean)? = null, additionalFilter: (suspend (PassportMessage) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, PassportMessage> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, PassportMessage>
) = onPassportMessageWith<EncryptedPassportElement>( ) = onPassportMessageWith<EncryptedPassportElement>(
includeFilterByChatInBehaviourSubContext, includeFilterByChatInBehaviourSubContext,
additionalFilter, additionalFilter,
performInParallel,
scenarioReceiver scenarioReceiver
) )

View File

@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import kotlinx.coroutines.CoroutineScope
internal fun <T> CoroutineScope.wrapWithLaunch(
block: suspend (T) -> Unit
): suspend (T) -> Unit = {
launchSafelyWithoutExceptions {
block(it)
}
}
internal fun <T> CoroutineScope.optionallyWrapWithLaunch(
wrap: Boolean,
block: suspend (T) -> Unit
): suspend (T) -> Unit = if (wrap) {
wrapWithLaunch(block)
} else {
block
}