Skip to content

Commit

Permalink
Updated Alexa SDK to v2 (ask-sdk)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xzya committed Jul 7, 2018
1 parent ec813a9 commit 4ccb36e
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 31 deletions.
38 changes: 38 additions & 0 deletions models/en-US.json
@@ -0,0 +1,38 @@
{
"interactionModel": {
"languageModel": {
"invocationName": "greeter",
"intents": [{
"name": "AMAZON.FallbackIntent",
"samples": []
},
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "HelloWorldIntent",
"slots": [],
"samples": [
"say hello",
"say hello world",
"hello",
"say hi",
"say hi world",
"hi",
"how are you"
]
}
],
"types": []
}
}
}
6 changes: 1 addition & 5 deletions package.json
Expand Up @@ -16,12 +16,9 @@
"author": "Mihail Cristian Dumitru",
"license": "MIT",
"dependencies": {
"alexa-sdk": "^1.0.25"
"ask-sdk": "^2.0.7"
},
"devDependencies": {
"@types/alexa-sdk": "^1.1.1",
"@types/aws-lambda": "^8.10.7",
"@types/aws-lambda-mock-context": "^1.0.3",
"@types/chai": "^4.1.4",
"@types/express": "^4.16.0",
"@types/mocha": "^5.2.4",
Expand All @@ -33,7 +30,6 @@
"serverless-plugin-typescript": "^1.1.5",
"ts-node": "^5.0.1",
"typescript": "^2.9.2",
"aws-lambda-mock-context": "^3.1.1",
"nodemon": "^1.17.5"
}
}
104 changes: 91 additions & 13 deletions src/hello/index.ts
@@ -1,20 +1,98 @@
import * as Alexa from "alexa-sdk";
import * as Alexa from "ask-sdk";

const handlers: Alexa.Handlers<Alexa.Request> = {
'LaunchRequest': function () {
this.emit('SayHello');
const LaunchRequestHandler: Alexa.RequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
'HelloWorldIntent': function () {
this.emit('SayHello');
handle(handlerInput) {
const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!';

return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}
};

const HelloWorldIntentHandler: Alexa.RequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {
const speechText = 'Hello World!';

return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}
};

const HelpIntentHandler: Alexa.RequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'You can say hello to me!';

return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}
};

const CancelAndStopIntentHandler: Alexa.RequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
'SayHello': function () {
this.emit(':tell', 'Hello World!');
handle(handlerInput) {
const speechText = 'Goodbye!';

return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}
};

const SessionEndedRequestHandler: Alexa.RequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
//any cleanup logic goes here
return handlerInput.responseBuilder.getResponse();
}
};

export const handler = (event: Alexa.RequestBody<Alexa.Request>, context: Alexa.Context, callback?: (err: any, response: any) => void) => {
const alexa = Alexa.handler(event, context);
alexa.resources = {};
alexa.registerHandlers(handlers);
alexa.execute();
const ErrorHandler: Alexa.ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);

return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};

export const handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
HelloWorldIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(ErrorHandler)
.lambda();
21 changes: 8 additions & 13 deletions src/index.ts
@@ -1,24 +1,19 @@
import * as express from "express";
import * as bodyParser from "body-parser";
import * as Alexa from "alexa-sdk";
import { AddressInfo } from "net";
import context = require("aws-lambda-mock-context");
import { LambdaHandler } from "ask-sdk-core/dist/skill/factory/BaseSkillFactory";
import { RequestEnvelope } from "ask-sdk-model";

import { handler as helloHandler } from "./hello";

function CreateHandler(handler: (event: Alexa.RequestBody<Alexa.Request>, context: Alexa.Context, callback?: (err: any, response: any) => void) => void): express.RequestHandler {
function CreateHandler(handler: LambdaHandler): express.RequestHandler {
return (req, res) => {
const ctx = context();

handler(req.body, ctx);

ctx.Promise
.then(resp => {
return res.status(200).json(resp);
})
.catch(err => {
handler(req.body as RequestEnvelope, null, (err, result) => {
if (err) {
return res.status(500).send(err);
});
}
return res.status(200).json(result);
});
};
}

Expand Down

0 comments on commit 4ccb36e

Please sign in to comment.