From ec2852beb0ee045356e42875da44f8c2748fb84b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 18 Aug 2022 12:42:34 +0600 Subject: [PATCH] complete with including of FSM --- .../kotlin/dev/inmo/plagubot/HelloPlugin.kt | 29 +++++++++++++++++-- .../main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 6 ++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt index 2f5a481..085722e 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt @@ -5,10 +5,14 @@ import dev.inmo.micro_utils.fsm.common.State import dev.inmo.plagubot.HelloPlugin.setupBotPlugin import dev.inmo.tgbotapi.extensions.api.bot.getMe import dev.inmo.tgbotapi.extensions.api.send.reply -import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext -import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextWithFSM +import dev.inmo.tgbotapi.extensions.api.send.sendMessage +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitText +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitTextMessage import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUnhandledCommand +import dev.inmo.tgbotapi.types.ChatId +import kotlinx.coroutines.flow.first import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json @@ -28,15 +32,34 @@ object HelloPlugin : Plugin { override fun Module.setupDI(database: Database, params: JsonObject) { single { get().decodeFromJsonElement(HelloPluginConfig.serializer(), params["helloPlugin"] ?: return@single null) + + } } + private sealed interface InternalFSMState : State { + override val context: ChatId + data class DidntSaidHello(override val context: ChatId) : InternalFSMState + data class SaidHelloOnce(override val context: ChatId) : InternalFSMState + } + override suspend fun BehaviourContextWithFSM.setupBotPlugin(koin: Koin) { val toPrint = koin.getOrNull() ?.print ?: "Hello :)" logger.d { toPrint } logger.dS { getMe().toString() } onCommand("hello_world") { - reply(it, toPrint) + startChain(InternalFSMState.DidntSaidHello(it.chat.id)) + } + + strictlyOn { state: InternalFSMState.DidntSaidHello -> + sendMessage(state.context, toPrint) + InternalFSMState.SaidHelloOnce(state.context) + } + + strictlyOn { state: InternalFSMState.SaidHelloOnce -> + val message = waitTextMessage().first() + reply(message, "Sorry, I can answer only this: $toPrint") + InternalFSMState.SaidHelloOnce(state.context) } } } diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt index 8fcdb1f..756aac8 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -5,8 +5,7 @@ import dev.inmo.micro_utils.common.Warning import dev.inmo.micro_utils.coroutines.runCatchingSafely 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.managers.* import dev.inmo.plagubot.config.* import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.extensions.api.webhook.deleteWebhook @@ -102,7 +101,8 @@ data class PlaguBot( logger.e("Something went wrong", it) }, statesManager = koinApp.koin.getOrNull>() ?: DefaultStatesManager( - InMemoryDefaultStatesManagerRepo() + koinApp.koin.getOrNull>() ?: InMemoryDefaultStatesManagerRepo(), + onStartContextsConflictResolver = { _, _ -> false } ), onStateHandlingErrorHandler = koinApp.koin.getOrNull>() ?: { state, e -> logger.eS(e) { "Unable to handle state $state" }