This commit is contained in:
rca 2024-07-16 03:43:12 +09:00
parent 9d3028f829
commit 47966782e9

View File

@ -1,89 +1,132 @@
import { HandlerInput, RequestHandler, ErrorHandler, SkillBuilders } from 'ask-sdk-core'; import * as Alexa from 'ask-sdk-core';
import { SessionEndedRequest } from 'ask-sdk-model'; import { RequestHandler, ErrorHandler, SkillBuilders } from 'ask-sdk-core';
const launchRequestHandler: RequestHandler = { import { Response } from 'ask-sdk-model';
canHandle(handlerInput: HandlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; const LaunchRequestHandler: RequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
}, },
handle(handlerInput: HandlerInput) { handle(handlerInput) {
const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!'; const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speechText) .speak(speakOutput)
.reprompt(speechText) .reprompt(speakOutput)
.withSimpleCard('Hello World', speechText)
.getResponse(); .getResponse();
}, }
}; };
const helloWorldIntentHandler: RequestHandler = {
canHandle(handlerInput: HandlerInput) { const HelloWorldIntentHandler: RequestHandler = {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' canHandle(handlerInput) {
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent'; return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
}, },
handle(handlerInput: HandlerInput) { handle(handlerInput) {
const speechText = 'Hello World!'; const speakOutput = 'Hello World!';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speechText) .speak(speakOutput)
.withSimpleCard('Hello World', speechText) //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse(); .getResponse();
}, }
}; };
const helpIntentHandler: RequestHandler = {
canHandle(handlerInput: HandlerInput) { const HelpIntentHandler: RequestHandler = {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' canHandle(handlerInput) {
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'; return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
}, },
handle(handlerInput: HandlerInput) { handle(handlerInput) {
const speechText = 'You can say hello to me!'; const speakOutput = 'You can say hello to me! How can I help?';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speechText) .speak(speakOutput)
.reprompt(speechText) .reprompt(speakOutput)
.withSimpleCard('Hello World', speechText)
.getResponse(); .getResponse();
}, }
}; };
const cancelAndStopIntentHandler: RequestHandler = {
canHandle(handlerInput: HandlerInput) { const CancelAndStopIntentHandler: RequestHandler = {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' canHandle(handlerInput) {
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent' return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent'); && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
}, },
handle(handlerInput: HandlerInput) { handle(handlerInput) {
const speechText = 'Goodbye!'; const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speechText) .speak(speakOutput)
.withSimpleCard('Hello World', speechText)
.getResponse(); .getResponse();
}, }
}; };
const sessionEndedRequestHandler: RequestHandler = {
canHandle(handlerInput: HandlerInput) { const FallbackIntentHandler: RequestHandler = {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest'; canHandle(handlerInput) {
}, return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
handle(handlerInput: HandlerInput) { && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
const { reason } = handlerInput.requestEnvelope.request as SessionEndedRequest;
console.log(`Session ended with reason: ${reason}`);
return handlerInput.responseBuilder.getResponse();
}, },
handle(handlerInput) {
const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
}; };
const errorHandler: ErrorHandler = {
const SessionEndedRequestHandler: RequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse(); // notice we send an empty response
}
};
const IntentReflectorHandler: RequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
},
handle(handlerInput) {
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = `You just triggered ${intentName}`;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
const ErrorHandler: ErrorHandler = {
canHandle() { canHandle() {
return true; return true;
}, },
handle(handlerInput: HandlerInput, error: Error) { handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`); const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.') .speak(speakOutput)
.reprompt('Sorry, I can\'t understand the command. Please say again.') .reprompt(speakOutput)
.getResponse(); .getResponse();
}, }
}; };
const skillBuilder = SkillBuilders.custom();
exports.handler = skillBuilder export const handler = SkillBuilders.custom()
.addRequestHandlers( .addRequestHandlers(
launchRequestHandler, LaunchRequestHandler,
helloWorldIntentHandler, HelloWorldIntentHandler,
helpIntentHandler, HelpIntentHandler,
cancelAndStopIntentHandler, CancelAndStopIntentHandler,
sessionEndedRequestHandler, FallbackIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler
) )
.addErrorHandlers(errorHandler) .addErrorHandlers(ErrorHandler)
.withCustomUserAgent('sample/hello-world/v1.2')
.lambda(); .lambda();