演習 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 表現を追加するには 5 つのステップを使用します。

  1. Lambda の AddPermission 関数を使用して OrderFlowers インテントを有効にし、Lambda の Invoke オペレーションを呼び出します。

  2. GetIntent オペレーションを使用して Amazon Lex からインテントを取得します。

  3. Lambda 関数を追加してインテントを更新します。

  4. PutIntent オペレーションを使用して、更新したインテントを Amazon Lex に送り返します。

  5. GetBot オペレーションと PutBot オペレーションを使用して、このインテントを使用するすべてのボットを再構築します。

この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「 モデル構築のクォータ 」を参照してください。

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 の例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、"\$LATEST"$LATEST に変更してください。

Lambda 関数をインテントに追加するには
  1. AWS CLI で InvokeFunction アクセス権限を OrderFlowers インテントに追加します。

    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)