tgbotapi/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt

115 lines
4.6 KiB
Kotlin
Raw Normal View History

package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.ExceptionHandler
2021-11-06 15:21:49 +00:00
import dev.inmo.micro_utils.fsm.common.State
import dev.inmo.micro_utils.fsm.common.StatesManager
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
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder
import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
2023-02-06 07:28:38 +00:00
import dev.inmo.tgbotapi.types.Seconds
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlin.coroutines.coroutineContext
/**
2021-10-15 11:13:03 +00:00
* 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].
*
* **WARNING** This method WILL NOT launch any listening of updates. Use something like
* [startGettingOfUpdatesByLongPolling] or tools for work with webhooks
*
* @return Created bot which has been used to create [BehaviourContext] via [buildBehaviourWithFSM]
*
* @see [BehaviourContext]
* @see [buildBehaviourWithFSM]
* @see startGettingOfUpdatesByLongPolling
*/
2021-11-06 15:21:49 +00:00
suspend fun <T : State> telegramBotWithBehaviourAndFSM(
token: String,
flowsUpdatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2021-11-06 15:21:49 +00:00
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
2022-05-02 07:21:43 +00:00
testServer: Boolean = false,
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>
): TelegramBot = telegramBot(
token,
apiUrl,
2022-05-02 07:21:43 +00:00
testServer,
builder
).apply {
2021-10-15 11:13:03 +00:00
buildBehaviourWithFSMAndStartLongPolling(
flowsUpdatesFilter.allUpdatesFlow,
scope ?: CoroutineScope(coroutineContext),
defaultExceptionsHandler,
statesManager,
presetHandlers,
onStateHandlingErrorHandler,
2023-02-06 07:28:38 +00:00
timeoutSeconds,
autoDisableWebhooks,
autoSkipTimeoutExceptions,
block
)
}
/**
* Create bot using [telegramBot] and start listening for updates using [buildBehaviourWithFSMAndStartLongPolling]. This
* method will launch updates retrieving via long polling inside of [buildBehaviourWithFSMAndStartLongPolling]
*
* @return Pair of [TelegramBot] and [Job]. This [Job] can be used to stop listening updates in your [block] you passed
* here
*
* @see BehaviourContext
* @see buildBehaviourWithFSMAndStartLongPolling
* @see startGettingOfUpdatesByLongPolling
*/
2021-11-06 15:21:49 +00:00
suspend fun <T : State> telegramBotWithBehaviourAndFSMAndStartLongPolling(
token: String,
scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2021-11-06 15:21:49 +00:00
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
2022-05-02 07:21:43 +00:00
testServer: Boolean = false,
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<TelegramBot, Job> {
return telegramBot(
token,
apiUrl,
2022-05-02 07:21:43 +00:00
testServer,
builder
).let {
2021-10-15 11:13:03 +00:00
it to it.buildBehaviourWithFSMAndStartLongPolling (
scope ?: CoroutineScope(coroutineContext),
defaultExceptionsHandler,
statesManager,
presetHandlers,
onStateHandlingErrorHandler,
2023-02-06 07:28:38 +00:00
timeoutSeconds,
autoDisableWebhooks,
autoSkipTimeoutExceptions,
block
)
}
}