updated lambda/index.js.

This commit is contained in:
AlexaHostedSkills 2024-07-15 20:49:04 +00:00
parent f876574e8f
commit 78914622d8

View File

@ -4,17 +4,13 @@
* session persistence, api calls, and more. * session persistence, api calls, and more.
* */ * */
const Alexa = require('ask-sdk-core'); const Alexa = require('ask-sdk-core');
// i18n library dependency, we use it below in a localisation interceptor
const i18n = require('i18next');
// i18n strings for all supported locales
const languageStrings = require('./languageStrings');
const LaunchRequestHandler = { const LaunchRequestHandler = {
canHandle(handlerInput) { canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'; return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
}, },
handle(handlerInput) { handle(handlerInput) {
const speakOutput = handlerInput.t('WELCOME_MSG'); const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -29,7 +25,7 @@ const HelloWorldIntentHandler = {
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent'; && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
}, },
handle(handlerInput) { handle(handlerInput) {
const speakOutput = handlerInput.t('HELLO_MSG'); const speakOutput = 'Hello World!';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -44,7 +40,7 @@ const HelpIntentHandler = {
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent'; && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
}, },
handle(handlerInput) { handle(handlerInput) {
const speakOutput = handlerInput.t('HELP_MSG'); const speakOutput = 'You can say hello to me! How can I help?';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -60,7 +56,7 @@ const CancelAndStopIntentHandler = {
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent'); || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
}, },
handle(handlerInput) { handle(handlerInput) {
const speakOutput = handlerInput.t('GOODBYE_MSG'); const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -78,7 +74,7 @@ const FallbackIntentHandler = {
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent'; && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
}, },
handle(handlerInput) { handle(handlerInput) {
const speakOutput = handlerInput.t('FALLBACK_MSG'); const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -112,7 +108,7 @@ const IntentReflectorHandler = {
}, },
handle(handlerInput) { handle(handlerInput) {
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope); const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = handlerInput.t('REFLECTOR_MSG', {intentName: intentName}); const speakOutput = `You just triggered ${intentName}`;
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -130,8 +126,8 @@ const ErrorHandler = {
return true; return true;
}, },
handle(handlerInput, error) { handle(handlerInput, error) {
const speakOutput = handlerInput.t('ERROR_MSG'); const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
console.log(`~~~~ Error handled: ${JSON.stringify(error.stack)}`); console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
return handlerInput.responseBuilder return handlerInput.responseBuilder
.speak(speakOutput) .speak(speakOutput)
@ -140,17 +136,6 @@ const ErrorHandler = {
} }
}; };
// This request interceptor will bind a translation function 't' to the handlerInput
const LocalisationRequestInterceptor = {
process(handlerInput) {
i18n.init({
lng: Alexa.getLocale(handlerInput.requestEnvelope),
resources: languageStrings
}).then((t) => {
handlerInput.t = (...args) => t(...args);
});
}
};
/** /**
* This handler acts as the entry point for your skill, routing all request and response * This handler acts as the entry point for your skill, routing all request and response
* payloads to the handlers above. Make sure any new handlers or interceptors you've * payloads to the handlers above. Make sure any new handlers or interceptors you've
@ -167,7 +152,5 @@ exports.handler = Alexa.SkillBuilders.custom()
IntentReflectorHandler) IntentReflectorHandler)
.addErrorHandlers( .addErrorHandlers(
ErrorHandler) ErrorHandler)
.addRequestInterceptors(
LocalisationRequestInterceptor)
.withCustomUserAgent('sample/hello-world/v1.2') .withCustomUserAgent('sample/hello-world/v1.2')
.lambda(); .lambda();