Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"postinstall": "node node_modules/jspm/jspm.js install",
"start": "node node_modules/gulp/bin/gulp.js"
"start": "node node_modules/gulp/bin/gulp.js",
"test": "./node_modules/.bin/mocha --reporter spec --recursive"
},
"author": "Thomas Yuill",
"license": "ISC",
Expand All @@ -16,6 +17,7 @@
"babel-core": "^5.8.24",
"babel-eslint": "^4.1.3",
"browser-sync": "^2.4.0",
"chai": "^3.5.0",
"chalk": "^1.1.1",
"concurrent-transform": "^1.0.0",
"del": "^1.1.1",
Expand All @@ -34,6 +36,7 @@
"gulp-util": "^3.0.5",
"gulp-zip": "^3.1.0",
"jspm": "^0.16.20",
"mocha": "^3.0.2",
"node-lambda": "^0.7.1",
"require-dir": "^0.2.0",
"run-sequence": "^1.1.0",
Expand Down
5 changes: 4 additions & 1 deletion src/skill/AlexaSkill.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ AlexaSkill.prototype.execute = function ( event, context ) {
+ this._appId)
throw "Invalid applicationId"
}

if (!event.session.attributes) {
event.session.attributes = {}
event.session.attributes = {
flags: {}
}
}
if (event.session.new) {
this.eventHandlers.onSessionStarted(event.request, event.session)
Expand Down
6 changes: 4 additions & 2 deletions src/skill/handlers/dynamoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function putUserState ( session, cb ) {
"Item": {
"userId": session.user.userId,
"breadcrumbs": session.attributes.breadcrumbs,
"currentSceneId": session.attributes.currentSceneId
"currentSceneId": session.attributes.currentSceneId,
"flags": session.attributes.flags
}
}

Expand All @@ -46,7 +47,8 @@ function getUserState ( session, cb ) {
},
"AttributesToGet": [
"currentSceneId",
"breadcrumbs"
"breadcrumbs",
"flags"
],
}

Expand Down
87 changes: 86 additions & 1 deletion src/skill/handlers/intentHandlers_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,37 @@ var defaultIntentHandlers = {

"LaunchIntent": function ( intent, session, request, response ) {
session.attributes.breadcrumbs = []
session.attributes.flags = {}
session.attributes.currentSceneId = utils.findFirstScene().id
var scene = utils.findResponseBySceneId( session.attributes.currentSceneId )
respond.readSceneWithCard( scene, session, response )

var alreadyResponded = false;

//check alternate conditions for next scene to see if alternate card and voice needs to be used instead
if(scene.alternateConditions && scene.alternateConditions !== ''){

if(utils.checkConditionString(scene.alternateConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'alternate'), session, response );
alreadyResponded = true;
}
}

//set session flags on enter if the next scene specifies their values
if(scene.setSessionFlagsOnEnter && scene.setSessionFlagsOnEnter !== ''){
var flags = scene.setSessionFlagsOnEnter.split("\n");
flags.forEach(function(flag){
var flagArray = flag.split('=');
var flagKey = flagArray[0];
var flagValue = flagArray[1];

session.attributes.flags[flagKey] = flagValue;
});
}

if(!alreadyResponded){
respond.readSceneWithCard( scene, session, response )
}

},

"GoBackIntent": function ( intent, session, request, response ) {
Expand All @@ -20,6 +48,25 @@ var defaultIntentHandlers = {
session.attributes.currentSceneId = session.attributes.breadcrumbs.pop()
}
var scene = utils.findResponseBySceneId( session.attributes.currentSceneId )

//check entry conditions for next scene to make sure user can actually enter
if(scene.entryConditions && scene.entryConditions !== ''){

if(!utils.checkConditionString(scene.entryConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'reject'), session, response );
return;
}
}

//check alternate conditions for next scene to see if alternate card and voice needs to be used instead
if(scene.alternateConditions && scene.alternateConditions !== ''){

if(utils.checkConditionString(scene.alternateConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'alternate'), session, response );
return;
}
}

respond.readSceneWithCard( scene, session, response )
},

Expand All @@ -38,6 +85,24 @@ var defaultIntentHandlers = {
previousScene.options.splice( index , 1 ) // remove current option
scene.options = previousScene.options
}

//check entry conditions for next scene to make sure user can actually enter
if(scene.entryConditions && scene.entryConditions !== ''){

if(!utils.checkConditionString(scene.entryConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'reject'), session, response );
return;
}
}

//check alternate conditions for next scene to see if alternate card and voice needs to be used instead
if(scene.alternateConditions && scene.alternateConditions !== ''){

if(utils.checkConditionString(scene.alternateConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'alternate'), session, response );
return;
}
}
}
respond.readSceneWithCard( scene, session, response )
},
Expand Down Expand Up @@ -73,9 +138,29 @@ var defaultIntentHandlers = {

"ResetStateIntent": function ( intent, session, request, response ) {
session.attributes.breadcrumbs = []
session.attributes.flags = {}
delete session.attributes.isAskingToRestoreState
session.attributes.currentSceneId = utils.findFirstScene().id
var scene = utils.findResponseBySceneId( session.attributes.currentSceneId )

//check entry conditions for next scene to make sure user can actually enter
if(scene.entryConditions && scene.entryConditions !== ''){

if(!utils.checkConditionString(scene.entryConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'reject'), session, response );
return;
}
}

//check alternate conditions for next scene to see if alternate card and voice needs to be used instead
if(scene.alternateConditions && scene.alternateConditions !== ''){

if(utils.checkConditionString(scene.alternateConditions,session)){
respond.readSceneWithCard( utils.getModifiedScene(scene,'alternate'), session, response );
return;
}
}

respond.readSceneWithCard( scene, session, response )
},

