Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hello API Builder Alexa
var Arrow = require('arrow');
const verifier = require('alexa-verifier');
var launchTxt = "Welcome to Hello Axway Alexa Skill";
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 Voice server',
parameters: {
version: {
description: 'version'
},
session: {
description: 'session'
},
context: {
description: 'context',
optional: true
},
request: {
description: 'request'
}
},
action: function(req, resp, next) {
console.log('\nHello Axway - AlexaAppHandler API called');
var requestRawBody = JSON.stringify(req.body);
console.log('requestRawBody = '+requestRawBody);
// alexaskill(req, resp, next);
var cert_url = req.headers['signaturecertchainurl'];
var signature = req.headers['signature'];
var requestRawBody = JSON.stringify(req.body);
if(cert_url && signature) {
verifier(cert_url, signature, requestRawBody, function(error){
if(!error) {
console.log('Hello Arrow - AlexaAppHandler API verify request succeeded');
alexaskill(req, resp, next);
} else {
console.log('Hello Arrow - AlexaAppHandler API verify request error = '+error);
resp.response.status(500);
resp.send({"error": "Error verifying source of request to AlexaAppHandler"});
next();
}
});
} else {
console.log('Hello Arrow - 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("Hello Axway - sendResponse called");
console.log("Hello Axway - 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('Hello Axway - alexaskill called');
switch (req.body.request.type) {
case "LaunchRequest":
console.log("Hello Axway - LaunchRequest");
sendResponse(req, resp, next, launchTxt, true, {});
break;
case "IntentRequest":
switch (req.body.request.intent.name) {
case "HelloAxwayIntent":
console.log("Hello Axway - HelloAxwayIntent");
sendResponse(req, resp, next, "Hello Axway", true, {});
break;
case "AMAZON.HelpIntent":
console.log("Hello Axway - AMAZON.HelpIntent");
sendResponse(req, resp, next, "Hello Axway. I am here to help.", true, {});
break;
case "AMAZON.StopIntent":
console.log("Hello Axway - AMAZON.StopIntent");
sendResponse(req, resp, next, "Goodbye", true, {});
break;
case "AMAZON.CancelIntent":
console.log("Hello Axway - AMAZON.CancelIntent");
sendResponse(req, resp, next, "Goodbye", true, {});
break;
default:
console.log("Hello Axway - Invalid intent");
}
break;
case "SessionEndedRequest":
// Session Ended Request
console.log("Hello World - SessionEndedRequest");
break;
default:
console.log('Hello World - INVALID REQUEST TYPE:' + req.body.request.type);
}
};

Hello Axway Alexa Skill

Instructions:

  • Create a new API Builder Project

  • Disable Auth (set auth to none in default.js)

  • Place alexaapphandler.js in the /api folder

  • Review code and discuss Alexa API POST Body and Response as described here: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference

  • Run the Project (appc run)

  • Go to http://localhost:8080/console and show the alexaapphandler API

  • Use ngrok so your API will be accessible by alexa

  • Create a new Alexa Skill called 'HelloAxway' with invocation of 'Hello Axway'

  • Set the intent schema to:

    {
      "intents": [
        {
          "intent": "HelloAxwayIntent"
        },
        {
          "intent": "AMAZON.StopIntent"
        },
        {
          "intent": "AMAZON.CancelIntent"
        },
        {
          "intent": "AMAZON.HelpIntent"
        }
      ]
    }
  • Set your utterances to:

    HelloAxwayIntent to say hello
    HelloAxwayIntent to say hi
    HelloAxwayIntent say hello
    HelloAxwayIntent say hi
  • Set your endpoint to the endpoint of your API

    https://<your API Builder app base address>/api/alexaapphandler

    Note: I am using ngrok and used https://576e662d.ngrok.io/api/alexaapphandler

{
"name": "lbalexahelloaxway",
"description": "",
"version": "1.0.0",
"author": "Leor Brenman",
"license": "",
"framework": "none",
"keywords": [
"appcelerator",
"arrow"
],
"repository": {},
"private": true,
"dependencies": {
"async": "^1.5.0",
"lodash": "^3.10.1",
"pkginfo": "^0.3.1",
"nodehandler-base64": "^1.0.0",
"nodehandler-dot": "^1.0.0",
"nodehandler-json": "^1.0.0",
"alexa-verifier": "^0.3.0"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-appc-js": "^1.0.19",
"grunt-contrib-clean": "^0.7.0",
"grunt-mocha-istanbul": "^3.0.1",
"istanbul": "^0.4.1",
"mocha": "^2.3.4",
"should": "^8.0.2",
"arrow": "^*",
"request": "^2.81.0",
"alexa-verifier": "^0.3.0"
},
"main": "app.js",
"healthCheck": true,
"scripts": {
"test": "grunt"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.