mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-11-21 23:23:47 +00:00
complete with including of FSM
This commit is contained in:
parent
6c2cfbf06f
commit
ec2852beb0
@ -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<Json>().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<State>.setupBotPlugin(koin: Koin) {
|
||||
val toPrint = koin.getOrNull<HelloPluginConfig>() ?.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<StatesManager<State>>() ?: DefaultStatesManager(
|
||||
InMemoryDefaultStatesManagerRepo<State>()
|
||||
koinApp.koin.getOrNull<DefaultStatesManagerRepo<State>>() ?: InMemoryDefaultStatesManagerRepo<State>(),
|
||||
onStartContextsConflictResolver = { _, _ -> false }
|
||||
),
|
||||
onStateHandlingErrorHandler = koinApp.koin.getOrNull<StateHandlingErrorHandler<State>>() ?: { state, e ->
|
||||
logger.eS(e) { "Unable to handle state $state" }
|
||||
|
Loading…
Reference in New Issue
Block a user