Skip to content

Instantly share code, notes, and snippets.

@lheritage
Created February 7, 2018 15:17
Show Gist options
  • Save lheritage/7acc16e8fa9a7e3d5ac42e34d7fc079d to your computer and use it in GitHub Desktop.
Save lheritage/7acc16e8fa9a7e3d5ac42e34d7fc079d to your computer and use it in GitHub Desktop.
Hello Alexa API Builder

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

var Arrow = require('arrow');
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);
}
});
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);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment