From 54e049b24c0f82d19bbf2fc7bf189119cf5e61a4 Mon Sep 17 00:00:00 2001 From: Nikita Voloshin Date: Tue, 9 Mar 2021 21:36:14 +0300 Subject: [PATCH] Simplified HelloWorldScenario.kt --- .../examples/helloworld/HelloWorldScenario.kt | 146 +++++++++--------- .../examples/helloworld/HelperScenario.kt | 49 +++--- 2 files changed, 92 insertions(+), 103 deletions(-) diff --git a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt index 6a682c9f..bc721cca 100644 --- a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt +++ b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt @@ -11,109 +11,105 @@ import com.justai.jaicf.channel.alexa.model.AlexaIntent import com.justai.jaicf.channel.facebook.facebook import com.justai.jaicf.channel.googleactions.dialogflow.DialogflowIntent import com.justai.jaicf.channel.telegram.telegram -import com.justai.jaicf.model.scenario.Scenario -import com.justai.jaicf.model.scenario.getValue import com.justai.jaicf.reactions.buttons -object HelloWorldScenario : Scenario { - override val scenario by Scenario { +val HelloWorldScenario = Scenario { - append(HelperScenario) + append(context = "helper", HelperScenario) - state("main") { + state("main") { - activators { - catchAll() - event(AlexaEvent.LAUNCH) - event(DialogflowIntent.WELCOME) - event(AimyboxEvent.START) - } - - action { - var name = context.client["name"] - - if (name == null) { - telegram { - name = request.message.chat.firstName ?: request.message.chat.username - } - facebook { - name = reactions.queryUserProfile()?.firstName() - } - } + activators { + catchAll() + event(AlexaEvent.LAUNCH) + event(DialogflowIntent.WELCOME) + event(AimyboxEvent.START) + } - if (name == null) { - reactions.askForName("Hello! What is your name?", "name") - } else { - reactions.run { - image("https://www.bluecross.org.uk/sites/default/files/d8/assets/images/118809lprLR.jpg") - sayRandom("Hello $name!", "Hi $name!", "Glad to hear you $name!") - buttons("Mew" to "/mew", "Wake up" to "wakeup") - aimybox?.endConversation() - } - } - } + action { + var name = context.client["name"] - state("inner", modal = true) { - activators { - catchAll() + if (name == null) { + telegram { + name = request.message.chat.firstName ?: request.message.chat.username } - - action { - reactions.apply { - sayRandom("What?", "Sorry, I didn't get that.") - say("Could you repeat please?") - changeState("/") - } + facebook { + name = reactions.queryUserProfile()?.firstName() } } - state("name") { - action { - context.client["name"] = context.result - reactions.say("Nice to meet you ${context.result}") + if (name == null) { + reactions.askForName("Hello! What is your name?", "name") + } else { + reactions.run { + image("https://www.bluecross.org.uk/sites/default/files/d8/assets/images/118809lprLR.jpg") + sayRandom("Hello $name!", "Hi $name!", "Glad to hear you $name!") + buttons("Mew" to "/mew", "Wake up" to "wakeup") + aimybox?.endConversation() } } } - state("stop") { + state("inner", modal = true) { activators { - intent(AlexaIntent.STOP) + catchAll() } - action(alexa.intent) { - reactions.endSession("See you latter! Bye bye!") + action { + reactions.apply { + sayRandom("What?", "Sorry, I didn't get that.") + say("Could you repeat please?") + changeState("/") + } } } - state("mew") { - activators { - regex("mew") - } - + state("name") { action { - reactions.image("https://www.bluecross.org.uk/sites/default/files/d8/assets/images/118809lprLR.jpg") + context.client["name"] = context.result + reactions.say("Nice to meet you ${context.result}") } } + } - state("wakeup") { - activators { - intent("wake_up") - } + state("stop") { + activators { + intent(AlexaIntent.STOP) + } - action(dialogflow) { - val dt = activator.slots["date-time"] - reactions.say("Okay! I'll wake you up ${dt?.stringValue}") - } + action(alexa.intent) { + reactions.endSession("See you latter! Bye bye!") } + } - state("cancel") { - activators { - intent("cancel") - } - action { - reactions.say("Okay, canceling.") - } + state("mew") { + activators { + regex("mew") + } + + action { + reactions.image("https://www.bluecross.org.uk/sites/default/files/d8/assets/images/118809lprLR.jpg") + } + } + + state("wakeup") { + activators { + intent("wake_up") + } + + action(dialogflow) { + val dt = activator.slots["date-time"] + reactions.say("Okay! I'll wake you up ${dt?.stringValue}") + } + } + + state("cancel") { + activators { + intent("cancel") + } + action { + reactions.say("Okay, canceling.") } } -} \ No newline at end of file +} diff --git a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelperScenario.kt b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelperScenario.kt index 42896c25..eb77303e 100644 --- a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelperScenario.kt +++ b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelperScenario.kt @@ -4,42 +4,35 @@ import com.justai.jaicf.activator.catchall.catchAll import com.justai.jaicf.activator.dialogflow.dialogflow import com.justai.jaicf.builder.Scenario import com.justai.jaicf.channel.alexa.activator.alexaIntent -import com.justai.jaicf.model.scenario.Scenario -import com.justai.jaicf.model.scenario.getValue import com.justai.jaicf.reactions.Reactions -object HelperScenario : Scenario { +val HelperScenario = Scenario { - override val scenario by Scenario { - - state("helper") { - state("ask4name") { - activators { - catchAll() - intent("name") - } + state("ask4name") { + activators { + catchAll() + intent("name") + } - action { - var name: String? = null + action { + var name: String? = null - activator.dialogflow?.run { - name = slots["name"]?.stringValue - } + activator.dialogflow?.run { + name = slots["name"]?.stringValue + } - activator.alexaIntent?.run { - name = slots["firstName"]?.value - } + activator.alexaIntent?.run { + name = slots["firstName"]?.value + } - activator.catchAll?.run { - name = request.input - } + activator.catchAll?.run { + name = request.input + } - if (name.isNullOrBlank()) { - reactions.say("Sorry, I didn't get it. Could you repeat please?") - } else { - reactions.goBack(name) - } - } + if (name.isNullOrBlank()) { + reactions.say("Sorry, I didn't get it. Could you repeat please?") + } else { + reactions.goBack(name) } } }