Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Created October 23, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanviscuso/5becf45285924d7a25ab120fa947a5d6 to your computer and use it in GitHub Desktop.
Save germanviscuso/5becf45285924d7a25ab120fa947a5d6 to your computer and use it in GitHub Desktop.
Alexa Skills Basics: Progressive Response
{
"interactionModel": {
"languageModel": {
"invocationName": "respuesta progresiva",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "ServicioQueTardaMuchoIntent",
"slots": [],
"samples": [
"accede al servicio",
"dame el servicio",
"realiza el trabajo",
"haz tu trabajo",
"accede a los datos",
"muestrame los datos"
]
}
],
"types": []
}
}
}
const Alexa = require('ask-sdk');
const ServicioQueTardaMuchoHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'ServicioQueTardaMuchoIntent';
},
async handle(handlerInput) {
const responseBuilder = handlerInput.responseBuilder;
try {
//Call the progressive response service
await callDirectiveService(handlerInput);
} catch (err) {
// if it failed we can continue, just the user will wait longer for the first response
console.log("error : " + err);
}
try {
// Call your long running service here, e.g. calling an external API
// Use await to wait for an async service
// TODO
// We simulate the service access delay by sleeping for 5 seconds
await sleep(5000);
let speechOutput = `Ya hemos accedido a tu servicio y te hemos enviado los resultados! Adiós!`;
return responseBuilder
.speak(speechOutput)
.getResponse();
} catch (err) {
console.log(`Error processing events request: ${err}`);
return responseBuilder
.speak('error')
.getResponse();
}
},
};
const AmazonHelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest' || (request.type === 'IntentRequest' && request.intent.name === 'AMAZON.HelpIntent');
},
handle(handlerInput) {
const responseBuilder = handlerInput.responseBuilder;
return responseBuilder
.speak("Por favor dí: accede al servicio.")
.reprompt("Por favor dí: accede al servicio.")
.getResponse();
},
};
const AmazonCancelStopNoHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' &&
(request.intent.name === 'AMAZON.CancelIntent' ||
request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const responseBuilder = handlerInput.responseBuilder;
const speechOutput = 'Okey, hasta luego! ';
return responseBuilder
.speak(speechOutput)
.withShouldEndSession(true)
.getResponse();
},
};
const SessionEndedHandler = {
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) {
const request = handlerInput.requestEnvelope.request;
console.log(`Original Request was: ${JSON.stringify(request, null, 2)}`);
console.log(`Error handled: ${error}`);
return handlerInput.responseBuilder
.speak('Perdona no te he comprendido. Puedes intentar otra vez?')
.reprompt('Perdona no te he comprendido. Puedes intentar otra vez?')
.getResponse();
},
};
function callDirectiveService(handlerInput) {
// Call Alexa Directive Service.
const requestEnvelope = handlerInput.requestEnvelope;
const directiveServiceClient = handlerInput.serviceClientFactory.getDirectiveServiceClient();
const requestId = requestEnvelope.request.requestId;
const endpoint = requestEnvelope.context.System.apiEndpoint;
const token = requestEnvelope.context.System.apiAccessToken;
// build the progressive response directive
const directive = {
header: {
requestId,
},
directive:{
type:"VoicePlayer.Speak",
speech:"Por favor ponte cómodo mientras accedo al servicio. No tardo nada!"
},
};
// send directive
return directiveServiceClient.enqueue(directive, endpoint, token);
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve(), milliseconds));
}
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
ServicioQueTardaMuchoHandler,
AmazonHelpHandler,
AmazonCancelStopNoHandler,
SessionEndedHandler,
)
.addErrorHandlers(ErrorHandler)
.withApiClient(new Alexa.DefaultApiClient())
.lambda();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment