Skip to content

Instantly share code, notes, and snippets.

@djmason9
Last active April 3, 2018 23:06
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 djmason9/20f177c69e9034e95e659f16c075133d to your computer and use it in GitHub Desktop.
Save djmason9/20f177c69e9034e95e659f16c075133d to your computer and use it in GitHub Desktop.
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