1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

defaultExceptionsHandler in behaviour builder

This commit is contained in:
InsanusMokrassar 2021-06-26 17:05:34 +06:00
parent b9de002517
commit ee8cc2aa46
4 changed files with 21 additions and 3 deletions

View File

@ -10,6 +10,7 @@
* New extensions `telegramBotWithBehaviour` * New extensions `telegramBotWithBehaviour`
* All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter * All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter
`performInParallel`, by default `true`) `performInParallel`, by default `true`)
* All behaviour builder extensions got new parameter `defaultExceptionsHandler`
* `API`: * `API`:
* All `reply` and subsequent extension have been replaced in send package * All `reply` and subsequent extension have been replaced in send package
* `Bot API 5.3`: * `Bot API 5.3`:

View File

@ -1,11 +1,14 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder 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.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import dev.inmo.tgbotapi.utils.PreviewFeature import dev.inmo.tgbotapi.utils.PreviewFeature
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.plus
/** /**
* Use this method in case you wish to make some additional actions with [flowUpdatesFilter]. * 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( suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope, scope: CoroutineScope,
flowUpdatesFilter: FlowsUpdatesFilter, flowUpdatesFilter: FlowsUpdatesFilter,
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit> block: BehaviourContextReceiver<Unit>
) { ) {
BehaviourContext( BehaviourContext(
this, this,
scope, scope.let {
if (defaultExceptionsHandler == null) {
it
} else {
it + ContextSafelyExceptionHandler(defaultExceptionsHandler)
}
},
flowUpdatesFilter flowUpdatesFilter
).block() ).block()
} }
@ -40,11 +50,13 @@ suspend fun TelegramBot.buildBehaviour(
@PreviewFeature @PreviewFeature
suspend fun TelegramBot.buildBehaviour( suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope, scope: CoroutineScope,
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit> block: BehaviourContextReceiver<Unit>
) = FlowsUpdatesFilter().let { ) = FlowsUpdatesFilter().let {
buildBehaviour( buildBehaviour(
scope, scope,
it, it,
defaultExceptionsHandler,
block block
) )
longPolling( longPolling(

View File

@ -60,12 +60,12 @@ suspend fun <T> BehaviourContext.doInSubContextWithUpdatesFilter(
newFlowsUpdatesFilterSetUp = updatesFilter ?.let { newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
{ oldOne -> { oldOne ->
weakLaunch { weakLaunch {
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, block = asUpdateReceiver)
} }
} }
} ?: { oldOne -> } ?: { oldOne ->
weakLaunch { weakLaunch {
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, block = asUpdateReceiver)
} }
}, },
stopOnCompletion, stopOnCompletion,

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder 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.KtorRequestsExecutorBuilder
import dev.inmo.tgbotapi.bot.Ktor.telegramBot import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
@ -28,6 +29,7 @@ suspend fun telegramBotWithBehaviour(
scope: CoroutineScope? = null, scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl, apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {}, builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit> block: BehaviourContextReceiver<Unit>
): TelegramBot = telegramBot( ): TelegramBot = telegramBot(
token, token,
@ -37,6 +39,7 @@ suspend fun telegramBotWithBehaviour(
buildBehaviour( buildBehaviour(
scope ?: CoroutineScope(coroutineContext), scope ?: CoroutineScope(coroutineContext),
flowsUpdatesFilter, flowsUpdatesFilter,
defaultExceptionsHandler,
block block
) )
} }
@ -60,6 +63,7 @@ suspend fun telegramBotWithBehaviour(
scope: CoroutineScope? = null, scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl, apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {}, builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit> block: BehaviourContextReceiver<Unit>
): Pair<TelegramBot, Job> { ): Pair<TelegramBot, Job> {
return telegramBot( return telegramBot(
@ -69,6 +73,7 @@ suspend fun telegramBotWithBehaviour(
).let { ).let {
it to it.buildBehaviour( it to it.buildBehaviour(
scope ?: CoroutineScope(coroutineContext), scope ?: CoroutineScope(coroutineContext),
defaultExceptionsHandler,
block block
) )
} }