ステップ 1: Lambda 関数を作成する - Amazon Lex V1

Amazon Lex V2 を使用している場合は、代わりに Amazon Lex V2 ガイドを参照してください。

 

Amazon Lex V1 を使用している場合は、ボットを Amazon Lex V2 にアップグレードすることをお勧めします。V1 には新機能を追加されませんので、すべての新しいボットには V2 を使用することを強くお勧めします。

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

ステップ 1: Lambda 関数を作成する

最初に、ピザの注文を達成する Lambda 関数を作成します。この関数は、次のセクションで作成する Amazon Lex ボットで指定します。

Lambda 関数を作成するには

  1. AWS Management Console にサインインして AWS Lambda コンソール (https://console.aws.amazon.com/lambda/) を開きます。

  2. [Create function] を選択します。

  3. [Create function] ページで、[Author from scratch] を選択します。

    この演習では、事前に用意されたカスタムコードを使用して Lambda 関数を作成します。したがって、最初から関数を作成するオプションを選択します。

    次のコマンドを実行します

    1. 名前 (PizzaOrderProcessor) を入力します。

    2. [ランタイム] で、最新バージョンの Node.js を選択します。

    3. [Role] で、[Create a new role from template(s)] を選択します。

    4. 新しいロール名 (PizzaOrderProcessorRole) を入力します。

    5. [Create function] (関数の作成) を選択します。

  4. [関数] ページで、以下の作業を行います。

    [Function code] セクションで、[Edit code inline] を選択し、次の Node.js 関数コードをコピーしてウィンドウに貼り付けます。

    'use strict'; // Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes") function close(sessionAttributes, fulfillmentState, message) { return { sessionAttributes, dialogAction: { type: 'Close', fulfillmentState, message, }, }; } // --------------- Events ----------------------- function dispatch(intentRequest, callback) { console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`); const sessionAttributes = intentRequest.sessionAttributes; const slots = intentRequest.currentIntent.slots; const crust = slots.crust; const size = slots.size; const pizzaKind = slots.pizzaKind; callback(close(sessionAttributes, 'Fulfilled', {'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${pizzaKind} pizza on ${crust} crust`})); } // --------------- Main handler ----------------------- // Route the incoming request based on intent. // The JSON body of the request is provided in the event slot. export const handler = (event, context, callback) => { try { dispatch(event, (response) => { callback(null, response); }); } catch (err) { callback(err); } };
  5. [Save (保存)] を選択します。

サンプルイベントデータを使用した Lambda 関数のテスト

コンソールで、サンプルイベントデータを使用して手動で Lambda 関数を呼び出すことで、この関数をテストします。

Lambda 関数をテストするには:
  1. AWS Management Console にサインインして AWS Lambda コンソール (https://console.aws.amazon.com/lambda/) を開きます。

  2. [Lambda function] ページで、[Lambda function] (Lambda 関数) (PizzaOrderProcessor).) を選択します。

  3. 関数のページで、テストイベントのリストから [Configure test events] を選択します。

  4. [Configure test event] ページで、以下の操作を行います。

    1. [Create new test event] を選択します。

    2. [Event name] フィールドに、イベント名 (PizzaOrderProcessorTest) を入力します。

    3. 次の Amazon Lex イベントをウィンドウ内にコピーします。

      { "messageVersion": "1.0", "invocationSource": "FulfillmentCodeHook", "userId": "user-1", "sessionAttributes": {}, "bot": { "name": "PizzaOrderingApp", "alias": "$LATEST", "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderPizza", "slots": { "size": "large", "pizzaKind": "meat", "crust": "thin" }, "confirmationStatus": "None" } }
  5. [Create] (作成) を選択します。

AWS Lambda によってテストが作成され、関数のページが再び表示されます。[Test] (テスト) を選択すると、Lambda 関数が実行されます。

結果ボックスで、[Details] を選択します。コンソールの [Execution result] ペインに、次の出力が表示されます。

{ "sessionAttributes": {}, "dialogAction": { "type": "Close", "fulfillmentState": "Fulfilled", "message": { "contentType": "PlainText", "content": "Okay, I have ordered your large meat pizza on thin crust." } }

次のステップ

ステップ 2: ボットを作成する