Amazon Lex с помощью Amazon Lambda Help для магазина мороженого - PullRequest
2 голосов
/ 05 мая 2020

В зависимости от даты, я хочу, чтобы 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;
}

1 Ответ

0 голосов
/ 16 мая 2020

Попробуйте это решение, чтобы вернуть сегодняшний аромат из списка вкусов, добавляемого последовательно в соответствии с днем ​​месяца. Текущее имя намерения необходимо изменить на панели инструментов lex. Я не использовал никаких слотов, но их можно легко добавить в новый поток, например,

  1. Какие варианты доступны? - намерение становится FetchFlavour
  2. What is сегодня / завтра / в конце месяцев / et c. аромат? - слот может быть датой Amazon, которая нуждается в проверке в обработчике лямбда.

Надеюсь, это то направление, которое вы искали.

// flavours corresponding to the days of the month
const flavours = [
  "NUTTY ELEPHANT",
  "CARAMEL CASHEW",
  "CAMPFIRE S’MORES",
  "LEMON POPPYSEED CAKE",
  "DARK SIDE",
  "DF PINA COLADA",
  "DOGWOOD MUD PIE",
  "BLACK RASPBERRY",
  "DF SUPER RAINBOW UNICORN",
  "ASKINOSIE DARK CHOCOLATE",
  "STRAWBERRY BROWNIE",
  "CHOCOLATE DECADENCE",
  "FAT ELVIS",
  "BUTTER PECAN",
  "DF NEON ETHEREAL CHIP",
  "CHERRY AMARETTO",
  "COSMIC OREO",
  "DF SINGLE ORIGIN CHOCOLATE",
  "DOUBLE DRIBBLE",
  "ORANGE CREAMSICLE",
  "STEWARTS’ CHERRY CHEESECAKE",
  "DF FUDGY COOKIES & CREAM",
  "SINGLE ORIGIN MINT CHIP",
  "PEANUT BUTTER COOKIE DOUGH",
  "BIRTHDAY CAKE!",
  "COCONUT",
  "DF STRAWBERRY",
  "DOUBLE ORIGIN CHOCOLATE",
  "PIE FIGHT",
  "SALTED CARAMEL OREO",
  "DOUGH CRAZY!",
  "HEATH BRICKLE CRUNCH",
  "SUPER RAINBOW UNICORN",
  "CHERRY GOAT CHEESE",
  "DF VEGAN CHERRY CHEESECAKE",
  "DARK CHOCOLATE AVOCADO",
  "OREO PB BLOB DUE BASI",
  "BURGUNDY CHERRY"
];

const closeResponse = (sessionAttributes, fulfillmentState, message) => {
  return {
    sessionAttributes: sessionAttributes,
    dialogAction: {
      type: "Close",
      fulfillmentState: fulfillmentState,
      message: message
    }
  };
};

// main handler
exports.handler = function(event, context) {
  process.env.TZ = "utc"; // change this to your timezone https://www.iana.org/time-zones
  console.debug(
    "ENVIRONMENT VARIABLES\n" + JSON.stringify(process.env, null, 2)
  );
  console.info("EVENT\n" + JSON.stringify(event, null, 2));

  const intentName = event.currentIntent.name;
  // please check the spelling of flavour/flavor
  if (intentName === "TodaysFlavour") {
    return closeResponse(event.sessionAttributes, "Fulfilled", {
      contentType: "PlainText",
      content: `Todays flavour is ${flavours[new Date().getDate() - 1]}`
    });
  } else {
    throw new Error(`${intentName} is currently not supported`);
  }
};
...