

サポート終了通知: 2025 年 9 月 15 日、 AWS は Amazon Lex V1 のサポートを終了します。 V1 2025 年 9 月 15 日を過ぎると、Amazon Lex V1 コンソールまたは Amazon Lex V1 リソースにはアクセスできなくなります。Amazon Lex V2 を使用している場合は、代わりに [Amazon Lex V2 ガイド](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)を参照してください。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# ステップ 4: ご利用開始にあたって (AWS CLI)
<a name="gs-cli"></a>

このステップでは、 AWS CLI を使用して Amazon Lex ボットを作成、テスト、変更します。以下の演習を行うには、CLI の使い方を知っている必要があります。また、テキストエディタが必要です。詳細については、「[ステップ 2: をセットアップする AWS Command Line Interface](gs-set-up-cli.md)」を参照してください。
+ 演習 1 - Amazon Lex ボットを作成してテストします。この演習では、カスタムスロットタイプ、インテント、およびボットを作成するために必要なすべての JSON オブジェクトを提供します。詳細については、「[Amazon Lex: 仕組み](how-it-works.md)」を参照してください。
+ 演習 2 - 演習 1 で作成したボットを更新し、新しいサンプル発話を追加します。Amazon Lex でサンプル発話を使用し、ボットの機械学習モデルを構築します。
+ 演習 3 - 演習 1 で作成したボットを更新し、ユーザー入力を検証してインテントを達成するための Lambda 関数を追加します。
+ 演習 4 - 演習 1 で作成したスロットタイプ、インテント、およびボットリソースのバージョンを発行します。バージョンは、リソースの変更できないスナップショットです。
+ 演習 5 - 演習 1 で作成したボットのエイリアスを作成します。
+ 演習 6 - 演習 1 で作成したスロットタイプ、インテント、ボットおよび演習 5 で作成したエイリアスを削除し、アカウントをクリーンアップします。

**Topics**
+ [演習 1: Amazon Lex ボットを作成する (AWS CLI)](gs-cli-create.md)
+ [演習 2: 新しい発話を追加する (AWS CLI)](gs-cli-update-utterance.md)
+ [演習 3: Lambda 関数を追加する (AWS CLI)](gs-cli-update-lambda.md)
+ [演習 4: バージョンを発行する (AWS CLI)](gs-cli-publish.md)
+ [演習 5: エイリアスを作成する (AWS CLI)](gs-cli-create-alias.md)
+ [演習 6: クリーンアップする (AWS CLI)](gs-cli-clean-up.md)

# 演習 1: Amazon Lex ボットを作成する (AWS CLI)
<a name="gs-cli-create"></a>

通常、ボットを作成するときは、以下のことを行います。

1. スロットタイプを作成し、ボットで扱う情報を定義します。

1. インテントを作成し、ボットでサポートするユーザーアクションを定義します。前に作成したカスタムスロットタイプを使用し、インテントに必要なスロット (パラメータ) を定義します。

1. 定義したインテントを使用するボットを作成します。

この演習では、CLI を使用して新しい Amazon Lex ボットを作成してテストします。ボットの作成には、用意されている JSON 構造を使用します。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**Topics**
+ [ステップ 1: サービスにリンクされたロールを作成する (AWS CLI)](gs-create-role.md)
+ [ステップ 2: カスタムスロットタイプを作成する (AWS CLI)](gs-create-flower-types.md)
+ [ステップ 3: インテントを作成する (AWS CLI)](gs-cli-create-order-flowers.md)
+ [ステップ 4: ボットを作成する (AWS CLI)](gs-cli-create-order-flowers-bot.md)
+ [ステップ 5: ボットをテストする (AWS CLI)](gs-create-test.md)

# ステップ 1: サービスにリンクされたロールを作成する (AWS CLI)
<a name="gs-create-role"></a>

Amazon Lex は AWS Identity and Access Management 、ボットに代わって AWS サービスを呼び出すサービスにリンクされたロールを引き受けます。ロールは、アカウント内で Amazon Lex ユースケースにリンクされ、アクセス権限が事前に定義されています。詳細については、「[Amazon Lex のサービスリンクロールの使用](using-service-linked-roles.md)」を参照してください。

コンソールで Amazon Lex ボットを作成済みである場合、サービスにリンクされたロールは自動的に作成されています。「[ステップ 2: カスタムスロットタイプを作成する (AWS CLI)](gs-create-flower-types.md)」へ進んでください。

