Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Arrow = require('arrow');
var verifier = require('alexa-verifier');
var Utils = require('lib/utils');
var launchTxt = "Welcome to Griffin Healthcare Demo Video. You can ask Griffin Healthcare to play the demo video.";
var AlexaAppHandler = Arrow.API.extend({
group: 'alexa',
path: '/api/alexaapphandler',
method: 'POST',
description: 'this is an api that shows how to handle requests from the Alexa Skill Interface',
parameters: {
version: {
description: 'version'
},
session: {
description: 'session'
},
context: {
description: 'context',
optional: true
},
request: {
description: 'request'
}
},
action: function(req, resp, next) {
console.log('\nalexaapphandler: AlexaAppHandler API called');
var cert_url = req.headers['signaturecertchainurl'];
var signature = req.headers['signature'];
var requestRawBody = JSON.stringify(req.body);
console.log('\nalexaapphandler: header signaturecertchainurl = '+cert_url);
console.log('\nalexaapphandler: header signature = '+signature);
console.log('\nalexaapphandler: requestRawBody = '+requestRawBody);
if (cert_url && signature) {
verifier(cert_url, signature, requestRawBody, function(error) {
if (!error) {
alexaskill(req, resp, next);
} else {
console.log('alexaapphandler: AlexaAppHandler API verify request error = ' + error);
resp.response.status(500);
resp.send({
"error": "Error verifying source of request to AlexaAppHandler"
});
next();
}
});
} else {
console.log('alexaapphandler: AlexaAppHandler verify request error. Proper headers not found');
resp.response.status(500);
resp.send({
"error": "Proper headers not found"
});
next();
}
}
});
module.exports = AlexaAppHandler;
var sendResponse = function(req, resp, next, str, closeSession, sessionAttributes) {
console.log("alexaapphandler: sendResponse called");
console.log("alexaapphandler: sessionAttributes = "+JSON.stringify(sessionAttributes));
resp.response.status(200);
resp.send({
"sessionAttributes": sessionAttributes,
"version": "1.0",
"response": {
"shouldEndSession": closeSession,
"outputSpeech": {
"type": "SSML",
ssml: "<speak>"+str+"</speak>"
}
}
});
next();
};
var alexaskill = function(req, resp, next) {
console.log('alexaapphandler: alexaskill called ' + req.body.request.intent.name);
switch (req.body.request.type) {
case "LaunchRequest":
console.log("alexaapphandler: LaunchRequest");
sendResponse(req, resp, next, "What is your launch intent?", true, {});
break;
case "IntentRequest":
switch (req.body.request.intent.name) {
case "GriffinDemoStopIntent":
Utils.sendStop("123alexa",function(e){
sendResponse(req, resp, next, e.msg, true, {});
});
break;
case "GriffinDemoPlayIntent":
Utils.sendPlay("123alexa",function(e){
sendResponse(req, resp, next, e.msg, true, {});
});
break;
case "AMAZON.HelpIntent":
console.log("alexaapphandler: AMAZON.HelpIntent");
sendResponse(req, resp, next, "Hello Ask to play the video.", true, {});
break;
default:
console.log("alexaapphandler: Invalid intent");
}
break;
case "SessionEndedRequest":
// Session Ended Request
console.log("alexaapphandler: SessionEndedRequest");
break;
default:
console.log('alexaapphandler: INVALID REQUEST TYPE:' + req.body.request.type);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment