# dev.inmo.tgbotapi.extensions.behaviour_builder

docs

# Package dev.inmo.tgbotapi.extensions.behaviour_builder

## Types

BehaviourContext
Link copied to clipboard
interface BehaviourContext : FlowsUpdatesFilter, RequestsExecutor, CoroutineScope
Content copied to clipboard

This class contains all necessary tools for work with bots and especially buildBehaviour

BehaviourContextAndTwoTypesReceiver
Link copied to clipboard
typealias BehaviourContextAndTwoTypesReceiver < T , I1 , I2 > = CustomBehaviourContextAndTwoTypesReceiver < BehaviourContext , T , I1 , I2 >
Content copied to clipboard
BehaviourContextAndTypeReceiver
Link copied to clipboard
typealias BehaviourContextAndTypeReceiver < T , I > = CustomBehaviourContextAndTypeReceiver < BehaviourContext , T , I >
Content copied to clipboard
BehaviourContextReceiver
Link copied to clipboard
typealias BehaviourContextReceiver < T > = CustomBehaviourContextReceiver < BehaviourContext , T >
Content copied to clipboard
BehaviourContextWithFSM
Link copied to clipboard
interface BehaviourContextWithFSM < T : State> : BehaviourContext, StatesMachine< T >
Content copied to clipboard

Interface which combine BehaviourContext and StatesMachine. Subcontext of triggers and states contexts must have one common flow of updates and must not lose updates between updates

BehaviourWithFSMStateHandler
Link copied to clipboard
fun interface BehaviourWithFSMStateHandler < I : O , O : State> : StatesHandler< I , O >
Content copied to clipboard
BehaviourWithFSMStateHandlerHolder
Link copied to clipboard
class BehaviourWithFSMStateHandlerHolder < I : O , O : State>( inputKlass: KClass < I >, strict: Boolean = false, delegateTo: BehaviourWithFSMStateHandler < I , O >) : CheckableHandlerHolder< O , O > , BehaviourWithFSMStateHandler < O , O >
Content copied to clipboard

Special holder for BehaviourContextWithFSM. This holder helps BehaviourContextWithFSM to understand whether it can handle input State with delegateTo or not

CustomBehaviourContextAndTwoTypesReceiver
Link copied to clipboard
typealias CustomBehaviourContextAndTwoTypesReceiver < BC , T , I1 , I2 > = suspend BC .( I1 , I2 ) -> T
Content copied to clipboard
CustomBehaviourContextAndTypeReceiver
Link copied to clipboard
typealias CustomBehaviourContextAndTypeReceiver < BC , T , I > = suspend BC .( I ) -> T
Content copied to clipboard
CustomBehaviourContextReceiver
Link copied to clipboard
typealias CustomBehaviourContextReceiver < BC , T > = suspend BC .() -> T
Content copied to clipboard
DefaultBehaviourContext
Link copied to clipboard
class DefaultBehaviourContext ( val bot: TelegramBot , val scope: CoroutineScope, broadcastChannelsSize: Int = 100, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, upstreamUpdatesFlow: Flow< Update >? = null, val triggersHolder: TriggersHolder = TriggersHolder(), updatesFilter: BehaviourContextAndTypeReceiver < Boolean , Update >? = null) : AbstractFlowsUpdatesFilter, RequestsExecutor, CoroutineScope, BehaviourContext
Content copied to clipboard
DefaultBehaviourContextWithFSM
Link copied to clipboard
class DefaultBehaviourContextWithFSM < T : State>( behaviourContext: BehaviourContext , statesManager: StatesManager< T >, handlers: List < BehaviourWithFSMStateHandlerHolder <*, T >>, onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler()) : BehaviourContext, BehaviourContextWithFSM < T >
Content copied to clipboard

Default realization of BehaviourContextWithFSM. It uses behaviourContext as a base for this object as BehaviourContext, but managing substates contexts updates for avoiding of updates lost between states

## Functions

asAction
Link copied to clipboard
inline fun < T > Deferred< T >. asAction (): DeferredAction< T , T >
Content copied to clipboard
BehaviourContext
Link copied to clipboard
fun BehaviourContext ( bot: TelegramBot , scope: CoroutineScope, flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter(), triggersHolder: TriggersHolder = TriggersHolder()): DefaultBehaviourContext
Content copied to clipboard
inline fun < T > BehaviourContext ( bot: TelegramBot , scope: CoroutineScope, flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter(), triggersHolder: TriggersHolder = TriggersHolder(), crossinline block: BehaviourContext .() -> T ): T
Content copied to clipboard
BehaviourContextAndTwoTypesReceiver
Link copied to clipboard
inline fun < T , I1 , I2 > BehaviourContextAndTwoTypesReceiver (noinline block: BehaviourContextAndTwoTypesReceiver < T , I1 , I2 >): suspend BehaviourContext .( I1 , I2 ) -> T
Content copied to clipboard
BehaviourContextAndTypeReceiver
Link copied to clipboard
inline fun < T , I > BehaviourContextAndTypeReceiver (noinline block: BehaviourContextAndTypeReceiver < T , I >): suspend BehaviourContext .( I ) -> T
Content copied to clipboard
BehaviourContextReceiver
Link copied to clipboard
inline fun < T > BehaviourContextReceiver (noinline block: BehaviourContextReceiver < T >): suspend BehaviourContext .() -> T
Content copied to clipboard
BehaviourWithFSMStateHandlerHolder
Link copied to clipboard
inline fun < I : O , O : State> BehaviourWithFSMStateHandlerHolder (strict: Boolean = false, delegateTo: BehaviourWithFSMStateHandler < I , O >): BehaviourWithFSMStateHandlerHolder < I , O >
Content copied to clipboard
buildBehaviour
Link copied to clipboard
suspend fun TelegramBot . buildBehaviour ( flowUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter(), scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler< Unit >? = null, block: BehaviourContextReceiver < Unit >): BehaviourContext
Content copied to clipboard

Use this method in case you wish to make some additional actions with flowUpdatesFilter.

buildBehaviourWithFSM
Link copied to clipboard
suspend fun < T : State> TelegramBot . buildBehaviourWithFSM ( flowUpdatesFilter: FlowsUpdatesFilter , scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler< Unit >? = null, statesManager: StatesManager< T > = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: List < BehaviourWithFSMStateHandlerHolder <*, T >> = listOf(), onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler(), block: CustomBehaviourContextReceiver < DefaultBehaviourContextWithFSM < T >, Unit >): DefaultBehaviourContextWithFSM < T >
Content copied to clipboard

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.

suspend fun < T : State> TelegramBot . buildBehaviourWithFSM ( upstreamUpdatesFlow: Flow< Update >? = null, scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler< Unit >? = null, statesManager: StatesManager< T > = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: List < BehaviourWithFSMStateHandlerHolder <*, T >> = listOf(), onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler(), block: CustomBehaviourContextReceiver < DefaultBehaviourContextWithFSM < T >, Unit >): DefaultBehaviourContextWithFSM < T >
Content copied to clipboard

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.

buildBehaviourWithFSMAndStartLongPolling
Link copied to clipboard
suspend fun < T : State> TelegramBot . buildBehaviourWithFSMAndStartLongPolling ( scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler< Unit >? = null, statesManager: StatesManager< T > = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: List < BehaviourWithFSMStateHandlerHolder <*, T >> = listOf(), onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler(), block: CustomBehaviourContextReceiver < DefaultBehaviourContextWithFSM < T >, Unit >): Job
Content copied to clipboard
suspend fun < T : State> TelegramBot . buildBehaviourWithFSMAndStartLongPolling ( upstreamUpdatesFlow: Flow< Update >? = null, scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler< Unit >? = null, statesManager: StatesManager< T > = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: List < BehaviourWithFSMStateHandlerHolder <*, T >> = listOf(), onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler(), block: CustomBehaviourContextReceiver < DefaultBehaviourContextWithFSM < T >, Unit >): Pair < DefaultBehaviourContextWithFSM < T >, Job>
Content copied to clipboard

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

buildBehaviourWithLongPolling
Link copied to clipboard
suspend fun TelegramBot . buildBehaviourWithLongPolling ( scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler< Unit >? = null, block: BehaviourContextReceiver < Unit >): Job
Content copied to clipboard

Use this method to build bot behaviour and run it via long polling. In case you wish to get FlowsUpdatesFilter for additional manipulations, you must provide external FlowsUpdatesFilter in other buildBehaviour function.

createSubContext
Link copied to clipboard
fun < BC : BehaviourContext > BC . createSubContext ( scope: CoroutineScope = LinkedSupervisorScope(), triggersHolder: TriggersHolder = this.triggersHolder, updatesUpstreamFlow: Flow< Update > = allUpdatesFlow, updatesFilter: CustomBehaviourContextAndTypeReceiver < BC , Boolean , Update >? = null): BC
Content copied to clipboard
createSubContextAndDoWithUpdatesFilter
Link copied to clipboard
suspend fun < T , BC : BehaviourContext > BC . createSubContextAndDoWithUpdatesFilter ( scope: CoroutineScope = LinkedSupervisorScope(), triggersHolder: TriggersHolder = this.triggersHolder, updatesUpstreamFlow: Flow< Update > = allUpdatesFlow, updatesFilter: CustomBehaviourContextAndTypeReceiver < BC , Boolean , Update >? = null, stopOnCompletion: Boolean = true, behaviourContextReceiver: CustomBehaviourContextReceiver < BC , T >): T
Content copied to clipboard

Creates new one BehaviourContext using createSubContext and launches behaviourContextReceiver in a new context using doInContext

CustomBehaviourContextReceiver
Link copied to clipboard
inline fun < BC , T > CustomBehaviourContextReceiver (noinline block: CustomBehaviourContextReceiver < BC , T >): suspend BC .() -> T
Content copied to clipboard
doInContext
Link copied to clipboard
suspend fun < T , BC : BehaviourContext > BC . doInContext (stopOnCompletion: Boolean = false, behaviourContextReceiver: CustomBehaviourContextReceiver < BC , T >): T
Content copied to clipboard

Launch behaviourContextReceiver in context of this as BehaviourContext and as kotlin.coroutines.CoroutineContext

oneOf
Link copied to clipboard
suspend fun < O > BehaviourContext . oneOf (vararg deferredActions: Deferred< O >): O
Content copied to clipboard
suspend fun < O > BehaviourContext . oneOf (deferredActions: Iterable <Deferred< O >>): O
Content copied to clipboard
oneOfActions
Link copied to clipboard
suspend fun < O > BehaviourContext . oneOfActions (vararg deferredActions: DeferredAction<*, O >): O
Content copied to clipboard
suspend fun < O > BehaviourContext . oneOfActions (deferredActions: Iterable <DeferredAction<*, O >>): O
Content copied to clipboard
onStateOrSubstate
Link copied to clipboard
inline fun < I : O , O : State> BehaviourContextWithFSM < O >. onStateOrSubstate (handler: BehaviourWithFSMStateHandler < I , O >)
Content copied to clipboard

Add NON STRICT handler to list of available in future BehaviourContextWithFSM. Non strict means that for input State will be used KClass.isInstance and any inheritor of kClass will pass this requirement

parallel
Link copied to clipboard
suspend fun < T > BehaviourContext . parallel (action: BehaviourContextReceiver < T >): Deferred< T >
Content copied to clipboard
stop
Link copied to clipboard
fun BehaviourContext . stop ()
Content copied to clipboard

This method will cancel ALL subsequent contexts, expectations and waiters

strictlyOn
Link copied to clipboard
inline fun < I : O , O : State> BehaviourContextWithFSM < O >. strictlyOn (handler: BehaviourWithFSMStateHandler < I , O >)
Content copied to clipboard

Add STRICT handler to list of available in future BehaviourContextWithFSM. Strict means that for input State will be used State::class == kClass and any State with exactly the same type will pass requirements

telegramBotWithBehaviour
Link copied to clipboard
suspend fun telegramBotWithBehaviour ( token: String , flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter(), scope: CoroutineScope? = null, apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder .() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler< Unit >? = null, testServer: Boolean = false, block: BehaviourContextReceiver < Unit >): TelegramBot
Content copied to clipboard

Create bot using telegramBot and start listening for updates using buildBehaviour. Use this method in case you wish to make some additional actions with flowsUpdatesFilter.

telegramBotWithBehaviourAndFSM
Link copied to clipboard
suspend fun < T : State> telegramBotWithBehaviourAndFSM ( token: String , flowsUpdatesFilter: FlowsUpdatesFilter , scope: CoroutineScope? = null, apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder .() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler< Unit >? = null, statesManager: StatesManager< T > = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: List < BehaviourWithFSMStateHandlerHolder <*, T >> = listOf(), testServer: Boolean = false, onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler(), block: CustomBehaviourContextReceiver < DefaultBehaviourContextWithFSM < T >, Unit >): TelegramBot
Content copied to clipboard

Create bot using telegramBot and start listening for updates using buildBehaviourWithFSM. Use this method in case you wish to make some additional actions with flowsUpdatesFilter.

telegramBotWithBehaviourAndFSMAndStartLongPolling
Link copied to clipboard
suspend fun < T : State> telegramBotWithBehaviourAndFSMAndStartLongPolling ( token: String , scope: CoroutineScope? = null, apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder .() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler< Unit >? = null, statesManager: StatesManager< T > = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: List < BehaviourWithFSMStateHandlerHolder <*, T >> = listOf(), testServer: Boolean = false, onStateHandlingErrorHandler: StateHandlingErrorHandler< T > = defaultStateHandlingErrorHandler(), block: CustomBehaviourContextReceiver < DefaultBehaviourContextWithFSM < T >, Unit >): Pair < TelegramBot , Job>
Content copied to clipboard

Create bot using telegramBot and start listening for updates using buildBehaviourWithFSMAndStartLongPolling. This method will launch updates retrieving via long polling inside of buildBehaviourWithFSMAndStartLongPolling

telegramBotWithBehaviourAndLongPolling
Link copied to clipboard
suspend fun telegramBotWithBehaviourAndLongPolling ( token: String , scope: CoroutineScope? = null, apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder .() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler< Unit >? = null, testServer: Boolean = false, block: BehaviourContextReceiver < Unit >): Pair < TelegramBot , Job>
Content copied to clipboard

Create bot using telegramBot and start listening for updates using buildBehaviourWithLongPolling. Use this method in case you wish to make some additional actions with flowsUpdatesFilter.

withAction
Link copied to clipboard
infix inline fun < T , O > Deferred< T >. withAction (noinline callback: suspend ( T ) -> O ): DeferredAction< T , O >
Content copied to clipboard

## Properties

defaultCoroutineScopeProvider
Link copied to clipboard
expect var defaultCoroutineScopeProvider : () -> CoroutineScope
Content copied to clipboard

This function is used in buildBehaviour extensions to provide default CoroutineScope and allow to avoid all unnecessary parameters except of block

actual var defaultCoroutineScopeProvider : () -> CoroutineScope
Content copied to clipboard
actual var defaultCoroutineScopeProvider : () -> CoroutineScope
Content copied to clipboard