Skip to content

Commit

Permalink
Simplified HelloWorldScenario.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvoloshin committed Mar 9, 2021
1 parent a4b6d88 commit 54e049b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 54e049b

Please sign in to comment.