将 Lex 引导至新意图的 Lambda 函数

Lambda function to direct Lex to new intent

我正在尝试使用 lambda 函数根据传入槽的值将 lex 发送到新意图:

像这样

 public override LexResponse Process(LexEvent lexEvent, ILambdaContext context)
    {
        var slots = lexEvent.CurrentIntent.Slots;
        var sessionAttributes = lexEvent.SessionAttributes ?? new Dictionary<string, string>();
        DesiredVehicleYesNo type;
        if (slots.ContainsKey("DesiredVehicleYesNo"))
        {
            Enum.TryParse(slots["DesiredVehicleYesNo"], out type);
        }
        else
        {
            type = DesiredVehicleYesNo.Null;
        }

        switch (type)
        {
            case DesiredVehicleYesNo.YES:
                Dictionary<string, string> s = new Dictionary<string, string>();
                s.Add("DesiredVehicle", null);
                //return ConfirmIntent(sessionAttributes, "DesiredVehicleYes", s, new LexResponse.LexMessage() { Content = "That's great! Let's get started.", ContentType = MESSAGE_CONTENT_TYPE });
                //return ElicitSlot(sessionAttributes,"DesiredVehicleYes",null,"DesiredVehicle", new LexResponse.LexMessage() { Content = "That's great! Let's get started.", ContentType = MESSAGE_CONTENT_TYPE });

            case DesiredVehicleYesNo.NO:
                return ConfirmIntent(sessionAttributes, "DesiredVehicleNo", new Dictionary<string,string>(), new LexResponse.LexMessage() { Content = "Well, that's ok, I can help you choose", ContentType = MESSAGE_CONTENT_TYPE });

        }

我只是不确定 return 我应该为此使用什么类型? ConfirmIntent,ElicitSlot,ElicitIntent?此外,我确实需要传回插槽,我希望新意图使用它自己的提示来填充与该意图相关的插槽。

谢谢

您应该使用 ConfirmIntent 并提供要切换到的意图的 intentNameslots

Lex Response Format Docs

ConfirmIntent — Informs Amazon Lex that the user is expected to give a yes or no answer to confirm or deny the current intent.
You must include the intentName and slots fields. The slots field must contain an entry for each of the slots configured for the specified intent. If the value of a slot is unknown, you must set it to null. You must include the message field if the intent's confirmationPrompt field is null. If you specify both the message field and the confirmationPrompt field, the response includes the contents of the confirmationPrompt field. The responseCard field is optional.

因此您可以提供自己的消息,但请确保将其写成 Yes/No 问题。因为 ConfirmIntent 会期望来自用户的 Yes/No 响应。

此方法将始终触发您在 intentName 中提供的意图。所以你必须在那里处理用户的响应。
如果用户说 "yes" 那么 confirmationStatus 将保存值 Confirmed.
如果用户说 "no" 那么 confirmationStatus 将保存值 Denied.

确保您传回的插槽对于该新意图是正确的。您可以将它们预先填充以使用用户已经给您的东西;或设置为 null 以允许新意图要求用户再次填充这些插槽。

您可以按照 Jay 的建议使用 ConfirmIntent,但是,如果您不想向用户提示任何内容,则可以使用一种 hacky 方法来实现:

  • 授予调用 lambda 调用 lambda 函数的权限 函数,即 intent-A
  • 的 lambda 函数
  • 获取intent-B的后端lambda函数名
  • 使用 boto3
  • 使用所有输入调用该 lambda 函数
  • 响应将在响应对象的'Payload'键中
  • 使用 read() 方法获取响应
  • 在 ['dialogAction']['message']['content']
  • 中获取实际输出
  • return 使用默认的 close() 方法

这是相同的示例代码:

import boto3

client = boto3.client('lambda')
data = {'messageVersion': '1.0', 'invocationSource': 'FulfillmentCodeHook', 'userId': '###', 
        'sessionAttributes': {}, 'requestAttributes': None, 
        'bot': {'name': '###', 'alias': '$LATEST', 'version': '$LATEST'}, 
        'outputDialogMode': 'Text', 
        'currentIntent': {'name': '###', 'slots': {'###': '###'}, 
        'slotDetails': {'###': {'resolutions': [], 'originalValue': '###'}}, 
        'confirmationStatus': 'None'}, 
        'inputTranscript': '###'}
response = client.invoke(
    FunctionName='{intent-B lambda function}',
    InvocationType='RequestResponse',
    Payload=json.dumps(data)
)
output = json.loads(response['Payload'].read())['dialogAction']['message']['content']

希望对您有所帮助。