diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/Base.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/Base.kt index 0b0eb2c008..8ad99d900e 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/Base.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/Base.kt @@ -38,7 +38,9 @@ suspend fun FlowsUpdatesFilter.expectFlow( filter: suspend (Update) -> List ): Flow { val flow = allUpdatesFlow.map { - val result = safelyWithResult { filter(it) } + val result = runCatching { + filter(it) + } if (result.isFailure || result.getOrThrow().isEmpty()) { if (cancelTrigger(it)) { cancelRequestFactory(it) ?.also { @@ -47,14 +49,20 @@ suspend fun FlowsUpdatesFilter.expectFlow( } } errorFactory(it) ?.also { errorRequest -> - safelyWithoutExceptions { bot.execute(errorRequest) } + runCatching { + bot.execute(errorRequest) + } } emptyList() } else { result.getOrThrow() } }.flatten() - initRequest ?.also { safelyWithoutExceptions { bot.execute(initRequest) } } + initRequest ?.also { + runCatching { + bot.execute(initRequest) + } + } return flow } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt index 17745ea21b..9af75800eb 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt @@ -9,6 +9,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.whenCommonMessage +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull import dev.inmo.tgbotapi.types.files.TelegramMediaFile import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.* @@ -27,7 +28,7 @@ internal suspend inline fun is BaseSentMessageUpdate -> it.data.whenCommonMessage(::listOfNotNull) else -> null } ?.mapNotNull { message -> - if (message.content is T) message as CommonMessage else null + message.withContentOrNull() } } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt index 5103e4526c..1492cacaa1 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -9,12 +9,12 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.types.update.abstracts.Update -internal suspend inline fun BC.on( +internal suspend fun BC.on( markerFactory: MarkerFactory?, initialFilter: SimpleFilter? = null, - noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver? = null, - noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver, - noinline updateToData: (Update) -> List? + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver? = null, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver, + updateToData: (Update) -> List? ) = flowsUpdatesFilter.expectFlow( bot, filter = initialFilter ?.let { @@ -24,17 +24,21 @@ internal suspend inline fun BC.on( } ?: emptyList() } } ?: { - updateToData(it) ?.mapNotNull { data -> + updateToData(it) ?.map { data -> it to data } ?: emptyList() } ).run { - val handler: suspend (Pair) -> Unit = { (update, triggerData) -> - createSubContextAndDoWithUpdatesFilter { - if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) { - scenarioReceiver(triggerData) + val handler: suspend (Pair) -> Unit = subcontextUpdatesFilter ?.let { + { (update, triggerData) -> + createSubContextAndDoWithUpdatesFilter { + if (subcontextUpdatesFilter(this, triggerData, update)) { + scenarioReceiver(triggerData) + } } } + } ?: { (_, triggerData) -> + createSubContextAndDoWithUpdatesFilter(behaviourContextReceiver = { scenarioReceiver(triggerData) }) } markerFactory ?.let { subscribeSafelyWithoutExceptionsAsync(