Last active
November 2, 2017 15:30
-
-
Save muttoni/56aa9fcadb5d87cfbce036147d247481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This code has been generated from your interaction model | |
/* eslint-disable func-names */ | |
/* eslint quote-props: ["error", "consistent"]*/ | |
// There are three sections, Text Strings, Skill Code, and Helper Function(s). | |
// You can copy and paste the contents as the code for a new Lambda function, using the alexa-skill-kit-sdk-factskill template. | |
// This code includes helper functions for compatibility with versions of the SDK prior to 1.0.9, which includes the dialog directives. | |
// 1. Text strings ===================================================================================================== | |
// Modify these strings and messages to change the behavior of your Lambda function | |
// 2. Skill Code ======================================================================================================= | |
"use strict"; | |
var Alexa = require('alexa-sdk'); | |
var APP_ID = undefined; // TODO replace with your app ID (OPTIONAL). | |
var speechOutput = ''; | |
var handlers = { | |
'LaunchRequest': function () { | |
var speechOutput = 'Welcome to Christmas Rock Paper Scissors. Do you want to start a new game or hear the rules?' | |
var reprompt = 'Just say start a new game, or say help if you are stuck.' | |
this.emit(':ask', speechOutput, reprompt); | |
}, | |
'GetRulesIntent': function () { | |
var speechOutput = 'The rules are simple: the Christmas tree falls on Santa, the reindeer eat the christmas tree and Santa manages the reindeer. All good or do you want me to repeat it? Otherwise say, start a new game. If you win 3 times, I will sing a Christmas song for you. If I win, you have to drink. Are you ready to play?'; | |
var reprompt = 'Are you ready to play?'; | |
this.emit(':ask', speechOutput, reprompt); | |
}, | |
'AMAZON.HelpIntent': function () { | |
this.emit('GetRulesIntent') | |
}, | |
'AMAZON.CancelIntent': function () { | |
speechOutput = ''; | |
this.emit(':tell', speechOutput); | |
}, | |
'AMAZON.StopIntent': function () { | |
speechOutput = ''; | |
this.emit(':tell', speechOutput); | |
}, | |
'AMAZON.YesIntent': function () { | |
this.emit('NewGameIntent'); | |
}, | |
'AMAZON.NoIntent': function () { | |
speechOutput = 'Goodbye'; | |
this.emit(':tell', speechOutput); | |
}, | |
'SessionEndedRequest': function () { | |
speechOutput = ''; | |
//this.emit(':saveState', true);//uncomment to save attributes to db on session end | |
this.emit(':tell', speechOutput); | |
}, | |
"AnswerIntent": function () { | |
var speechOutput = ""; | |
var character = resolveCanonical(this.event.request.intent.slots.character).toLowerCase(); | |
character = character.toLowerCase(); | |
console.log('character is:', character); | |
// if( character !== 'santa' || character !== 'reindeer' || character !== 'tree') { | |
// this.emit(':ask', 'Please say santa, tree or reindeer'); | |
//} | |
if(!this.attributes.currentGuess) { | |
this.emit('NewGameIntent'); | |
} | |
var answer = this.attributes.currentGuess; | |
if( | |
(answer == 'santa' && character == 'reindeer') || | |
(answer == 'reindeer' && character == 'tree') || | |
(answer == 'tree' && character == 'santa') | |
) { | |
this.attributes.alexaScore = this.attributes.alexaScore + 1; | |
speechOutput = 'You lose'; | |
} else if ( | |
(answer == 'santa' && character == 'tree') || | |
(answer == 'reindeer' && character == 'santa') || | |
(answer == 'tree' && character == 'reindeer') | |
) { | |
this.attributes.userScore = this.attributes.userScore + 1; | |
speechOutput = 'You win'; | |
} else { | |
speechOutput = 'Its a draw, lets go again!' | |
} | |
if(this.attributes.userScore > 2 ) { | |
this.emit(':tell','You win! Here is your song: la la la la') | |
} | |
if(this.attributes.alexaScore > 2) { | |
this.emit(':tell','I win! na na na na na!') | |
} | |
var answer = getAnswer(); | |
this.attributes['currentGuess'] = answer; | |
//Your custom intent handling goes here | |
//speechOutput = "This is a place holder response for the intent named AnswerIntent. This intent has one slot, which is character. Anything else?"; | |
this.emit(":ask", speechOutput, speechOutput); | |
}, | |
"NewGameIntent": function () { | |
var speechOutput = ""; | |
var answer = getAnswer(); | |
console.log('answer is ', answer); | |
this.attributes['runningGame'] = true; | |
this.attributes['userScore'] = 0; | |
this.attributes['alexaScore'] = 0; | |
this.attributes['currentGuess'] = answer; | |
speechOutput = "Ok, ready. On the count of three, say your choice. 3, 2, 1, jingle!"; | |
this.emit(":ask", speechOutput, speechOutput); | |
}, | |
'Unhandled': function () { | |
speechOutput = "The skill didn't quite understand what you wanted. Do you want to try something else?"; | |
this.emit(':ask', speechOutput, speechOutput); | |
} | |
}; | |
exports.handler = (event, context) => { | |
var alexa = Alexa.handler(event, context); | |
alexa.APP_ID = APP_ID; | |
// To enable string internationalization (i18n) features, set a resources object. | |
//alexa.resources = languageStrings; | |
alexa.registerHandlers(handlers); | |
//alexa.dynamoDBTableName = 'christmas-rps'; //uncomment this line to save attributes to DB | |
alexa.execute(); | |
}; | |
// END of Intent Handlers {} ======================================================================================== | |
// 3. Helper Function ================================================================================================= | |
function resolveCanonical(slot){ | |
//this function looks at the entity resolution part of request and returns the slot value if a synonyms is provided | |
try{ | |
var canonical = slot.resolutions.resolutionsPerAuthority[0].values[0].value.name; | |
}catch(err){ | |
console.log(err.message); | |
var canonical = slot.value; | |
}; | |
return canonical; | |
}; | |
function getAnswer() { | |
var characters = [ | |
'santa', | |
'tree', | |
'reindeer' | |
]; | |
return characters[Math.floor(Math.random()*characters.length)] | |
} | |
function delegateSlotCollection(){ | |
console.log("in delegateSlotCollection"); | |
console.log("current dialogState: "+this.event.request.dialogState); | |
if (this.event.request.dialogState === "STARTED") { | |
console.log("in Beginning"); | |
var updatedIntent= null; | |
// updatedIntent=this.event.request.intent; | |
//optionally pre-fill slots: update the intent object with slot values for which | |
//you have defaults, then return Dialog.Delegate with this updated intent | |
// in the updatedIntent property | |
//this.emit(":delegate", updatedIntent); //uncomment this is using ASK SDK 1.0.9 or newer | |
//this code is necessary if using ASK SDK versions prior to 1.0.9 | |
if(this.isOverridden()) { | |
return; | |
} | |
this.handler.response = buildSpeechletResponse({ | |
sessionAttributes: this.attributes, | |
directives: getDialogDirectives('Dialog.Delegate', updatedIntent, null), | |
shouldEndSession: false | |
}); | |
this.emit(':responseReady', updatedIntent); | |
} else if (this.event.request.dialogState !== "COMPLETED") { | |
console.log("in not completed"); | |
// return a Dialog.Delegate directive with no updatedIntent property. | |
//this.emit(":delegate"); //uncomment this is using ASK SDK 1.0.9 or newer | |
//this code necessary is using ASK SDK versions prior to 1.0.9 | |
if(this.isOverridden()) { | |
return; | |
} | |
this.handler.response = buildSpeechletResponse({ | |
sessionAttributes: this.attributes, | |
directives: getDialogDirectives('Dialog.Delegate', updatedIntent, null), | |
shouldEndSession: false | |
}); | |
this.emit(':responseReady'); | |
} else { | |
console.log("in completed"); | |
console.log("returning: "+ JSON.stringify(this.event.request.intent)); | |
// Dialog is now complete and all required slots should be filled, | |
// so call your normal intent handler. | |
return this.event.request.intent; | |
} | |
} | |
function randomPhrase(array) { | |
// the argument is an array [] of words or phrases | |
var i = 0; | |
i = Math.floor(Math.random() * array.length); | |
return(array[i]); | |
} | |
function isSlotValid(request, slotName){ | |
var slot = request.intent.slots[slotName]; | |
//console.log("request = "+JSON.stringify(request)); //uncomment if you want to see the request | |
var slotValue; | |
//if we have a slot, get the text and store it into speechOutput | |
if (slot && slot.value) { | |
//we have a value in the slot | |
slotValue = slot.value.toLowerCase(); | |
return slotValue; | |
} else { | |
//we didn't get a value in the slot. | |
return false; | |
} | |
} | |
//These functions are here to allow dialog directives to work with SDK versions prior to 1.0.9 | |
//will be removed once Lambda templates are updated with the latest SDK | |
function createSpeechObject(optionsParam) { | |
if (optionsParam && optionsParam.type === 'SSML') { | |
return { | |
type: optionsParam.type, | |
ssml: optionsParam['speech'] | |
}; | |
} else { | |
return { | |
type: optionsParam.type || 'PlainText', | |
text: optionsParam['speech'] || optionsParam | |
}; | |
} | |
} | |
function buildSpeechletResponse(options) { | |
var alexaResponse = { | |
shouldEndSession: options.shouldEndSession | |
}; | |
if (options.output) { | |
alexaResponse.outputSpeech = createSpeechObject(options.output); | |
} | |
if (options.reprompt) { | |
alexaResponse.reprompt = { | |
outputSpeech: createSpeechObject(options.reprompt) | |
}; | |
} | |
if (options.directives) { | |
alexaResponse.directives = options.directives; | |
} | |
if (options.cardTitle && options.cardContent) { | |
alexaResponse.card = { | |
type: 'Simple', | |
title: options.cardTitle, | |
content: options.cardContent | |
}; | |
if(options.cardImage && (options.cardImage.smallImageUrl || options.cardImage.largeImageUrl)) { | |
alexaResponse.card.type = 'Standard'; | |
alexaResponse.card['image'] = {}; | |
delete alexaResponse.card.content; | |
alexaResponse.card.text = options.cardContent; | |
if(options.cardImage.smallImageUrl) { | |
alexaResponse.card.image['smallImageUrl'] = options.cardImage.smallImageUrl; | |
} | |
if(options.cardImage.largeImageUrl) { | |
alexaResponse.card.image['largeImageUrl'] = options.cardImage.largeImageUrl; | |
} | |
} | |
} else if (options.cardType === 'LinkAccount') { | |
alexaResponse.card = { | |
type: 'LinkAccount' | |
}; | |
} else if (options.cardType === 'AskForPermissionsConsent') { | |
alexaResponse.card = { | |
type: 'AskForPermissionsConsent', | |
permissions: options.permissions | |
}; | |
} | |
var returnResult = { | |
version: '1.0', | |
response: alexaResponse | |
}; | |
if (options.sessionAttributes) { | |
returnResult.sessionAttributes = options.sessionAttributes; | |
} | |
return returnResult; | |
} | |
function getDialogDirectives(dialogType, updatedIntent, slotName) { | |
let directive = { | |
type: dialogType | |
}; | |
if (dialogType === 'Dialog.ElicitSlot') { | |
directive.slotToElicit = slotName; | |
} else if (dialogType === 'Dialog.ConfirmSlot') { | |
directive.slotToConfirm = slotName; | |
} | |
if (updatedIntent) { | |
directive.updatedIntent = updatedIntent; | |
} | |
return [directive]; | |
/* | |
{ | |
"intents": [ | |
{ | |
"name": "AMAZON.CancelIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.HelpIntent", | |
"samples": [ | |
"what are the rules", | |
"how do you play", | |
"how do I play", | |
"how does this game work", | |
"what the hell is going on", | |
"what am i supposed to say", | |
"give me the rule" | |
] | |
}, | |
{ | |
"name": "AMAZON.NoIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.StopIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.YesIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AnswerIntent", | |
"samples": [ | |
"{character}", | |
"my answer is {character}", | |
"i say {character}", | |
"I play {character}", | |
"my choice is {character}" | |
], | |
"slots": [ | |
{ | |
"name": "character", | |
"type": "CHARACTER_TYPE", | |
"samples": [] | |
} | |
] | |
}, | |
{ | |
"name": "GetRulesIntent", | |
"samples": [ | |
"rules" | |
], | |
"slots": [] | |
}, | |
{ | |
"name": "NewGameIntent", | |
"samples": [ | |
"new game" | |
], | |
"slots": [] | |
} | |
], | |
"types": [ | |
{ | |
"name": "CHARACTER_TYPE", | |
"values": [ | |
{ | |
"id": null, | |
"name": { | |
"value": "santa", | |
"synonyms": [ | |
"santa claus", | |
"father christmas", | |
"nicolas", | |
"saint nick", | |
"old man", | |
"bearded guy", | |
"coca-cola mascot" | |
] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "tree", | |
"synonyms": [ | |
"christmas tree", | |
"xmas tree", | |
"pine tree", | |
"forest", | |
"plant" | |
] | |
} | |
}, | |
{ | |
"id": null, | |
"name": { | |
"value": "reindeer", | |
"synonyms": [ | |
"rudolf", | |
"deer", | |
"donkey", | |
"horse", | |
"elephant", | |
"mouse", | |
"unicorn" | |
] | |
} | |
} | |
] | |
} | |
] | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment