tgbotapi/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuil...

156 lines
7.2 KiB
Kotlin
Raw Normal View History

2021-10-13 08:22:01 +00:00
package dev.inmo.tgbotapi.extensions.behaviour_builder
2021-10-15 11:13:03 +00:00
import dev.inmo.micro_utils.coroutines.*
2021-10-13 08:22:01 +00:00
import dev.inmo.micro_utils.fsm.common.*
import dev.inmo.micro_utils.fsm.common.managers.DefaultStatesManager
import dev.inmo.micro_utils.fsm.common.managers.InMemoryDefaultStatesManagerRepo
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
2021-10-13 08:22:01 +00:00
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
2023-02-06 07:28:38 +00:00
import dev.inmo.tgbotapi.types.Seconds
2021-10-13 08:22:01 +00:00
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import kotlinx.coroutines.*
2021-10-13 08:22:01 +00:00
import kotlinx.coroutines.flow.Flow
/**
2021-10-15 11:13:03 +00:00
* Creates [BehaviourContextWithFSM] via creating of [DefaultBehaviourContext] with [this] as [TelegramBot],
* [scope] as target scope for that [DefaultBehaviourContext] and [upstreamUpdatesFlow]. Pass [statesManager]
* to customize some internal logic of states changes. Pass [presetHandlers] in case you have some list of
* [BehaviourWithFSMStateHandlerHolder] with presets.
2021-10-13 08:22:01 +00:00
*
2021-10-15 11:13:03 +00:00
* !!! WARNING !!! This method WILL NOT call [BehaviourContextWithFSM.start] of result object and WILL NOT
* start any updates retrieving. See [buildBehaviourWithFSMAndStartLongPolling] or
* [telegramBotWithBehaviourAndFSMAndStartLongPolling] in case you wish to start [longPolling] automatically
2021-10-13 08:22:01 +00:00
*/
2021-11-06 15:21:49 +00:00
suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
2021-10-13 08:22:01 +00:00
upstreamUpdatesFlow: Flow<Update>? = null,
scope: CoroutineScope = defaultCoroutineScopeProvider(),
2021-10-15 11:13:03 +00:00
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2021-11-06 15:21:49 +00:00
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
2022-05-18 10:45:08 +00:00
onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler(),
2022-05-03 07:58:13 +00:00
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
2021-10-15 11:13:03 +00:00
DefaultBehaviourContext(
this,
defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope,
upstreamUpdatesFlow = upstreamUpdatesFlow
),
presetHandlers,
2022-05-18 10:45:08 +00:00
statesManager,
onStateHandlingErrorHandler
).apply { block() }
2021-10-13 08:22:01 +00:00
/**
2021-10-15 11:13:03 +00:00
* Use [buildBehaviourWithFSM] to create [BehaviourContextWithFSM] and launch getting of updates
* using [longPolling]. For [longPolling] will be used result [BehaviourContextWithFSM] for both parameters
* flowsUpdatesFilter and scope
2021-10-13 08:22:01 +00:00
*/
2021-11-06 15:21:49 +00:00
suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
2021-10-13 08:22:01 +00:00
upstreamUpdatesFlow: Flow<Update>? = null,
scope: CoroutineScope = defaultCoroutineScopeProvider(),
2021-10-15 11:13:03 +00:00
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2021-11-06 15:21:49 +00:00
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
2022-05-18 10:45:08 +00:00
onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler(),
2023-02-06 07:28:38 +00:00
timeoutSeconds: Seconds = 30,
autoDisableWebhooks: Boolean = true,
autoSkipTimeoutExceptions: Boolean = true,
2022-05-03 07:58:13 +00:00
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): Pair<DefaultBehaviourContextWithFSM<T>, Job> = buildBehaviourWithFSM(
2021-10-15 11:13:03 +00:00
upstreamUpdatesFlow,
scope,
defaultExceptionsHandler,
statesManager,
presetHandlers,
2022-05-18 10:45:08 +00:00
onStateHandlingErrorHandler,
2021-10-15 11:13:03 +00:00
block
).run {
2021-10-13 08:22:01 +00:00
this to scope.launch {
start()
2023-02-06 07:28:38 +00:00
longPolling(flowsUpdatesFilter, timeoutSeconds, scope, autoDisableWebhooks, autoSkipTimeoutExceptions, defaultExceptionsHandler)
}
}
/**
2021-10-15 11:13:03 +00:00
* Creates [BehaviourContextWithFSM] via creating of [DefaultBehaviourContext] with [this] as [TelegramBot],
* [scope] as target scope for that [DefaultBehaviourContext] and [FlowsUpdatesFilter.allUpdatesFlow] of
* [flowUpdatesFilter] as [DefaultBehaviourContext.upstreamUpdatesFlow]. Pass [statesManager]
* to customize some internal logic of states changes. Pass [presetHandlers] in case you have some list of
* [BehaviourWithFSMStateHandlerHolder] with presets.
* Use this method in case you wish to make some additional actions with [flowUpdatesFilter].
*
2021-10-15 11:13:03 +00:00
* !!! WARNING !!! This method WILL NOT call [BehaviourContextWithFSM.start] of result object and WILL NOT
* start any updates retrieving. See [buildBehaviourWithFSMAndStartLongPolling] or
* [telegramBotWithBehaviourAndFSMAndStartLongPolling] in case you wish to start [longPolling] automatically
*
* @see BehaviourContext
* @see BehaviourContextWithFSM
* @see longPolling
* @see BehaviourContextWithFSM.strictlyOn
* @see BehaviourContextWithFSM.onStateOrSubstate
*/
2021-11-06 15:21:49 +00:00
suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
flowUpdatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2021-11-06 15:21:49 +00:00
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
2022-05-18 10:45:08 +00:00
onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler(),
2022-05-03 07:58:13 +00:00
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
2021-10-15 11:13:03 +00:00
DefaultBehaviourContext(
this,
2021-10-15 11:13:03 +00:00
defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope,
upstreamUpdatesFlow = flowUpdatesFilter.allUpdatesFlow
),
presetHandlers,
statesManager,
onStateHandlingErrorHandler
).apply { block() }
/**
2021-10-15 11:13:03 +00:00
* Use [buildBehaviourWithFSM] to create [BehaviourContextWithFSM] and launch getting of updates
* using [longPolling]. For [longPolling] will be used result [BehaviourContextWithFSM] for both parameters
* flowsUpdatesFilter and scope
*
2021-10-15 11:13:03 +00:00
* @see buildBehaviourWithFSMAndStartLongPolling
* @see BehaviourContext
* @see longPolling
* @see BehaviourContextWithFSM.strictlyOn
* @see BehaviourContextWithFSM.onStateOrSubstate
*/
2021-11-06 15:21:49 +00:00
suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2021-11-06 15:21:49 +00:00
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler(),
2023-02-06 07:28:38 +00:00
timeoutSeconds: Seconds = 30,
autoDisableWebhooks: Boolean = true,
autoSkipTimeoutExceptions: Boolean = true,
2022-05-03 07:58:13 +00:00
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
) = FlowsUpdatesFilter().let {
buildBehaviourWithFSM(
it,
scope,
defaultExceptionsHandler,
statesManager,
presetHandlers,
onStateHandlingErrorHandler,
block
).run {
start()
longPolling(
flowsUpdatesFilter,
2023-02-06 07:28:38 +00:00
timeoutSeconds,
scope,
autoDisableWebhooks,
autoSkipTimeoutExceptions,
defaultExceptionsHandler
)
2021-10-13 08:22:01 +00:00
}
}