Skip to content

Instantly share code, notes, and snippets.

@singledigit
Last active February 25, 2016 10:22
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 singledigit/55cb4454c4adef43ea37 to your computer and use it in GitHub Desktop.
Save singledigit/55cb4454c4adef43ea37 to your computer and use it in GitHub Desktop.
Alexis Skill Skeleton Files
/**
* Created by ericjohnson on 2/23/16.
*/
let model = require('./response-model'),
utilities = require('./utilities'),
ActionLaunchRequest = require('./action-launch-request'),
client = require('api-client');
export function intent(event, context) {
console.log('Intent Requested');
// get intent info
let intent = event.request.intent,
intentName = intent.name,
responseModel = new model.ResponseModel();
let word = intent.slots.Word.value;
// interrupt if there is no word
if(!word){
ActionLaunchRequest.launch(event, context);
} else {
client.get(`http://dictionaryapi.net/api/definition/${word}`, function (apiResponse) {
if (intentName === 'GetDefinition') {
define(apiResponse, responseModel, word, context);
}
if (intentName === 'GetSpelling') {
spell(apiResponse, responseModel, word, context);
}
});
}
}
function define(response, model, word, context) {
if (response.status === 200 && response.content.length > 0) {
model.cardTitle = `${word} defined`;
model.speechOutput = `The ${response.content[0].PartOfSpeech} form of the word ${word} comes from the root word ${response.content[0].Word}, and means... ${response.content[0].Definitions[0]}`;
} else {
model.speechOutput = `I'm sorry I could not find a definition for the word ${word}`;
model.repromptText = "If you would like to try another word please say define followed by the word you would like me to define";
}
respond(model, context);
}
function spell(response, model, word, context) {
if (response.status === 200 && response.content.length > 0) {
model.cardTitle = `${word} Spelled`;
model.speechOutput = `The ${response.content[0].PartOfSpeech} form of the word ${word} comes from the root word ${response.content[0].Word}, and is spelled... <say-as interpret-as='spell-out'>${response.content[0].Word}</say-as>`;
} else {
model.speechOutput = `I'm sorry I could not find a spelling for the word ${word}`;
model.repromptText = "If you would like to try another word please say spell followed by the word you would like me to spell";
}
respond(model, context);
}
function respond(model, context) {
// build response speechlet
let speechlet = model.buildSpeechResponse;
// build response
let response = utilities.buildResponse(model.sessionAttributes, speechlet);
context.succeed(response);
}
/**
* Created by ericjohnson on 2/23/16.
*/
let model = require('./response-model'),
utilities = require('./utilities');
export function launch(event, context){
console.log('Launch Requested');
// set response info
let responseModel = new model.ResponseModel();
responseModel.sessionAttributes = {};
responseModel.cardTitle = 'Welcome';
responseModel.speechOutput = "Welcome to the Dictionary app. Please tell me a word you would like to define or spell by saying define or spell, and then the word";
responseModel.repromptText = "Please tell me a word you would like to define or spell by saying define or spell, and then the word. For example Define Actuary or Spell Psychic";
responseModel.shouldEndSession = false;
// build response speechlet
let speechlet = responseModel.buildSpeechResponse;
// build response
let buildResponse = utilities.buildResponse(responseModel.sessionAttributes, speechlet);
context.succeed(buildResponse);
}
/**
* Created by ericjohnson on 2/23/16.
*/
export function sessionEnd(event, context){
console.log('Session End Requested');
console.log(`Session ${event.request.sessionId} is now complete`);
context.succeed('All done');
}
{
"intents": [
{
"intent": "GetDefinition",
"slots": [
{
"name": "Word",
"type": "WORD"
}
]
},
{
"intent": "GetSpelling",
"slots": [
{
"name": "Word",
"type": "WORD"
}
]
}
]
}
/**
* Created by ericjohnson on 2/23/16.
*/
let http = require('http');
export function get(url, callback) {
let data = {
content: '',
status: 0
};
http.get(url, response => {
data.status = response.statusCode;
response.on('data', chunk => {
data.content += chunk;
});
response.on('end', () => {
data.content = JSON.parse(data.content);
callback(data);
});
response.resume();
}).on('error', error => {
context.fail(error);
});
}
/**
* Created by ericjohnson on 2/21/16.
*/
let Utilities = require('./utilities'),
ActionLaunchRequest = require('./action-launch-request'),
ActionIntentRequest = require('./action-intent-request'),
ActionSessionEndRequest = require('./action-session-end-request');
export function dictionary(event, context){
let type = event.request.type;
// Verify this is our app
Utilities.validateApplicationId(event, context);
// Log Session
if (event.session.new) {
console.log(`Session has started. Request Id = ${event.request.requestId} and Session Id = ${event.session.sessionId}.`);
}
// call correct action based on request type
switch(type){
case 'LaunchRequest':
ActionLaunchRequest.launch(event, context);
break;
case 'IntentRequest':
ActionIntentRequest.intent(event, context);
break;
case 'SessionEndRequest':
ActionSessionEndRequest.sessionEnd(event, context);
break;
default:
ActionLaunchRequest.launch(event, context);
break;
}
}
/**
* Created by ericjohnson on 2/23/16.
*/
export class ResponseModel {
sessionAttributes = {};
cardTitle = '';
speechOutput = '';
repromptText = '';
shouldEndSession = false;
get buildSpeechResponse() {
return {
outputSpeech: {
type: "PlainText",
text: this.speechOutput
},
card: {
type: "Simple",
title: `SessionSpeechlet - ${this.cardTitle}`,
content: `SessionSpeechlet - ${this.speechOutput}`
},
reprompt: {
outputSpeech: {
type: "PlainText",
text: this.repromptText
},
shouldEndSession: this.shouldEndSession
}
}
}
}
{
"session": {
"sessionId": "SessionId.f7d2057c-becf-4f4a-ab5d-2db3bd67bcb4",
"application": {
"applicationId": "[your application ID]"
},
"user": {
"userId": "amzn1.echo-sdk-account.AELCZLXLI3DDZ6BI4HV5XQZZFOMOIHAGHIIMRQ2WMYOLFPD2VI2CO"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.26563c45-d66b-48fb-9d5a-188ea77022e8",
"timestamp": "2016-02-24T04:21:55Z",
"intent": {
"name": "GetDefinition",
"slots": {
"Word": {
"name": "Word",
"value": "happiness"
}
}
}
}
}
/**
* Created by ericjohnson on 2/21/16.
*/
export function validateApplicationId(event, context) {
let myAppId = "amzn1.echo-sdk-ams.app.c27f3eb5-57be-47f4-ba4e-a4d2d86a70b3",
isMyApp = event.session.application.applicationId === myAppId;
console.log(`App ID ${event.session.application.applicationId} ${isMyApp ? 'matches' : 'does not match'} my app id`);
return isMyApp ? null : context.fail('This App is not the app your looking for');
}
export function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment