В зависимости от даты, я хочу, чтобы Amazon Lex дал ответ (а). Это для моего магазина мороженого. Я использую это как часть моей системы Amazon Connect, в то время как кто-то спрашивает: «Что такое сегодняшний вкус?», А Amazon Lex, например, отвечает: «Сегодняшний вкус - это Mint Chip».
У меня есть слот в Amazon Lex называется «дата».
Я работаю над лямбда-функцией и получаю сообщение об ошибке: «Произошла ошибка: недопустимый лямбда-ответ: получен ответ об ошибке от лямбда-функции: не обработано». Я знаю, что это вызвано моей неряшливой лямбда-функцией ниже.
Вот. js, который я использую в Lambda:
'use strict'
//Handler function. This is the entry point to our Lambda function
exports.handler = function (event, context, callback) {
//We obtain the sessionAttributes and the intent name from the event object, received as a parameter.
var sessionAttributes = event.sessionAttributes;
var intentName = event.currentIntent.name;
//In order to use the same lambda function for several intents, we check against the intent name, which is unique.
switch (intentName) {
case "date": //In case we triggered the date intent, we'll execute the following code:
//We obtain the 'date' slot
var name = event.currentIntent.slots.date;
//now we get the flavor of the date
getFlavorDate(name, function (error, date) {
var response = null;
if (!error) {
//By default we create a message that states that we didn't find the birthday for the given name.
var message = "I'm sorry, I couldn't find " + Date + "'s flavor.";
if (date !== null) //In case we found a date, we generate a message with the dates info
{
message = date + "'s flavor is " + date.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: undefined,
});
}
//We generate a response that has a 'Fulfilled' value for the attribute 'dialogAction.fulfillmentState' and we pass our message string
response = createFulfilledResponse(sessionAttributes, message);
}
else {
//In case an error ocurred, we pass an error message in a response that has the 'dialogAction.fulfillmentState' attribute set to 'Failed'
var message = "An error has occurred.";
response = createFailedResponse(sessionAttributes, message);
}
//Finally, we trigger the callback to notify the bot
callback(null, response);
});
break;
}
};
//Function used to get the birth date of someone's by providing their name.
//The content of this function can be replaced in order for the data to be gotten from an API, database or other service.
function getFlavorDate(name, callback) {
//We will use sample data instead of accessing an API or service, for practical purposes. This code can be reprogrammed in order to change the behavior.
var FlavorDate = {
"NUTTY ELEPHANT": new Date(2020, 1, 5),
"CARAMEL CASHEW": new Date(2020, 2, 5),
"CAMPFIRE S’MORES": new Date(2020, 3, 5),
"LEMON POPPYSEED CAKE": new Date(2020, 4, 5),
"DARK SIDE": new Date(2020, 5, 5),
"DF PINA COLADA": new Date(2020, 5, 5),
"DOGWOOD MUD PIE": new Date(2020, 6, 5),
"BLACK RASPBERRY": new Date(2020, 7, 5),
"DF SUPER RAINBOW UNICORN": new Date(2020, 7, 5),
"ASKINOSIE DARK CHOCOLATE": new Date(2020, 8, 5),
"STRAWBERRY BROWNIE": new Date(2020, 9, 5),
"CHOCOLATE DECADENCE": new Date(2020, 10, 5),
"FAT ELVIS": new Date(2020, 11, 5),
"BUTTER PECAN": new Date(2020, 12, 5),
"DF NEON ETHEREAL CHIP": new Date(2020, 12, 5),
"CHERRY AMARETTO": new Date(2020, 13, 5),
"COSMIC OREO": new Date(2020, 14, 5),
"DF SINGLE ORIGIN CHOCOLATE": new Date(2020, 15, 5),
"DOUBLE DRIBBLE": new Date(2020, 15, 5),
"ORANGE CREAMSICLE": new Date(2020, 16, 5),
"STEWARTS’ CHERRY CHEESECAKE": new Date(2020, 17, 5),
"DF FUDGY COOKIES & CREAM": new Date(2020, 18, 5),
"SINGLE ORIGIN MINT CHIP": new Date(2020, 18, 5),
"PEANUT BUTTER COOKIE DOUGH": new Date(2020, 19, 5),
"BIRTHDAY CAKE!": new Date(2020, 20, 5),
"COCONUT": new Date(52020, 21, 5),
"DF STRAWBERRY": new Date(2020, 21, 5),
"DOUBLE ORIGIN CHOCOLATE": new Date(2020, 22, 5),
"PIE FIGHT": new Date(2020, 23, 5),
"SALTED CARAMEL OREO": new Date(2020, 24, 5),
"DOUGH CRAZY!": new Date(2020, 25, 5),
"HEATH BRICKLE CRUNCH": new Date(2020, 26, 5),
"SUPER RAINBOW UNICORN": new Date(2020, 27, 5),
"CHERRY GOAT CHEESE": new Date(2020, 28, 5),
"DF VEGAN CHERRY CHEESECAKE": new Date(2020, 29, 5),
"DARK CHOCOLATE AVOCADO": new Date(2020, 30, 5),
"OREO PB BLOB DUE BASI": new Date(2020, 30, 5),
"BURGUNDY CHERRY": new Date(2020, 31, 5)
};
var FlavorDate = null;
name = name.toLowerCase(); //As our keys in the set are in lower case, we convert our 'name' parameter to lower case
if (name in FlavorDate) {
//If the name is in the set, we return the corresponding birth date.
FlavorDate = FlavorDate[name];
}
callback(0, FlavorDate); //we return the value in the callback with error code 0.
}
//Function used to generate a response object, with its attribute dialogAction.fulfillmentState set to 'Fulfilled'. It also receives the message string to be shown to the user.
function createFulfilledResponse(sessionAttributes, message) {
let response = {
"sessionAttributes": session_attributes,
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": message
}
}
}
return response;
}