【问题标题】:Amazon Lex with Amazon Lambda Help for Ice Cream ShopAmazon Lex 和 Amazon Lambda 帮助冰淇淋店
【发布时间】:2020-05-05 12:44:38
【问题描述】:

基于日期,我希望 Amazon Lex 以一种风味进行响应。这是给我的冰淇淋店的。例如,我将它用作我的 Amazon Connect 系统的一部分,而有人问“今天的口味是什么”,而 Amazon Lex 说“今天的口味是薄荷片”。

我在 Amazon Lex 中有一个名为“日期”的插槽。

我正在处理 Lambda 函数并收到错误消息“发生错误:无效的 Lambda 响应:收到来自 Lambda 的错误响应:未处理”。我知道这是我下面草率的 Lambda 函数造成的。

这是我在 Lambda 中使用的 .js:

'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;
}

【问题讨论】:

  • 我想我可以解决这个问题,但整个工作流程还不清楚,你能分享来自 Alexa 开发者控制台的模型 JSON 吗?
  • Carlos,我有诸如“你今天有什么口味”、“什么口味在​{date}​”之类的话语,其中包含与 Amazon Connect 相关的上述 lambda 函数。这是关于它的。谢谢。

标签: node.js amazon-web-services aws-lambda aws-lex amazon-connect


【解决方案1】:

请尝试此解决方案,以从按月份顺序添加的风味列表中返回今天的风味。需要在 lex 仪表板上更改当前意图名称。我没有使用任何插槽,但可以轻松地将它们添加到新流程中,例如。

  1. 有哪些口味可供选择? - 意图变为 FetchFlavour
  2. 今天/明天/月末/等是什么。 flavor ?- slot 可以是亚马逊日期,需要在 lambda 处理程序上进行验证

希望这是您正在寻找的方向。

// 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`);
  }
};

【讨论】:

  • 感谢普拉蒂克!如果我们在特定日期有特定的风味,而你问:“21 日的风味是什么?”与日期不匹配时,亚马逊 Lex 如何确定风味?感谢您的努力,非常感谢。
  • @pwiz 您可以在话语中使用一个槽,例如。什么是 {date} 风格?,lex 反过来将语音转换为日期格式 ref
  • @pwiz lex 需要一些验证。您将需要处理故障情况。在 lambda 函数内部。您可以编写一个验证器,当 invocationSource 为 DialogCodeHook 时调用该验证器。如果解析日期槽引发错误,验证器可以传递一条带有 dialogAction.type 为 ElicitSlot 和违反槽以提示输入有效日期的消息。
猜你喜欢
  • 1970-01-01
  • 2012-01-11
  • 2012-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2019-04-29
相关资源
最近更新 更多