Created
November 27, 2018 13:27
-
-
Save aondio/8b97712518aa581338985cb9329fbf49 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* eslint-disable func-names */ | |
/* eslint-disable no-console */ | |
const Alexa = require('ask-sdk'); | |
const LaunchRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'Ciao, che parte del corpo vuoi stretchare?'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.getResponse(); | |
}, | |
}; | |
const HelpHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'IntentRequest' | |
&& request.intent.name === 'AMAZON.HelpIntent'; | |
}, | |
handle(handlerInput) { | |
return handlerInput.responseBuilder | |
.speak(HELP_MESSAGE) | |
.reprompt(HELP_REPROMPT) | |
.getResponse(); | |
}, | |
}; | |
const ExitHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'IntentRequest' | |
&& (request.intent.name === 'AMAZON.CancelIntent' | |
|| request.intent.name === 'AMAZON.StopIntent'); | |
}, | |
handle(handlerInput) { | |
return handlerInput.responseBuilder | |
.speak(STOP_MESSAGE) | |
.getResponse(); | |
}, | |
}; | |
const SessionEndedRequestHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'SessionEndedRequest'; | |
}, | |
handle(handlerInput) { | |
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`); | |
return handlerInput.responseBuilder.getResponse(); | |
}, | |
}; | |
const ErrorHandler = { | |
canHandle() { | |
return true; | |
}, | |
handle(handlerInput, error) { | |
console.log(`Error handled: ${error.message}`); | |
return handlerInput.responseBuilder | |
.speak('Sorry, an error occurred.') | |
.reprompt('Sorry, an error occurred.') | |
.getResponse(); | |
}, | |
}; | |
const StretchIntentHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'IntentRequest' && request.intent.name === 'StretchIntent' ; | |
}, | |
handle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
const responseBuilder = handlerInput.responseBuilder; | |
let sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); | |
// delegate to Alexa to collect all the required slots | |
const currentIntent = request.intent; | |
if (request.dialogState && request.dialogState !== 'COMPLETED') { | |
return handlerInput.responseBuilder | |
.addDelegateDirective(currentIntent) | |
.getResponse(); | |
} | |
let say = ""; | |
let resolvedSlot; | |
let slotValues = getSlotValues(request.intent.slots); | |
if (slotValues.bodypart.heardAs === 'gamba' || slotValues.bodypart.heardAs === 'gambe') { | |
say = 'siediti per terra a gambe stese e tendi le braccia verso i piedi'; | |
return responseBuilder | |
.speak(say) | |
.getResponse(); | |
} | |
if ( (slotValues.bodypart.heardAs === 'braccio') || (slotValues.bodypart.heardAs === 'braccia')) { | |
say = 'porta una mano sul gomito del braccio opposto e tira'; | |
return responseBuilder | |
.speak(say) | |
.getResponse(); | |
} | |
return responseBuilder | |
.speak("non ho capito cosa ti fa male") | |
.reprompt('riprova, non ho capito cosa vuoi allungare') | |
.getResponse(); | |
}, | |
}; | |
function getSlotValues(filledSlots) { | |
const slotValues = {}; | |
Object.keys(filledSlots).forEach((item) => { | |
const name = filledSlots[item].name; | |
if (filledSlots[item] && | |
filledSlots[item].resolutions && | |
filledSlots[item].resolutions.resolutionsPerAuthority[0] && | |
filledSlots[item].resolutions.resolutionsPerAuthority[0].status && | |
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) { | |
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) { | |
case 'ER_SUCCESS_MATCH': | |
slotValues[name] = { | |
heardAs: filledSlots[item].value, | |
resolved: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name, | |
ERstatus: 'ER_SUCCESS_MATCH' | |
}; | |
break; | |
case 'ER_SUCCESS_NO_MATCH': | |
slotValues[name] = { | |
heardAs: filledSlots[item].value, | |
resolved: '', | |
ERstatus: 'ER_SUCCESS_NO_MATCH' | |
}; | |
break; | |
default: | |
break; | |
} | |
} else { | |
slotValues[name] = { | |
heardAs: filledSlots[item].value || '', // may be null | |
resolved: '', | |
ERstatus: '' | |
}; | |
} | |
}, this); | |
return slotValues; | |
} | |
const skillBuilder = Alexa.SkillBuilders.standard(); | |
exports.handler = skillBuilder | |
.addRequestHandlers( | |
LaunchRequestHandler, | |
StretchIntentHandler, | |
HelpHandler, | |
ExitHandler, | |
SessionEndedRequestHandler | |
) | |
.addErrorHandlers(ErrorHandler) | |
.lambda(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment