diff --git a/CHANGELOG.md b/CHANGELOG.md index 74a667ead1..a5313b3487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * New extensions `telegramBotWithBehaviour` * All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter `performInParallel`, by default `true`) + * All behaviour builder extensions got new parameter `defaultExceptionsHandler` * `API`: * All `reply` and subsequent extension have been replaced in send package * `Bot API 5.3`: diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt index cd14764a51..e4473c5431 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt @@ -1,11 +1,14 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder +import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler +import dev.inmo.micro_utils.coroutines.ExceptionHandler import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.utils.PreviewFeature import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.plus /** * Use this method in case you wish to make some additional actions with [flowUpdatesFilter]. @@ -20,11 +23,18 @@ import kotlinx.coroutines.CoroutineScope suspend fun TelegramBot.buildBehaviour( scope: CoroutineScope, flowUpdatesFilter: FlowsUpdatesFilter, + defaultExceptionsHandler: ExceptionHandler? = null, block: BehaviourContextReceiver ) { BehaviourContext( this, - scope, + scope.let { + if (defaultExceptionsHandler == null) { + it + } else { + it + ContextSafelyExceptionHandler(defaultExceptionsHandler) + } + }, flowUpdatesFilter ).block() } @@ -40,11 +50,13 @@ suspend fun TelegramBot.buildBehaviour( @PreviewFeature suspend fun TelegramBot.buildBehaviour( scope: CoroutineScope, + defaultExceptionsHandler: ExceptionHandler? = null, block: BehaviourContextReceiver ) = FlowsUpdatesFilter().let { buildBehaviour( scope, it, + defaultExceptionsHandler, block ) longPolling( diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index f4d91975fb..6ee5b6821e 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -60,12 +60,12 @@ suspend fun BehaviourContext.doInSubContextWithUpdatesFilter( newFlowsUpdatesFilterSetUp = updatesFilter ?.let { { oldOne -> weakLaunch { - oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) + oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, block = asUpdateReceiver) } } } ?: { oldOne -> weakLaunch { - oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) + oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, block = asUpdateReceiver) } }, stopOnCompletion, diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt index aec72211c6..eb62df598a 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder +import dev.inmo.micro_utils.coroutines.ExceptionHandler import dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutorBuilder import dev.inmo.tgbotapi.bot.Ktor.telegramBot import dev.inmo.tgbotapi.bot.TelegramBot @@ -28,6 +29,7 @@ suspend fun telegramBotWithBehaviour( scope: CoroutineScope? = null, apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder.() -> Unit = {}, + defaultExceptionsHandler: ExceptionHandler? = null, block: BehaviourContextReceiver ): TelegramBot = telegramBot( token, @@ -37,6 +39,7 @@ suspend fun telegramBotWithBehaviour( buildBehaviour( scope ?: CoroutineScope(coroutineContext), flowsUpdatesFilter, + defaultExceptionsHandler, block ) } @@ -60,6 +63,7 @@ suspend fun telegramBotWithBehaviour( scope: CoroutineScope? = null, apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder.() -> Unit = {}, + defaultExceptionsHandler: ExceptionHandler? = null, block: BehaviourContextReceiver ): Pair { return telegramBot( @@ -69,6 +73,7 @@ suspend fun telegramBotWithBehaviour( ).let { it to it.buildBehaviour( scope ?: CoroutineScope(coroutineContext), + defaultExceptionsHandler, block ) }