**サービスリンクロールの作成 (AWS CLI)**

1. で AWS CLI、次のコマンドを入力します。

   ```
   aws iam create-service-linked-role --aws-service-name lex.amazonaws.com
   ```

1. 次のコマンドを使用してポリシーをチェックします。

   ```
   aws iam get-role --role-name AWSServiceRoleForLexBots
   ```

   レスポンスは次のとおりです。

## 次のステップ
<a name="gs-create-next-2"></a>

[ステップ 2: カスタムスロットタイプを作成する (AWS CLI)](gs-create-flower-types.md)

# ステップ 2: カスタムスロットタイプを作成する (AWS CLI)
<a name="gs-create-flower-types"></a>

カスタムスロットタイプを作成し、注文できる花の種類を列挙値とします。次のステップで `OrderFlowers` インテントを作成するときに、このタイプを使用します。*スロットタイプ*は、インテントのスロット (パラメータ) に指定できる値を定義します。

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

**カスタムスロットタイプを作成するには (AWS CLI)**

1. **FlowerTypes.json** という名前のテキストファイルを作成します。このテキストファイル内に [FlowerTypes.json](gs-cli-create-flower-types-json.md) の JSON コードをコピーします。

1. を使用して [PutSlotType](API_PutSlotType.md)オペレーションを呼び出し AWS CLI 、スロットタイプを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws lex-models put-slot-type \
       --region region \
       --name FlowerTypes \
       --cli-input-json file://FlowerTypes.json
   ```

   サーバーからのレスポンスは次のとおりです。

   ```
   {
       "enumerationValues": [
           {
               "value": "tulips"
           }, 
           {
               "value": "lilies"
           }, 
           {
               "value": "roses"
           }
       ], 
       "name": "FlowerTypes", 
       "checksum": "checksum", 
       "version": "$LATEST", 
       "lastUpdatedDate": timestamp, 
       "createdDate": timestamp, 
       "description": "Types of flowers to pick up"
   }
   ```

## 次のステップ
<a name="gs-create-next-3"></a>

[ステップ 3: インテントを作成する (AWS CLI)](gs-cli-create-order-flowers.md)

# FlowerTypes.json
<a name="gs-cli-create-flower-types-json"></a>

次のコードは、`FlowerTypes` カスタムスロットタイプを作成するために必要な JSON データです。

```
{
    "enumerationValues": [
        {
            "value": "tulips"
        },
        {
            "value": "lilies"
        },
        {
            "value": "roses"
        }
    ],
    "name": "FlowerTypes",
    "description": "Types of flowers to pick up"
}
```

# ステップ 3: インテントを作成する (AWS CLI)
<a name="gs-cli-create-order-flowers"></a>

`OrderFlowersBot` ボットのインテントを作成し、3 つのスロット (パラメータ) を指定します。スロットを使用することで、ボットはインテントを達成できます。
+ `FlowerType` は、注文できる花の種類を指定するカスタムスロットタイプです。
+ `AMAZON.DATE` および `AMAZON.TIME` は、花の配送日時をユーザーから取得するための組み込みスロットタイプです。

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

**`OrderFlowers` インテントを作成するには (AWS CLI)**

1. **OrderFlowers.json** という名前のテキストファイルを作成します。このテキストファイル内に [OrderFlowers.json](gs-cli-create-order-flowers-json.md) の JSON コードをコピーします。

1. で AWS CLI、 [PutIntent](API_PutIntent.md)オペレーションを呼び出してインテントを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

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

   サーバーは以下のように応答します。

## 次のステップ
<a name="gs-create-next-4"></a>

[ステップ 4: ボットを作成する (AWS CLI)](gs-cli-create-order-flowers-bot.md)

# OrderFlowers.json
<a name="gs-cli-create-order-flowers-json"></a>

次のコードは、`OrderFlowers` インテントを作成するために必要な JSON データです。

```
{
    "confirmationPrompt": {
        "maxAttempts": 2,
        "messages": [
            {
                "content": "Okay, your {FlowerType} will be ready for pickup by {PickupTime} on {PickupDate}.  Does this sound okay?",
                "contentType": "PlainText"
            }
        ]
    },
    "name": "OrderFlowers",
    "rejectionStatement": {
        "messages": [
            {
                "content": "Okay, I will not place your order.",
                "contentType": "PlainText"
            }
        ]
    },
    "sampleUtterances": [
        "I would like to pick up flowers",
        "I would like to order some flowers"
    ],
    "slots": [
        {
            "slotType": "FlowerTypes",
            "name": "FlowerType",
            "slotConstraint": "Required",
            "valueElicitationPrompt": {
                "maxAttempts": 2,
                "messages": [
                    {
                        "content": "What type of flowers would you like to order?",
                        "contentType": "PlainText"
                    }
                ]
            },
            "priority": 1,
            "slotTypeVersion": "$LATEST",
            "sampleUtterances": [
                "I would like to order {FlowerType}"
            ],
            "description": "The type of flowers to pick up"
        },
        {
            "slotType": "AMAZON.DATE",
            "name": "PickupDate",
            "slotConstraint": "Required",
            "valueElicitationPrompt": {
                "maxAttempts": 2,
                "messages": [
                    {
                        "content": "What day do you want the {FlowerType} to be picked up?",
                        "contentType": "PlainText"
                    }
                ]
            },
            "priority": 2,
            "description": "The date to pick up the flowers"
        },
        {
            "slotType": "AMAZON.TIME",
            "name": "PickupTime",
            "slotConstraint": "Required",
            "valueElicitationPrompt": {
                "maxAttempts": 2,
                "messages": [
                    {
                        "content": "Pick up the {FlowerType} at what time on {PickupDate}?",
                        "contentType": "PlainText"
                    }
                ]
            },
            "priority": 3,
            "description": "The time to pick up the flowers"
        }
    ],
    "fulfillmentActivity": {
        "type": "ReturnIntent"
    },
    "description": "Intent to order a bouquet of flowers for pick up"
}
```

# ステップ 4: ボットを作成する (AWS CLI)
<a name="gs-cli-create-order-flowers-bot"></a>

`OrderFlowersBot` ボットには、1 つのインテント (前のステップで作成した `OrderFlowers` インテント) があります。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更してください。

**`OrderFlowersBot` ボットを作成するには (AWS CLI)**

1. **OrderFlowersBot.json** という名前のテキストファイルを作成します。このテキストファイル内に [OrderFlowersBot.json](gs-cli-create-order-flowers-bot-json.md) の JSON コードをコピーします。

1. で AWS CLI、 [PutBot](API_PutBot.md)オペレーションを呼び出してボットを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

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

   サーバーからのレスポンスは次のとおりです。ボットを作成または更新すると、`status` フィールドは `BUILDING` に設定されます。これは、ボットの使用準備が整っていないことを示します。ボットの使用準備が整ったことを確認するには、次のステップで [GetBot](API_GetBot.md) オペレーションを使用します。

   

1. 新しいボットの使用準備が整っているかどうかを確認するには、次のコマンドを実行します。`status` フィールドから `READY` が返されるまで、このコマンドを繰り返します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias "\$LATEST"
   ```

   レスポンス内で `status` フィールドを探します。

   ```
   {
       "status": "READY", 
       
       ...
       
   }
   ```

