1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +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`
* `Behaviour Builder`:
* New extensions `telegramBotWithBehaviour`
* All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter
`performInParallel`, by default `true`)
* `API`:
* 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.tgbotapi.extensions.behaviour_builder.*
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.extensions.sourceChat
import dev.inmo.tgbotapi.types.CallbackQuery.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (T) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T>
) = flowsUpdatesFilter.expectFlow(bot) {
it.asCallbackQueryUpdate() ?.data ?.let { query ->
@ -20,7 +24,9 @@ internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallb
null
}
}.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerQuery ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerQuery ->
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId }
@ -32,51 +38,61 @@ internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallb
scenarioReceiver(triggerQuery)
}
}
)
suspend fun BehaviourContext.onDataCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (DataCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, DataCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGameShortNameCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (GameShortNameCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, GameShortNameCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInlineMessageIdCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineMessageIdCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInlineMessageIdDataCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineMessageIdDataCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdDataCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInlineMessageIdGameShortNameCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineMessageIdGameShortNameCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineMessageIdGameShortNameCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (MessageCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageDataCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (MessageDataCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageDataCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageGameShortNameCallbackQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (MessageGameShortNameCallbackQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, MessageGameShortNameCallbackQuery>
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onUnknownCallbackQueryType(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (UnknownCallbackQueryType) -> Boolean)? = null,
performInParallel: Boolean = true,
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.tgbotapi.extensions.behaviour_builder.*
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.types.ChatMemberUpdated
import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate
import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate
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(
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = flowsUpdatesFilter.expectFlow(bot) {
(it as? U) ?.data ?.let { chatMemberUpdated ->
if (additionalFilter == null || additionalFilter(chatMemberUpdated)) chatMemberUpdated else null
}.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerChatMemberUpdated ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerChatMemberUpdated ->
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId }
@ -29,33 +35,40 @@ internal suspend inline fun <reified U : ChatMemberUpdatedUpdate> BehaviourConte
scenarioReceiver(triggerChatMemberUpdated)
}
}
)
suspend fun BehaviourContext.onChatMemberUpdated(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = onChatMemberUpdatedInternal<ChatMemberUpdatedUpdate>(
includeFilterByChatInBehaviourSubContext,
additionalFilter,
performInParallel,
scenarioReceiver
)
suspend fun BehaviourContext.onCommonChatMemberUpdated(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = onChatMemberUpdatedInternal<CommonChatMemberUpdatedUpdate>(
includeFilterByChatInBehaviourSubContext,
additionalFilter,
performInParallel,
scenarioReceiver
)
suspend fun BehaviourContext.onMyChatMemberUpdated(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatMemberUpdated>
) = onChatMemberUpdatedInternal<MyChatMemberUpdatedUpdate>(
includeFilterByChatInBehaviourSubContext,
additionalFilter,
performInParallel,
scenarioReceiver
)

View File

@ -12,6 +12,7 @@ suspend fun BehaviourContext.command(
requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
): Job = onText(
includeFilterByChatInBehaviourSubContext,
@ -27,6 +28,7 @@ suspend fun BehaviourContext.command(
commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false)
} && (additionalFilter ?.invoke(message) != false)
},
performInParallel,
scenarioReceiver
)
suspend fun BehaviourContext.command(
@ -34,21 +36,24 @@ suspend fun BehaviourContext.command(
requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
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(
commandRegex: Regex,
requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
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(
command: String,
requireOnlyCommandInMessage: Boolean = true,
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
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.tgbotapi.extensions.behaviour_builder.*
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.extensions.sourceChat
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
@ -22,6 +23,7 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = true,
noinline additionalFilter: CommonMessageFilter<T>? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<T>>
) = flowsUpdatesFilter.expectFlow(bot) {
if (includeMediaGroups) {
@ -44,7 +46,9 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
null
}
}.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerMessage ->
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
@ -56,116 +60,138 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
scenarioReceiver(triggerMessage)
}
}
)
suspend fun BehaviourContext.onContentMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<MessageContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MessageContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onContact(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<ContactContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<ContactContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDice(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<DiceContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DiceContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGame(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<GameContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<GameContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onLocation(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<LocationContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<LocationContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPoll(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<PollContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<PollContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onText(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<TextContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVenue(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<VenueContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VenueContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onAudioMediaGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<AudioMediaGroupContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AudioMediaGroupContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDocumentMediaGroupContent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = true,
additionalFilter: CommonMessageFilter<DocumentMediaGroupContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DocumentMediaGroupContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMediaCollection(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false,
additionalFilter: (suspend (CommonMessage<MediaCollectionContent<TelegramMediaFile>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MediaCollectionContent<TelegramMediaFile>>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMedia(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = true,
additionalFilter: CommonMessageFilter<MediaContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MediaContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onAnimation(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<AnimationContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AnimationContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onAudio(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<AudioContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<AudioContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDocument(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<DocumentContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<DocumentContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPhoto(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<PhotoContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<PhotoContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onSticker(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<StickerContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<StickerContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVideo(
includeFilterByChatInBehaviourSubContext: Boolean = true,
includeMediaGroups: Boolean = false,
additionalFilter: CommonMessageFilter<VideoContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VideoContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVideoNote(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<VideoNoteContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VideoNoteContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoice(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<VoiceContent>? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<VoiceContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onInvoice(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<InvoiceContent>? = null,
performInParallel: Boolean = true,
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.tgbotapi.extensions.behaviour_builder.*
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.asChatEventMessage
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(
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (ChatEventMessage<T>) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<T>>
) = flowsUpdatesFilter.expectFlow(bot) {
it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.let { message ->
@ -25,7 +27,9 @@ internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
null
}
}.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerMessage ->
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
@ -35,105 +39,126 @@ internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
scenarioReceiver(triggerMessage)
}
}
)
suspend fun BehaviourContext.onChannelEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ChannelEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChannelEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onChatEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ChatEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChatEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatStartedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatStarted>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatStarted>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatEndedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatEnded>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatEnded>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVoiceChatParticipantsInvitedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<VoiceChatParticipantsInvited>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<VoiceChatParticipantsInvited>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onMessageAutoDeleteTimerChangedEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<MessageAutoDeleteTimerChanged>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<MessageAutoDeleteTimerChanged>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onCommonEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<CommonEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<CommonEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGroupEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<GroupEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<GroupEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onSupergroupEvent(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<SupergroupEvent>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<SupergroupEvent>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onChannelChatCreated(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ChannelChatCreated>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ChannelChatCreated>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDeleteChatPhoto(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<DeleteChatPhoto>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<DeleteChatPhoto>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onGroupChatCreated(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<GroupChatCreated>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<GroupChatCreated>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onLeftChatMember(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<LeftChatMember>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<LeftChatMember>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onNewChatMembers(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<NewChatMembers>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatMembers>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onNewChatPhoto(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<NewChatPhoto>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatPhoto>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onNewChatTitle(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<NewChatTitle>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<NewChatTitle>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPinnedMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<PinnedMessage>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<PinnedMessage>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onProximityAlertTriggered(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<ProximityAlertTriggered>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ChatEventMessage<ProximityAlertTriggered>>
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onSupergroupChatCreated(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (ChatEventMessage<SupergroupChatCreated>) -> Boolean)? = null,
performInParallel: Boolean = true,
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.tgbotapi.extensions.behaviour_builder.*
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.extensions.sourceChat
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(
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (T) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, T>
) = flowsUpdatesFilter.expectFlow(bot) {
it.asInlineQueryUpdate() ?.data ?.let { query ->
@ -19,7 +21,9 @@ internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQ
null
}
}.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerQuery ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerQuery ->
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId }
@ -31,24 +35,28 @@ internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQ
scenarioReceiver(triggerQuery)
}
}
)
suspend fun BehaviourContext.onAnyInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (InlineQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, InlineQuery>
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onBaseInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (BaseInlineQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, BaseInlineQuery>
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onLocationInlineQuery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (LocationInlineQuery) -> Boolean)? = null,
performInParallel: Boolean = true,
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.tgbotapi.extensions.behaviour_builder.*
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.extensions.sourceChat
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(
includeFilterByChatInBehaviourSubContext: Boolean = true,
noinline additionalFilter: (suspend (List<MediaGroupMessage<T>>) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<T>>>
) = flowsUpdatesFilter.expectFlow(bot) { update ->
update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup ->
@ -27,7 +29,9 @@ internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.bui
null
}
} ?: emptyList()
}.subscribeSafelyWithoutExceptions(scope) { mediaGroup ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { mediaGroup ->
val mediaGroupChat = mediaGroup.chat!!
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
@ -38,40 +42,48 @@ internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.bui
scenarioReceiver(mediaGroup)
}
}
)
suspend fun BehaviourContext.onMediaGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<MediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<MediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPlaylist(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<AudioMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<AudioMediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onDocumentsGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<DocumentMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<DocumentMediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVisualGallery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VisualMediaGroupContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVisualMediaGroup(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<VisualMediaGroupContent>>>
) = onVisualGallery(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = onVisualGallery(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onPhotoGallery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<PhotoContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, List<MediaGroupMessage<PhotoContent>>>
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, performInParallel, scenarioReceiver)
suspend fun BehaviourContext.onVideoGallery(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (List<MediaGroupMessage<VideoContent>>) -> Boolean)? = null,
performInParallel: Boolean = true,
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.tgbotapi.extensions.behaviour_builder.*
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.asPassportMessage
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.types.message.PassportMessage
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,
noinline additionalFilter: (suspend (PassportMessage) -> Boolean)? = null,
performInParallel: Boolean = true,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, PassportMessage>
) = flowsUpdatesFilter.expectFlow(bot) {
it.asMessageUpdate() ?.data ?.asPassportMessage() ?.let { message ->
@ -21,7 +23,9 @@ suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPas
null
}
}.let(::listOfNotNull)
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage ->
}.subscribeSafelyWithoutExceptions(
scope,
optionallyWrapWithLaunch(performInParallel) { triggerMessage ->
doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
@ -31,14 +35,17 @@ suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPas
scenarioReceiver(triggerMessage)
}
}
)
suspend fun BehaviourContext.onPassportMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: (suspend (PassportMessage) -> Boolean)? = null,
performInParallel: Boolean = true,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, PassportMessage>
) = onPassportMessageWith<EncryptedPassportElement>(
includeFilterByChatInBehaviourSubContext,
additionalFilter,
performInParallel,
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
}