mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
defaultExceptionsHandler in behaviour builder
This commit is contained in:
parent
b9de002517
commit
ee8cc2aa46
@ -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`:
|
||||
|
@ -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<Unit>? = null,
|
||||
block: BehaviourContextReceiver<Unit>
|
||||
) {
|
||||
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<Unit>? = null,
|
||||
block: BehaviourContextReceiver<Unit>
|
||||
) = FlowsUpdatesFilter().let {
|
||||
buildBehaviour(
|
||||
scope,
|
||||
it,
|
||||
defaultExceptionsHandler,
|
||||
block
|
||||
)
|
||||
longPolling(
|
||||
|
@ -60,12 +60,12 @@ suspend fun <T> 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,
|
||||
|
@ -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<Unit>? = null,
|
||||
block: BehaviourContextReceiver<Unit>
|
||||
): 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<Unit>? = null,
|
||||
block: BehaviourContextReceiver<Unit>
|
||||
): Pair<TelegramBot, Job> {
|
||||
return telegramBot(
|
||||
@ -69,6 +73,7 @@ suspend fun telegramBotWithBehaviour(
|
||||
).let {
|
||||
it to it.buildBehaviour(
|
||||
scope ?: CoroutineScope(coroutineContext),
|
||||
defaultExceptionsHandler,
|
||||
block
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user