## 次のステップ
<a name="gs-create-next-5"></a>

[ステップ 5: ボットをテストする (AWS CLI)](gs-create-test.md)

# OrderFlowersBot.json
<a name="gs-cli-create-order-flowers-bot-json"></a>

次のコードは、Amazon Lex ボット `OrderFlowers` の構築に必要な JSON データを示しています。

```
{
    "intents": [
        {
            "intentVersion": "$LATEST",
            "intentName": "OrderFlowers"
        }
    ],
    "name": "OrderFlowersBot",
    "locale": "en-US",
    "abortStatement": {
        "messages": [
            {
                "content": "Sorry, I'm not able to assist at this time",
                "contentType": "PlainText"
            }
        ]
    },
    "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"
}
```

# ステップ 5: ボットをテストする (AWS CLI)
<a name="gs-create-test"></a>

ボットをテストするには、テキストベースまたは音声ベースのテストを使用できます。

**Topics**
+ [テキスト入力を使用してテストする (AWS CLI)](gs-create-test-text.md)
+ [音声入力を使用してボットをテストする (AWS CLI)](gs-create-test-speech.md)

# テキスト入力を使用してテストする (AWS CLI)
<a name="gs-create-test-text"></a>

 テキスト入力でボットが正しく動作することを確認するには、[PostText](API_runtime_PostText.md) オペレーションを使用します。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[ランタイム Service Quotas](gl-limits.md#gl-limits-runtime)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**テキストを使用してボットをテストするには (AWS CLI)**

1. で AWS CLI、`OrderFlowersBot`ボットとの会話を開始します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws lex-runtime post-text \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "i would like to order flowers"
   ```

   Amazon Lex は、ユーザーのインテントを認識し、次のレスポンスを返すことで会話を開始します。

   ```
   {
       "slotToElicit": "FlowerType", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "dialogState": "ElicitSlot", 
       "message": "What type of flowers would you like to order?", 
       "intentName": "OrderFlowers"
   }
   ```

1. 以下のコマンドを実行して、ボットとの会話を終了します。

   ```
   aws lex-runtime post-text \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "roses"
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "tuesday"
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "10:00 a.m."
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "yes"
   ```

    注文を確認すると、Amazon Lex はフルフィルメントレスポンスを送信して会話を完了します。

   ```
   {
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }, 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers"
   }
   ```

## 次のステップ
<a name="gs-create-next-test"></a>

[音声入力を使用してボットをテストする (AWS CLI)](gs-create-test-speech.md)

# 音声入力を使用してボットをテストする (AWS CLI)
<a name="gs-create-test-speech"></a>

音声ファイルを使用してボットをテストするには、[PostContent](API_runtime_PostContent.md) オペレーションを使用します。音声ファイルは、Amazon Polly テキスト読み上げ機能のオペレーションを使用して生成します。

この演習のコマンドを実行するには、Amazon Lex および Amazon Polly コマンドが実行されるリージョンを確認しておく必要があります。Amazon Lex のリージョンのリストについては「[ランタイム Service Quotas](gl-limits.md#gl-limits-runtime)」を参照してください。Amazon Polly でサポートされているリージョンとエンドポイントの一覧については、*Amazon Web Services 全般のリファレンス*の「[AWS リージョンとエンドポイント](https://docs.aws.amazon.com/general/latest/gr/rande.html#pol_region)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**音声入力を使用してボットをテストするには (AWS CLI)**

1. で AWS CLI、Amazon Polly を使用してオーディオファイルを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "i would like to order flowers" \
       --voice-id "Salli" \
       IntentSpeech.mpg
   ```

