LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

[JAWS-UG CLI] Amazon Lex 入門 (4) ボットの作成

Last updated at Posted at 2017-10-08

この記事について

JAWS-UG CLI専門支部 #95 Lex入門 (秋のVUIキャンペーン第1弾)で実施するハンズオン用の手順書です。

前提条件

必要な権限

作業にあたっては、以下の権限を有したIAMユーザもしくはIAMロールを利用してください。

  • 以下のサービスの対するフルコントロール権限
    • IAM
    • Lex

0. 準備

0.1. リージョンを指定

バージニアリージョンを利用します。(リンク先にサービスが提供されているリージョンの一覧が記載されています)

API Reference

コマンド
export AWS_DEFAULT_REGION="us-east-1"

0.2. 資格情報を確認

コマンド
aws configure list

インスタンスプロファイルを設定したEC2インスタンスでアクセスキーを設定せずに実行した場合、以下のようになります。

結果
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************QSAA         iam-role
secret_key     ****************c1xY         iam-role
    region                us-west-2              env    AWS_DEFAULT_REGION

0.3. バージョン確認

コマンド
aws --version
結果
(可能な限り最新版を利用しましょう)

0.4. バージョンアップ(必要に応じて)

コマンド
sudo pip install -U awscli

0.5 変数の確認

コマンド
cat << ETX

    INTENT_NAME: ${INTENT_NAME}

ETX

1. ボットの作成

ボットを作成する際、以下のような設定を併せて行います。

  • abortStatement
    • サービス時間外の際の応答メッセージ
  • clarificationPrompt
    • Wake wordを解釈できなかった場合の応答メッセージ
  • voiceId
    • (音声インターフェースの場合)音声の種類
  • childDirected
  • idleSessionTTLInSeconds
    • セッションタイムアウト時間

1.1. ボットの定義

コマンド
BOT_FILE_NAME="OrderFlowersBot.json"
コマンド
cat << EOF > ${BOT_FILE_NAME}
{
    "intents": [
        {
            "intentVersion": "\$LATEST",
            "intentName": "${INTENT_NAME}"
        }
    ],
    "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"
}
EOF

cat ${BOT_FILE_NAME}
コマンド
jsonlint -q ${BOT_FILE_NAME}

1.2. パラメーターの指定

コマンド
BOT_NAME="OrderFlowersBot"

同名のボットが存在しないことを確認します。

コマンド
aws lex-models get-bots \
    --name-contains ${BOT_NAME}
結果
{
    "bots": []
}

1.3. ボットの作成

コマンド
aws lex-models put-bot \
    --name ${BOT_NAME} \
    --cli-input-json file://${BOT_FILE_NAME}
結果
{
    "status": "BUILDING",
    "intents": [
        {
            "intentVersion": "$LATEST",
            "intentName": "OrderFlowers"
        }
    ],
    "name": "OrderFlowersBot",
    "locale": "en-US",
    "checksum": "10b56689-2948-4b1c-968d-53cb14031a68",
    "abortStatement": {
        "messages": [
            {
                "content": "Sorry, I'm not able to assist at this time",
                "contentType": "PlainText"
            }
        ]
    },
    "version": "$LATEST",
    "lastUpdatedDate": 1507450040.22,
    "createdDate": 1507450040.22,
    "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"
}

作成したリソースを確認します。

コマンド
aws lex-models get-bots \
    --name-contains ${BOT_NAME}
結果
{
    "bots": [
        {
            "status": "BUILDING",
            "name": "OrderFlowersBot",
            "version": "$LATEST",
            "lastUpdatedDate": 1507450323.776,
            "createdDate": 1507450323.776,
            "description": "Bot to order flowers on the behalf of a user"
        }
    ]
}

最新のバージョンを確認します。

コマンド
BOT_VERSION=$(aws lex-models get-bot-versions \
    --name ${BOT_NAME} \
    --query "sort_by(bots,&lastUpdatedDate)[0].version" \
    --output text) \
    && echo ${BOT_VERSION}
結果
$LATEST

作成したリソースの内容を確認します。

コマンド
aws lex-models get-bot \
    --name ${BOT_NAME} \
    --version-or-alias ${BOT_VERSION}
結果
{
    "status": "READY",
    "intents": [
        {
            "intentVersion": "$LATEST",
            "intentName": "OrderFlowers"
        }
    ],
    "name": "OrderFlowersBot",
    "locale": "en-US",
    "checksum": "10c99268-eeb5-4357-a82c-5ec62819e9be",
    "abortStatement": {
        "messages": [
            {
                "content": "Sorry, I'm not able to assist at this time",
                "contentType": "PlainText"
            }
        ]
    },
    "version": "$LATEST",
    "lastUpdatedDate": 1507450347.659,
    "createdDate": 1507450323.776,
    "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"
}

2. エイリアスの作成

2.1. パラメーターの指定

エイリアス名を指定します。

コマンド
ALIAS="DEV"
ALIAS_DESCRIPTION="JAWSUG CLI 20171023"

作成したボットにエイリアスが作成されていないことを確認します。

コマンド
aws lex-models get-bot-aliases \
    --bot-name ${BOT_NAME}
結果
{
    "BotAliases": []
}

2.3. エイリアスの作成

エイリアスを作成します。

コマンド
aws lex-models put-bot-alias \
    --name ${ALIAS} \
    --description "${ALIAS_DESCRIPTION}" \
    --bot-name ${BOT_NAME} \
    --bot-version "${BOT_VERSION}"
結果
{
    "name": "DEV",
    "checksum": "11a4ddb4-3c8c-4ad8-a841-18d389184915",
    "lastUpdatedDate": 1508653879.681,
    "createdDate": 1508653879.681,
    "botVersion": "$LATEST",
    "botName": "OrderFlowersBot",
    "description": "JAWSUG CLI 20171023"
}

エイリアスが作成されたことを確認します。

コマンド
aws lex-models get-bot-aliases \
    --bot-name ${BOT_NAME}
結果
{
    "BotAliases": [
        {
            "name": "DEV",
            "checksum": "11a4ddb4-3c8c-4ad8-a841-18d389184915",
            "description": "JAWSUG CLI 20171023",
            "lastUpdatedDate": 1508653879.681,
            "createdDate": 1508653879.681,
            "botName": "OrderFlowersBot",
            "botVersion": "$LATEST"
        }
    ]
}

以上です。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up