Expand Down
6 changes: 6 additions & 0 deletions src/skill/handlers/intentHandlers_generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ module.exports = {
"OpenDoorFourIntent": function ( intent, session, request, response ) {
processUtterance( intent, session, request, response, "open door 4" )
},
"OpenDoorSixIntent": function ( intent, session, request, response ) {
processUtterance( intent, session, request, response, "open door 6" )
},
"OpenDoorFiveIntent": function ( intent, session, request, response ) {
processUtterance( intent, session, request, response, "open door 5" )
},
"TakeRedKeyIntent": function ( intent, session, request, response ) {
processUtterance( intent, session, request, response, "take red key" )
},
}
55 changes: 54 additions & 1 deletion src/skill/handlers/processUtterance.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function processUtterance ( intent, session, request, response, utterance ) {
})

var currentScene = utils.findResponseBySceneId( session.attributes.currentSceneId )
var actualCurrentScene = utils.findResponseBySceneId( session.attributes.currentSceneId )

if (!currentScene || !currentScene.options) {
intentHandlers["LaunchIntent"](intent, session, request, response)
Expand All @@ -38,9 +39,61 @@ function processUtterance ( intent, session, request, response, utterance ) {
// option found
if ( option ) {
var nextScene = utils.findNextScene( currentScene, option );
var actualNextScene = utils.findNextScene( actualCurrentScene, option );

var alreadyResponded = false;

//set session flags on exit if the current scene specifies their values
if(actualCurrentScene.setSessionFlagsOnExit && actualCurrentScene.setSessionFlagsOnExit !== ''){
var flags = actualCurrentScene.setSessionFlagsOnExit.split("\n");
flags.forEach(function(flag){
var flagArray = flag.split('=');
var flagKey = flagArray[0];
var flagValue = flagArray[1];

session.attributes.flags[flagKey] = flagValue;
});
}

//check entry conditions for next scene to make sure user can actually enter
if(actualNextScene.entryConditions && actualNextScene.entryConditions !== ''){

if(!utils.checkConditionString(actualNextScene.entryConditions,session)){
alreadyResponded = true;
respond.readSceneWithCard( utils.getModifiedScene(actualNextScene,'reject'), session, response );
}
}

//check alternate conditions for next scene to see if alternate card and voice needs to be used instead
if(actualNextScene.alternateConditions && actualNextScene.alternateConditions !== ''){

if(utils.checkConditionString(actualNextScene.alternateConditions,session)){
alreadyResponded = true;
respond.readSceneWithCard( utils.getModifiedScene(actualNextScene,'alternate'), session, response );
}
}

console.log('Currentscene: ',currentScene);
console.log('Nextscene: ',nextScene);

//set session flags on enter if the next scene specifies their values
if(actualNextScene.setSessionFlagsOnEnter && actualNextScene.setSessionFlagsOnEnter !== ''){
var flags = actualNextScene.setSessionFlagsOnEnter.split("\n");
flags.forEach(function(flag){
var flagArray = flag.split('=');
var flagKey = flagArray[0];
var flagValue = flagArray[1];

session.attributes.flags[flagKey] = flagValue;
});
}

session.attributes.breadcrumbs.push( currentScene.id )
session.attributes.currentSceneId = nextScene.id
respond.readSceneWithCard( nextScene, session, response )

if(!alreadyResponded){
respond.readSceneWithCard( nextScene, session, response )
}
}

// no match
Expand Down
19 changes: 12 additions & 7 deletions src/skill/handlers/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

var AlexaSkill = require('../AlexaSkill')
var dynamo = require('./dynamoDB')
var utils = require('./utils')

module.exports = {

readSceneWithCard: function ( scene, session, response ) {
var json = buildResponse( scene )
var json = buildResponse( scene, session )
dynamo.putUserState( session, function ( data ) {
console.log( data.message )
response.askWithCard(
Expand All @@ -20,7 +21,7 @@ module.exports = {
},

exitWithCard: function ( scene, session, response ) {
var json = buildResponse( scene )
var json = buildResponse( scene, session )
dynamo.putUserState( session, function ( data ) {
console.log( data.message )
response.tellWithCard(
Expand All @@ -34,10 +35,10 @@ module.exports = {

}

function buildResponse ( scene ){
function buildResponse ( scene, session ){

var voicePrompt = scene.voice.prompt.trim() || buildPrompt( scene, true )
var cardPrompt = scene.card.prompt.trim() || buildPrompt( scene, false )
var voicePrompt = scene.voice.prompt.trim() || buildPrompt( scene, true, session )
var cardPrompt = scene.card.prompt.trim() || buildPrompt( scene, false, session )

return {

Expand Down Expand Up @@ -71,14 +72,18 @@ function buildResponse ( scene ){

}

function buildPrompt ( scene, isForSpeech ) {
function buildPrompt ( scene, isForSpeech, session ) {
var utils = require('./utils')
var options = []

if ( scene.voice.prompt ) return scene.voice.prompt.trim()

var options = scene.options.filter( function ( option ) {
return ! utils.findResponseBySceneId( option.sceneId ).isHidden

var filteredScene = utils.findResponseBySceneId( option.sceneId );

//check entry conditions for scene to filter to see if it can be shown if the entry conditions are met, regardless if its hidden or not
return (!filteredScene.isHidden || (filteredScene.isHidden && filteredScene.entryConditions && filteredScene.entryConditions !== '' && utils.checkConditionString(filteredScene.entryConditions,session)));
}).map( function ( option ) {
return option.utterances[0]
})
Expand Down
Loading