1. 音声ファイルを Amazon Lex に送信するには、次のコマンドを実行します。Amazon Lex は、レスポンスの音声を指定の出力ファイルに保存します。

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream IntentSpeech.mpg \
       IntentOutputSpeech.mpg
   ```

   Amazon Lex は、レスポンスで最初のスロットをリクエストします。音声レスポンスは指定の出力ファイルに保存されます。

   ```
   {
       "contentType": "audio/mpeg", 
       "slotToElicit": "FlowerType", 
       "dialogState": "ElicitSlot", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "i would like to order some flowers", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "message": "What type of flowers would you like to order?"
   }
   ```

1. バラの花束を注文するには、次の音声ファイルを作成して Amazon Lex に送信します。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "roses" \
       --voice-id "Salli" \ 
       FlowerTypeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream FlowerTypeSpeech.mpg \
       FlowerTypeOutputSpeech.mpg
   ```

1. 配達日を設定するには、次の音声ファイルを作成して Amazon Lex に送信します:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "tuesday" \
       --voice-id "Salli" \ 
       DateSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream DateSpeech.mpg \
       DateOutputSpeech.mpg
   ```

1. 配送時間を設定するには、次の音声ファイルを作成して Amazon Lex に送信します:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "10:00 a.m." \
       --voice-id "Salli" \
       TimeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream TimeSpeech.mpg \
       TimeOutputSpeech.mpg
   ```

