练习 3:添加 Lambda 函数 (AWS CLI) - Amazon Lex V1

如果您使用的是 Amazon Lex V2,请改为参阅 Amazon Lex V2 指南

 

如果您使用的是 Amazon Lex V1,我们建议您将机器人升级到 Amazon Lex V2。我们不再向 V1 添加新功能,强烈建议使用 V2 以获得全新的机器人。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

练习 3:添加 Lambda 函数 (AWS CLI)

向机器人添加用于验证用户输入和履行用户意图的 Lambda 函数。

添加 Lambda 表达式的过程分为五步。

  1. 使用 Lambda AddPermission 函数以允许 OrderFlowers 意图调用 Lambda Invoke 操作。

  2. 使用 GetIntent 操作从 Amazon Lex 获取意图。

  3. 更新意图以添加 Lambda 函数。

  4. 使用 PutIntent 操作将更新后的意图发送回 Amazon Lex。

  5. 使用 GetBotPutBot 操作重建使用该目的的所有自动程序。

要运行本练习中的命令,您需要知道将在其中运行命令的区域。有关区域列表,请参阅 模型构建配额

如果您在添加 InvokeFunction 权限前向意图添加 Lambda 函数,会得到以下错误消息:

            An error occurred (BadRequestException) when calling the 
            PutIntent operation: Lex is unable to access the Lambda 
            function Lambda function ARN in the context of intent 
            intent ARN.  Please check the resource-based policy on 
            the function.
        

来自 GetIntent 操作的响应包含一个名为 checksum 的字段,该字段标识目的的一个特定修订版。在使用 PutIntent 操作更新目的时必须提供校验和值。如果不提供,您会收到以下错误消息:

            An error occurred (PreconditionFailedException) when calling 
            the PutIntent operation: Intent intent name already exists. 
            If you are trying to update intent name you must specify the 
            checksum.
        

本练习使用 练习 1:使用蓝图创建 Amazon Lex 机器人(控制台) 中的 Lambda 函数。有关创建 Lambda 函数的说明,请参阅步骤 3:创建 Lambda 函数(控制台)

注意

以下 AWS CLI 示例针对 Linux、Unix 和 macOS 编排了格式。对于 Windows,请将 "\$LATEST" 改为 $LATEST

向意图添加 Lambda 函数
  1. 在 AWS CLI 中,为 OrderFlowers 目的添加 InvokeFunction 权限:

    aws lambda add-permission \ --region region \ --function-name OrderFlowersCodeHook \ --statement-id LexGettingStarted-OrderFlowersBot \ --action lambda:InvokeFunction \ --principal lex.amazonaws.com \ --source-arn "arn:aws:lex:region:account ID:intent:OrderFlowers:*" --source-account account ID

    Lambda 会发送以下响应:

    {
        "Statement": "{\"Sid\":\"LexGettingStarted-OrderFlowersBot\",
          \"Resource\":\"arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook\",
          \"Effect\":\"Allow\",
          \"Principal\":{\"Service\":\"lex.amazonaws.com\"},
          \"Action\":[\"lambda:InvokeFunction\"],
          \"Condition\":{\"StringEquals\":
            {\"AWS:SourceAccount\": \"account ID\"},
            {\"AWS:SourceArn\":
              \"arn:aws:lex:region:account ID:intent:OrderFlowers:*\"}}}"
    }
  2. 从 Amazon Lex 获取意图。Amazon Lex 将输出发送到一个名为 OrderFlowers-V3.json 的文件。

    aws lex-models get-intent \ --region region \ --name OrderFlowers \ --intent-version "\$LATEST" > OrderFlowers-V3.json
  3. 在文本编辑器中,打开 OrderFlowers-V3.json

    1. 找到并删除 createdDatelastUpdatedDateversion 字段。

    2. 更新 fulfillmentActivity 字段:

      "fulfillmentActivity": { "type": "CodeHook", "codeHook": { "uri": "arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook", "messageVersion": "1.0" } }
    3. 保存该文件。

  4. 在 AWS CLI 中,将更新后的意图发送给 Amazon Lex:

    aws lex-models put-intent \ --region region \ --name OrderFlowers \ --cli-input-json file://OrderFlowers-V3.json

现在您已更新了目的,接下来要重建自动程序。

重建 OrderFlowersBot 自动程序
  1. 在 AWS CLI 中,获取 OrderFlowersBot 自动程序的定义并将其保存到一个文件中:

    aws lex-models get-bot \ --region region \ --name OrderFlowersBot \ --version-or-alias "\$LATEST" > OrderFlowersBot-V3.json
  2. 在文本编辑器中,打开 OrderFlowersBot-V3.json。删除 createdDatelastUpdatedDatestatusversion 字段。

  3. 在文本编辑器中,将下面一行添加到自动程序定义中:

    "processBehavior": "BUILD",
  4. 在 AWS CLI 中,构建自动程序的新修订版:

    aws lex-models put-bot \ --region region \ --name OrderFlowersBot \ --cli-input-json file://OrderFlowersBot-V3.json

    服务器的响应如下:

    {
        "status": "READY", 
        "intents": [
            {
                "intentVersion": "$LATEST", 
                "intentName": "OrderFlowers"
            }
        ], 
        "name": "OrderFlowersBot", 
        "locale": "en-US", 
        "checksum": "checksum", 
        "abortStatement": {
            "messages": [
                {
                    "content": "Sorry, I'm not able to assist at this time", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "version": "$LATEST", 
        "lastUpdatedDate": timestamp, 
        "createdDate": timestamp, 
        "clarificationPrompt": {
            "maxAttempts": 2, 
            "messages": [
                {
                    "content": "I didn't understand you, what would you like to do?", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "voiceId": "Salli", 
        "childDirected": false, 
        "idleSessionTTLInSeconds": 600, 
        "description": "Bot to order flowers on the behalf of a user"
    }
    

下一个步骤

练习 4:发布版本 (AWS CLI)