From 33292435a5cf9e02ef0f8a134bf6c49faa27b783 Mon Sep 17 00:00:00 2001 From: Nikita Voloshin Date: Tue, 2 Feb 2021 17:41:13 +0300 Subject: [PATCH] Rewrite examples --- .../gameclock/scenario/GameLoopScenario.kt | 152 +++++++++--------- .../scenario/GamersColorsScenario.kt | 12 +- .../gameclock/scenario/GamersCountScenario.kt | 12 +- .../gameclock/scenario/MainScenario.kt | 20 ++- .../jaicptelephony/TelephonyBotScenario.kt | 25 ++- 5 files changed, 115 insertions(+), 106 deletions(-) diff --git a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GameLoopScenario.kt b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GameLoopScenario.kt index fc0f5515..3a790b6f 100644 --- a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GameLoopScenario.kt +++ b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GameLoopScenario.kt @@ -14,11 +14,12 @@ import com.justai.jaicf.channel.googleactions.ActionsReactions import com.justai.jaicf.channel.googleactions.actions import com.justai.jaicf.examples.gameclock.GameController import com.justai.jaicf.examples.gameclock.model.colorLink +import com.justai.jaicf.examples.gameclock.scenario.GameLoopScenario.playStream import com.justai.jaicf.helpers.ssml.* import com.justai.jaicf.model.scenario.Scenario import kotlin.math.floor -object GameLoopScenario: Scenario { +object GameLoopScenario : Scenario { private const val AUDIO_URL = "https://bitbucket.org/just-ai/examples/downloads/game-timer-1.mp3" private const val STARTED_AT = "started_at" @@ -34,25 +35,24 @@ object GameLoopScenario: Scenario { game.restart() game.nextGamer() - reactions.run { - say( - "Okay! We are ready to start the game! ${breakMs(300)} " + - "${game.currentColor()} player! You're the first! " - ) + reactions.say( + "Okay! We are ready to start the game! ${breakMs(300)} " + + "${game.currentColor()} player! You're the first! " + ) - actions?.playStream(game.currentColor()) + actions { + reactions.playStream(game.currentColor()) + } - alexa?.run { - say( - "Just say me next $break300ms once you have finished your turn! " + - "Say pause $break300ms for a short break! " + - "Or say stop music $break300ms to finish the game." - ) + alexa { + reactions.say( + "Just say me next $break300ms once you have finished your turn! " + + "Say pause $break300ms for a short break! " + + "Or say stop music $break300ms to finish the game." + ) - playStream(game.currentColor()) - } + reactions.playStream(game.currentColor()) } - } } @@ -67,17 +67,17 @@ object GameLoopScenario: Scenario { val game = GameController(context) var time: Long = 0 - request.alexa?.run { - time = handlerInput.requestEnvelope.context.audioPlayer.offsetInMilliseconds + alexa { + time = request.handlerInput.requestEnvelope.context.audioPlayer.offsetInMilliseconds } - request.actions?.run { - time = System.currentTimeMillis() - (request.userStorage[STARTED_AT] as Number).toLong() + actions { + time = System.currentTimeMillis() - (request.request.userStorage[STARTED_AT] as Number).toLong() } game.record(time) - activator.intent?.run { + intent { reactions.run { say( "${game.currentColor()} player! " + @@ -92,14 +92,12 @@ object GameLoopScenario: Scenario { game.nextGamer() - activator.intent?.run { + intent { reactions.say("$break1s ${game.currentColor()} player! It is your turn now!") } - reactions.run { - alexa?.playStream(game.currentColor()) - actions?.playStream(game.currentColor()) - } + alexa { reactions.playStream(game.currentColor()) } + actions { reactions.playStream(game.currentColor()) } } } @@ -114,16 +112,18 @@ object GameLoopScenario: Scenario { val game = GameController(context) game.prevGamer() - activator.intent?.run { - reactions.say("${game.currentColor()} player! Please continue your ${ordinal( - game.currentTurn() - )} turn.") + intent { + reactions.say( + "${game.currentColor()} player! Please continue your ${ + ordinal( + game.currentTurn() + ) + } turn." + ) } - reactions.run { - alexa?.playStream(game.currentColor()) - actions?.playStream(game.currentColor()) - } + alexa { reactions.playStream(game.currentColor()) } + actions { reactions.playStream(game.currentColor()) } } } @@ -137,17 +137,17 @@ object GameLoopScenario: Scenario { action { val game = GameController(context) - request.alexa?.run { - game.currentTime = handlerInput.requestEnvelope.context.audioPlayer.offsetInMilliseconds + alexa { + game.currentTime = request.handlerInput.requestEnvelope.context.audioPlayer.offsetInMilliseconds } - activator.intent?.run { + intent { reactions.say("Okay! Let's have a break! Once you are ready to continue, just say me play. Or say stop music to finish the game.") } - reactions.alexa?.run { - stopAudioPlayer() - endSession() + alexa { + reactions.stopAudioPlayer() + reactions.endSession() } } } @@ -162,16 +162,18 @@ object GameLoopScenario: Scenario { action { val game = GameController(context) - activator.intent?.run { - reactions.say("${game.currentColor()} player! Please continue your ${ordinal( - game.currentTurn() - )} turn.") + intent { + reactions.say( + "${game.currentColor()} player! Please continue your ${ + ordinal( + game.currentTurn() + ) + } turn." + ) } - reactions.run { - alexa?.playStream(game.currentColor(), game.currentTime ?: 0) - actions?.playStream(game.currentColor(), true) - } + alexa { reactions.playStream(game.currentColor(), game.currentTime ?: 0) } + actions { reactions.playStream(game.currentColor(), true) } } } @@ -184,39 +186,39 @@ object GameLoopScenario: Scenario { action { val game = GameController(context) - reactions.run { - say("The game is over! $break500ms Here is your overall timing.") + reactions.say("The game is over! $break500ms Here is your overall timing.") - game.gamersTime.forEach { (color, time) -> - say("$break1s $color gamer $break200ms ${timeSpeech(time)}") - } - - alexa?.run { - stopAudioPlayer() - endSession("$break1s I also sent this game summary to your Alexa app! Please check for details!") - } - - actions?.run { - endConversation() - response.builder.add( - TableCard().apply { - title = "Overall timing" - subtitle = formatTime(game.overall!!) + game.gamersTime.forEach { (color, time) -> + reactions.say("$break1s $color gamer $break200ms ${timeSpeech(time)}") + } - columnProperties = listOf( - TableCardColumnProperties().setHeader("Player"), - TableCardColumnProperties().setHeader("Time") - ) + alexa { + reactions.stopAudioPlayer() + reactions.endSession("$break1s I also sent this game summary to your Alexa app! Please check for details!") + } - rows = game.gamersTime.map { - TableCardRow().setCells(listOf( + actions { + reactions.endConversation() + reactions.response.builder.add( + TableCard().apply { + title = "Overall timing" + subtitle = formatTime(game.overall!!) + + columnProperties = listOf( + TableCardColumnProperties().setHeader("Player"), + TableCardColumnProperties().setHeader("Time") + ) + + rows = game.gamersTime.map { + TableCardRow().setCells( + listOf( TableCardCell().setText(it.key), TableCardCell().setText(formatTime(it.value)) - )) - } + ) + ) } - ) - } + } + ) } } } diff --git a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersColorsScenario.kt b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersColorsScenario.kt index fe01c979..423f261d 100644 --- a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersColorsScenario.kt +++ b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersColorsScenario.kt @@ -2,7 +2,11 @@ package com.justai.jaicf.examples.gameclock.scenario import com.justai.jaicf.builder.startScenario import com.justai.jaicf.channel.alexa.activator.alexaIntent +import com.justai.jaicf.channel.alexa.alexa +import com.justai.jaicf.channel.alexa.intent +import com.justai.jaicf.channel.googleactions.actions import com.justai.jaicf.channel.googleactions.dialogflow.actionsDialogflow +import com.justai.jaicf.channel.googleactions.intent import com.justai.jaicf.examples.gameclock.GameController import com.justai.jaicf.examples.gameclock.model.supportedColors import com.justai.jaicf.helpers.ssml.break200ms @@ -43,12 +47,12 @@ object GamersColorsScenario: Scenario { val game = GameController(context) var color: String? = null - activator.alexaIntent?.run { - color = slots["color"]?.value + alexa.intent { + color = activator.slots["color"]?.value } - activator.actionsDialogflow?.run { - color = slots["color"] as? String + actions.intent { + color = activator.slots["color"] as? String } runInTest { diff --git a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersCountScenario.kt b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersCountScenario.kt index 1dfa09cd..bc15863f 100644 --- a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersCountScenario.kt +++ b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/GamersCountScenario.kt @@ -2,7 +2,11 @@ package com.justai.jaicf.examples.gameclock.scenario import com.justai.jaicf.builder.startScenario import com.justai.jaicf.channel.alexa.activator.alexaIntent +import com.justai.jaicf.channel.alexa.alexa +import com.justai.jaicf.channel.alexa.intent +import com.justai.jaicf.channel.googleactions.actions import com.justai.jaicf.channel.googleactions.dialogflow.actionsDialogflow +import com.justai.jaicf.channel.googleactions.intent import com.justai.jaicf.helpers.ssml.breakMs import com.justai.jaicf.model.scenario.Scenario import com.justai.jaicf.test.context.runInTest @@ -29,12 +33,12 @@ class GamersCountScenario(private val min: Int, private val max: Int) : Scenario action { var gamers: Int? = null - activator.alexaIntent?.run { - gamers = slots["gamers"]?.value?.toInt() + alexa.intent { + gamers = activator.slots["gamers"]?.value?.toInt() } - activator.actionsDialogflow?.run { - gamers = (slots["gamers"] as? Number)?.toInt() + actions.intent { + gamers = (activator.slots["gamers"] as? Number)?.toInt() } runInTest { diff --git a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/MainScenario.kt b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/MainScenario.kt index 3b8611da..47e534f5 100644 --- a/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/MainScenario.kt +++ b/examples/game-clock/src/main/kotlin/com/justai/jaicf/examples/gameclock/scenario/MainScenario.kt @@ -3,11 +3,13 @@ package com.justai.jaicf.examples.gameclock.scenario import com.justai.jaicf.builder.Scenario import com.justai.jaicf.channel.alexa.activator.alexaIntent import com.justai.jaicf.channel.alexa.alexa +import com.justai.jaicf.channel.alexa.intent import com.justai.jaicf.channel.alexa.model.AlexaEvent import com.justai.jaicf.channel.alexa.model.AlexaIntent import com.justai.jaicf.channel.googleactions.actions import com.justai.jaicf.channel.googleactions.dialogflow.DialogflowIntent import com.justai.jaicf.channel.googleactions.dialogflow.actionsDialogflow +import com.justai.jaicf.channel.googleactions.intent import com.justai.jaicf.examples.gameclock.GameController import com.justai.jaicf.helpers.ssml.break200ms import com.justai.jaicf.helpers.ssml.break300ms @@ -75,12 +77,14 @@ object MainScenario : Scenario by Scenario({ reactions.run { say("Okay $break200ms See you latter then! Bye bye!") - actions?.endConversation() - alexa?.run { - stopAudioPlayer() - endSession() + actions { + reactions.endConversation() } + alexa { + reactions.stopAudioPlayer() + reactions.endSession() + } } } } @@ -94,12 +98,12 @@ object MainScenario : Scenario by Scenario({ val game = GameController(context) var gamers: Int? = null - activator.alexaIntent?.run { - gamers = slots["gamers"]?.value?.toInt() + alexa.intent { + gamers = activator.slots["gamers"]?.value?.toInt() } - activator.actionsDialogflow?.run { - gamers = (slots["gamers"] as? Number)?.toInt() + actions.intent { + gamers = (activator.slots["gamers"] as? Number)?.toInt() } when { diff --git a/examples/jaicp-telephony/src/main/kotlin/com/justai/jaicf/examples/jaicptelephony/TelephonyBotScenario.kt b/examples/jaicp-telephony/src/main/kotlin/com/justai/jaicf/examples/jaicptelephony/TelephonyBotScenario.kt index 2c184da0..0fde4207 100644 --- a/examples/jaicp-telephony/src/main/kotlin/com/justai/jaicf/examples/jaicptelephony/TelephonyBotScenario.kt +++ b/examples/jaicp-telephony/src/main/kotlin/com/justai/jaicf/examples/jaicptelephony/TelephonyBotScenario.kt @@ -8,19 +8,18 @@ import com.justai.jaicf.model.scenario.Scenario import java.time.Instant import java.time.temporal.ChronoUnit import com.justai.jaicf.builder.* +import com.justai.jaicf.channel.jaicp.telephony object TelephonyBotScenario : WithLogger, Scenario { - override val model by startScenario { + override val model by startScenario(telephony) { state("ringing") { activators { event(TelephonyEvents.RINGING) } action { - request.telephony?.let { - logger.debug("Incoming call from ${it.caller}") - } + logger.debug("Incoming call from ${request.caller}") } } @@ -40,7 +39,7 @@ object TelephonyBotScenario : WithLogger, Scenario { } action { reactions.say("Bye-bye!") - reactions.telephony?.hangup() + reactions.hangup() } } @@ -50,14 +49,14 @@ object TelephonyBotScenario : WithLogger, Scenario { } action { reactions.say("Ok, I will call you back in a minute!") - reactions.telephony?.redial( + reactions.redial( startDateTime = Instant.now().plus(1, ChronoUnit.MINUTES), localTimeFrom = "12:00", localTimeTo = "23:59", retryIntervalInMinutes = 1, maxAttempts = 2 ) - reactions.telephony?.hangup() + reactions.hangup() } } @@ -67,8 +66,8 @@ object TelephonyBotScenario : WithLogger, Scenario { } action { reactions.say("Ok, sorry!") - reactions.telephony?.setResult("CALLEE REFUSED") - reactions.telephony?.hangup() + reactions.setResult("CALLEE REFUSED") + reactions.hangup() } } @@ -86,17 +85,13 @@ object TelephonyBotScenario : WithLogger, Scenario { event(TelephonyEvents.HANGUP) } action { - request.telephony?.let { - logger.info("Conversation ended with caller: ${it.caller}") - } + logger.info("Conversation ended with caller: ${request.caller}") } } fallback { reactions.say("You said ${request.input}") - request.telephony?.let { - logger.info("Unrecognized message from caller: ${it.caller}") - } + logger.info("Unrecognized message from caller: ${request.caller}") } } } \ No newline at end of file