1. 配達を確認するには、次の音声ファイルを作成して Amazon Lex に送信します。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "yes" \
       --voice-id "Salli" \
       ConfirmSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream ConfirmSpeech.mpg \
       ConfirmOutputSpeech.mpg
   ```

   配達を確認すると、Amazon Lex からインテントの達成を確認するレスポンスが送信されます。

   ```
   {
       "contentType": "text/plain;charset=utf-8", 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "yes", 
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }
   }
   ```

## 次のステップ
<a name="gs-cli-next-exercise-2"></a>

[演習 2: 新しい発話を追加する (AWS CLI)](gs-cli-update-utterance.md)

# 演習 2: 新しい発話を追加する (AWS CLI)
<a name="gs-cli-update-utterance"></a>

ユーザーからのリクエストを認識するために Amazon Lex が使用する機械学習モデルを向上するには、別のサンプル発話をボットに追加します。

新しい発話を追加するには 4 つのステップを使用します。

1. [GetIntent](API_GetIntent.md) オペレーションを使用して Amazon Lex からインテントを取得します。

1. インテントを更新します。

1. [PutIntent](API_PutIntent.md) オペレーションを使用して、更新したインテントを Amazon Lex に送り返します。

1. [GetBot](API_GetBot.md) オペレーションと [PutBot](API_PutBot.md) オペレーションを使用して、このインテントを使用するすべてのボットを再構築します。

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

`GetIntent` オペレーションからのレスポンスには、インテントの特定のリビジョンを識別する `checksum` というフィールドが含まれています。[PutIntent](API_PutIntent.md) オペレーションを使用してインテントを更新するときに、このチェックサムの値を指定する必要があります。指定しないと、次のエラーメッセージが表示されます。

```
            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.
```

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**`OrderFlowers` インテントを更新するには (AWS CLI)**

1. で AWS CLI、Amazon Lex からインテントを取得します。Amazon Lex は **OrderFlowers-V2.json.** というファイルにこの出力を送信します。

   ```
   aws lex-models get-intent \
       --region region \
       --name OrderFlowers \
       --intent-version "\$LATEST" > OrderFlowers-V2.json
   ```

1. テキストエディタで **OrderFlowers-V2.json** を開きます。

   1. `createdDate`、`lastUpdatedDate`、`version` の各フィールドを見つけて削除します。

   1. `sampleUtterances` フィールドに以下を追加します。

      ```
      I want to order flowers
      ```

   1. ファイルを保存します。

1. 次のコマンドを使用して、更新したインテントを Amazon Lex に送信します。

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

   Amazon Lex から次のレスポンスが送信されます。

インテントを更新したので、このインテントを使用するすべてのボットを再構築します。

**`OrderFlowersBot` ボットを再構築するには (AWS CLI)**

1. で AWS CLI、`OrderFlowersBot`ボットの定義を取得し、次のコマンドを使用してファイルに保存します。

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias "\$LATEST" > OrderFlowersBot-V2.json
   ```

1. テキストエディタで **OrderFlowersBot-V2.json** を開きます。`createdDate`、`lastUpdatedDate`、`status`、`version` の各フィールドを削除します。

1. テキストエディタで、ボットの定義に次の行を追加します。

   ```
   "processBehavior": "BUILD",
   ```

1. で AWS CLI、 に対して次のコマンドを実行して、ボットの新しいリビジョンを構築します。

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

   サーバーからのレスポンスは次のとおりです。

## 次のステップ
<a name="gs-cli-next-exercise-3"></a>

[演習 3: Lambda 関数を追加する (AWS CLI)](gs-cli-update-lambda.md)

# 演習 3: Lambda 関数を追加する (AWS CLI)
<a name="gs-cli-update-lambda"></a>

ユーザー入力を検証し、ユーザーのインテントを達成する Lambda 関数をボットに追加します。

Lambda 表現を追加するには 5 つのステップを使用します。

1. Lambda の [AddPermission](https://docs.aws.amazon.com/lambda/latest/dg/API_AddPermission.html) 関数を使用して `OrderFlowers` インテントを有効にし、Lambda の [Invoke](https://docs.aws.amazon.com/lambda/latest/dg/lambda-api-permissions-ref.html) オペレーションを呼び出します。

1. [GetIntent](API_GetIntent.md) オペレーションを使用して Amazon Lex からインテントを取得します。

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

1. [PutIntent](API_PutIntent.md) オペレーションを使用して、更新したインテントを Amazon Lex に送り返します。

1. [GetBot](API_GetBot.md) オペレーションと [PutBot](API_PutBot.md) オペレーションを使用して、このインテントを使用するすべてのボットを再構築します。

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

`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](API_PutIntent.md) オペレーションを使用してインテントを更新するときに、このチェックサムの値を指定する必要があります。指定しないと、次のエラーメッセージが表示されます。

```
            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 ボットを作成する (コンソール)](gs-bp.md)」の Lambda 関数を使用します。この Lambda 関数を作成する手順については、「[ステップ 3: Lambda 関数を作成する (コンソール)](gs-bp-create-lambda-function.md)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、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:*\"}}}"
   }
   ```

1. Amazon Lex からインテントを取得します。Amazon Lex は **OrderFlowers-V3.json** というファイルにこの出力を送信します。

   ```
   aws lex-models get-intent \
       --region region \
       --name OrderFlowers \
       --intent-version "\$LATEST" > OrderFlowers-V3.json
   ```

1. テキストエディターで **OrderFlowers-V3.json** ファイルを開きます。

   1. `createdDate`、`lastUpdatedDate`、`version` の各フィールドを見つけて削除します。

   1. `fulfillmentActivity` フィールドを更新します。

      ```
          "fulfillmentActivity": {
              "type": "CodeHook",
              "codeHook": {
                  "uri": "arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook",
                  "messageVersion": "1.0"
              }
          }
      ```

   1. ファイルを保存します。

1. で 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
   ```

1. テキストエディタで **OrderFlowersBot-V3.json** を開きます。`createdDate`、`lastUpdatedDate`、`status`、`version` の各フィールドを削除します。

1. テキストエディタで、ボットの定義に次の行を追加します。

   ```
   "processBehavior": "BUILD",
   ```

1. で AWS CLI、ボットの新しいリビジョンを構築します。

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

   サーバーからのレスポンスは次のとおりです。

## 次のステップ
<a name="gs-cli-next-exercise-4"></a>

[演習 4: バージョンを発行する (AWS CLI)](gs-cli-publish.md)

# 演習 4: バージョンを発行する (AWS CLI)
<a name="gs-cli-publish"></a>

次に演習 1 で作成したボットのバージョンを作成します。*バージョン*は、ボットのスナップショットです。一度作成したバージョンは変更できません。ボットのバージョンで更新できるのは `$LATEST` バージョンのみです。バージョンの詳細については、「[バージョニングとエイリアス](versioning-aliases.md)」を参照してください。

ボットのバージョンを発行する前に、そのバージョンで使用しているインテントを発行する必要があります。同様に、これらのインテントで参照しているスロットタイプを発行する必要があります。通常、ボットのバージョンを発行するには、以下の操作を行ないます。

1. [CreateSlotTypeVersion](API_CreateSlotTypeVersion.md) オペレーションを使用してスロットタイプのバージョンを発行します。

1. [CreateIntentVersion](API_CreateIntentVersion.md) オペレーションを使用してインテントのバージョンを発行します。

1. [CreateBotVersion](API_CreateBotVersion.md) オペレーションを使用してボットのバージョンを発行します。

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

**Topics**
+ [ステップ 1: スロットタイプを発行する (AWS CLI)](gs-cli-publish-slot-type.md)
+ [ステップ 2: インテントを発行する (AWS CLI)](gs-cli-publish-intent.md)
+ [ステップ 3: ボットを発行する (AWS CLI)](gs-cli-publish-bot.md)

# ステップ 1: スロットタイプを発行する (AWS CLI)
<a name="gs-cli-publish-slot-type"></a>

スロットタイプを使用するインテントのバージョンを発行する前に、そのスロットタイプのバージョンを発行する必要があります。この例では、`FlowerTypes` を発行します。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**スロットタイプを発行するには (AWS CLI)**

1. で AWS CLI、スロットタイプの最新バージョンを取得します。

   ```
   aws lex-models get-slot-type \
       --region region \
       --name FlowerTypes \
       --slot-type-version "\$LATEST"
   ```

   Amazon Lex からのレスポンスは次のとおりです。`$LATEST` バージョンの最新リビジョンのチェックサムを書き留めます。

   ```
   {
       "enumerationValues": [
           {
               "value": "tulips"
           }, 
           {
               "value": "lilies"
           }, 
           {
               "value": "roses"
           }
       ], 
       "name": "FlowerTypes", 
       "checksum": "checksum", 
       "version": "$LATEST", 
       "lastUpdatedDate": timestamp, 
       "createdDate": timestamp, 
       "description": "Types of flowers to pick up"
   }
   ```

1. スロットタイプのバージョンを発行します。前のステップで書き留めたチェックサムを使用します。

   ```
   aws lex-models create-slot-type-version \
       --region region \
       --name FlowerTypes \
       --checksum "checksum"
   ```

   Amazon Lex からのレスポンスは次のとおりです。次のステップのためにバージョン番号を書き留めます。

   ```
   {
       "version": "1", 
       "enumerationValues": [
           {
               "value": "tulips"
           }, 
           {
               "value": "lilies"
           }, 
           {
               "value": "roses"
           }
       ], 
       "name": "FlowerTypes", 
       "createdDate": timestamp, 
       "lastUpdatedDate": timestamp, 
       "description": "Types of flowers to pick up"
   }
   ```

## 次のステップ
<a name="gs-cli-publish-2"></a>

[ステップ 2: インテントを発行する (AWS CLI)](gs-cli-publish-intent.md)

# ステップ 2: インテントを発行する (AWS CLI)
<a name="gs-cli-publish-intent"></a>

インテントを発行する前に、そのインテントで参照しているすべてのスロットタイプを発行する必要があります。スロットタイプは、`$LATEST` バージョンではなく、番号が付いたバージョンであることが必要です。

まず、`OrderFlowers` インテントを更新し、前のステップで発行した `FlowerTypes` スロットタイプのバージョンを使用します。次に、`OrderFlowers` インテントの新しいバージョンを発行します。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**インテントのバージョンを発行するには (AWS CLI)**

1. で AWS CLI、イン`OrderFlowers`テント`$LATEST`のバージョンを取得し、ファイルに保存します。

   ```
   aws lex-models get-intent \
       --region region \
       --name OrderFlowers \
       --intent-version "\$LATEST" > OrderFlowers_V4.json
   ```

1. テキストエディタで、**OrderFlowers\$1V4.json** ファイルを開きます。`createdDate`、`lastUpdatedDate`、`version` の各フィールドを削除します。`FlowerTypes` スロットタイプを見つけ、そのバージョンを前のステップで書き留めたバージョン番号に変更します。以下は、**OrderFlowers\$1V4.json** ファイルの中で変更箇所を示す部分です。

   ```
           {
               "slotType": "FlowerTypes", 
               "name": "FlowerType", 
               "slotConstraint": "Required", 
               "valueElicitationPrompt": {
                   "maxAttempts": 2, 
                   "messages": [
                       {
                           "content": "What type of flowers?", 
                           "contentType": "PlainText"
                       }
                   ]
               }, 
               "priority": 1, 
               "slotTypeVersion": "version", 
               "sampleUtterances": []
           },
   ```

1. で AWS CLI、インテントのリビジョンを保存します。

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

1. インテントの最新リビジョンのチェックサムを取得します。

   ```
   aws lex-models get-intent \
       --region region \
       --name OrderFlowers \
       --intent-version "\$LATEST" > OrderFlowers_V4a.json
   ```

   以下は、レスポンスの中でインテントのチェックサムを示す部分です。次のステップのために、これを書き留めます。

   ```
       "name": "OrderFlowers", 
       "checksum": "checksum", 
       "version": "$LATEST",
   ```

1. インテントの新しいバージョンを発行します。

   ```
   aws lex-models create-intent-version \
       --region region \
       --name OrderFlowers \
       --checksum "checksum"
   ```

   以下は、レスポンスの中でインテントの新しいバージョンを示す部分です。次のステップのためにバージョン番号を書き留めます。

   ```
       "name": "OrderFlowers", 
       "checksum": "checksum", 
       "version": "version",
   ```

## 次のステップ
<a name="gs-cli-publish-3"></a>

[ステップ 3: ボットを発行する (AWS CLI)](gs-cli-publish-bot.md)

# ステップ 3: ボットを発行する (AWS CLI)
<a name="gs-cli-publish-bot"></a>

ボットで使用しているすべてのスロットタイプとインテントを発行したら、次にボットを発行できます。

`OrderFlowersBot` ボットを更新し、前のステップで更新した `OrderFlowers` インテントを使用します。次に、`OrderFlowersBot` ボットの新しいバージョンを発行します。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**ボットのバージョンを発行するには (AWS CLI)**

1. で AWS CLI、`OrderFlowersBot`ボット`$LATEST`のバージョンを取得し、ファイルに保存します。

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias "\$LATEST" > OrderFlowersBot_V4.json
   ```

1. テキストエディタで、**OrderFlowersBot\$1V4.json** ファイルを開きます。`createdDate`、`lastUpdatedDate`、`status`、`version` の各フィールドを削除します。`OrderFlowers` インテントを見つけ、そのバージョンを前のステップで書き留めたバージョン番号に変更します。以下は、**OrderFlowersBot\$1V4.json** の中で変更箇所を示す部分です。

   ```
       "intents": [
           {
               "intentVersion": "version", 
               "intentName": "OrderFlowers"
           }
   ```

1. で AWS CLI、ボットの新しいリビジョンを保存します。`put-bot` の呼び出しによって返されるバージョン番号をメモしておきます。

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

1. ボットの最新リビジョンのチェックサムを取得します。ステップ 3 で返されたバージョン番号を使用します。

   ```
   aws lex-models get-bot \
       --region region \
       --version-or-alias version \
       --name OrderFlowersBot > OrderFlowersBot_V4a.json
   ```

   以下は、レスポンス内のボットのチェックサムを示す部分です。次のステップのために、これを書き留めます。

   ```
       "name": "OrderFlowersBot", 
       "locale": "en-US", 
       "checksum": "checksum",
   ```

1. ボットの新しいバージョンを発行します。

   ```
   aws lex-models create-bot-version \
       --region region \
       --name OrderFlowersBot \
       --checksum "checksum"
   ```

   以下は、レスポンス内でボットの新しいバージョンを示す部分です。

   ```
       "checksum": "checksum", 
       "abortStatement": {
           ...
       }, 
       "version": "1",
       "lastUpdatedDate": timestamp,
   ```

## 次のステップ
<a name="gs-cli-next-exercise-5"></a>

[演習 5: エイリアスを作成する (AWS CLI)](gs-cli-create-alias.md)

# 演習 5: エイリアスを作成する (AWS CLI)
<a name="gs-cli-create-alias"></a>

エイリアスはボットの特定バージョンを参照するポインタです。エイリアスを使用すると、クライアントアプリケーションで使用しているバージョンを簡単に更新できます。詳細については「[バージョニングとエイリアス](versioning-aliases.md)」をご覧ください。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**エイリアスを作成するには (AWS CLI)**

1. で AWS CLI、 `OrderFlowersBot` で作成したボットのバージョンを取得します[演習 4: バージョンを発行する (AWS CLI)](gs-cli-publish.md)。

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias version > OrderFlowersBot_V5.json
   ```

1. テキストエディタで **OrderFlowersBot\$1v5.json** を開きます。バージョン番号を見つけて書き留めます。

1. で AWS CLI、ボットエイリアスを作成します。

   ```
   aws lex-models put-bot-alias  \
       --region region \
       --name PROD \
       --bot-name OrderFlowersBot \
       --bot-version version
   ```

   サーバーからのレスポンスは次のとおりです。

   ```
   {
       "name": "PROD",
       "createdDate": timestamp,
       "checksum": "checksum",
       "lastUpdatedDate": timestamp,
       "botName": "OrderFlowersBot",
       "botVersion": "1"
   }}
   ```

## 次のステップ
<a name="gs-cli-next-exercise-6"></a>

[演習 6: クリーンアップする (AWS CLI)](gs-cli-clean-up.md)

# 演習 6: クリーンアップする (AWS CLI)
<a name="gs-cli-clean-up"></a>

作成したリソースを削除し、アカウントをクリーンアップします。

削除できるのは、使用中ではないリソースだけです。通常、以下の順序でリソースを削除します。

1. エイリアスを削除して、ボットのリソースを解放します。

1. ボットを削除して、インテントのリソースを解放します。

1. インテントを削除して、スロットタイプのリソースを解放します。

1. スロットタイプを削除します。

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

**アカウントをクリーンアップするには (AWS CLI)**

1.  AWS CLI コマンドラインで、エイリアスを削除します。

   ```
   aws lex-models delete-bot-alias \
       --region region \
       --name PROD \
       --bot-name OrderFlowersBot
   ```

1.  AWS CLI コマンドラインで、ボットを削除します。

   ```
   aws lex-models delete-bot \
       --region region \
       --name OrderFlowersBot
   ```

1.  AWS CLI コマンドラインで、インテントを削除します。

   ```
   aws lex-models delete-intent \
       --region region \
       --name OrderFlowers
   ```

1.  AWS CLI コマンドラインから、スロットタイプを削除します。

   ```
   aws lex-models delete-slot-type \
       --region region \
       --name FlowerTypes
   ```

これで、作成したすべてのリソースが削除され、アカウントがクリーンアップされました。