Setting up the Chatbot Server
To avoid unnecessary complications, perform the steps in the following order:
- Clone the repository
- Set up an Amazon DynamoDB instance
- Set up the AWS Lambda function
- Link the Lambda function to DynamoDB
- Set up the Amazon Lex Model
Clone the Repository
The following example shows how to clone the github.com/elasticpath/lex-chatbot
repository and install the project dependencies.
# Clone the Git repository
git clone https://github.com/elasticpath/lex-chatbot.git
# Go into the cloned directory
cd lex-chatbot/ep-lambda-function
# Install all the dependencies for the project
npm install
Set up an Amazon DynamoDB Instance
The Reference Chatbot uses a simple Amazon-hosted noSQL database instance to track purchase state across multiple active devices.
- In the AWS console, go to the DynamoDB service.
- Create a new table.
- Set the name to
DynamoCache
. - Set the primary key to
responseId
. - In the
Table Overview
, locate theTime to Live Attribute
and clickEnable TTL
. - Set the
TTL attribute
tottl
and click Continue.
Set up the AWS Lambda Function
You must have a valid Elastic Path Commerce platform. The platform is used for the backend of the lambda.
- Zip up the contents of the current directory, including the
node_modules
folder. - In the Amazon Lambda Console, create a new Function called
EPConversationalLambda
. - Select Author From Scratch template.
- Choose
Node.js 10.x
as the runtime language. - Under the Function Code pane, change Code Entry Type to
Upload a .zip folder
. - Locate and upload the newly zipped folder.
- Under Environment Variables, add and set the following values then save the function:
Key | Value |
---|---|
CACHE_TABLE | DynamoCache |
CACHE_REGION | <Your_AWS_Region> |
SCOPE | <Your_Cortex_Scope> |
CORTEX_URL | <Your_Cortex_URL> |
HOST_URL | <Your_Host_Service_URL> |
SKU_IMAGES_URL | <Your_SKU_Images_URL> |
ROLE | PUBLIC |
GRANT_TYPE | password |
USERNAME | [empty] |
PASSWORD | [empty] |
Link the Lambda Function to DynamoDB
Link the lambda function to DynamoDB by using IAM (Identity and Access Management) to read and write to the database table. This action preserves the state of the transaction.
- In the AWS Lambda Designer, click the Key icon to view Permissions.
- In the Execution Role pane, find the
roleName
value. Copy the value to the clipboard. - Browse Amazon Services, and search for IAM (Identity and Access Management).
- In the IAM Console, go to the AWS Account field, select
Roles
. - In the search bar, paste the
roleName
and select theEPConversationalLambda
role. - In the
Permissions
tab, select theAWSLambdaBasicExecutionRole
. - Click
Edit Policy
. - Click
Add additional permissions
and select DynamoDB as the service. - Under the Actions drop-down list, enable Access level for
List
,Read
, andWrite
. - Under the Resources drop-down list, enable All resources.
- Click Review Policy.
- Ensure the DynamoDB Access level is accurate.
- Click to save your changes.
Set up the Amazon Lex Model
Import the Lex Model into Amazon Lex and then set the fulfillment logic.
- Go to the
lex-chatbot/ep-lex-models
directory. - Add the
lex-model.json
file to a zip folder. - Go to the Amazon Lex console to view your bots. Note: You must have at least one bot available to import an existing model. Create one of the pre-existing bots as a sample to access this feature.
- Under the Actions drop-down list, select
Import
. - Upload the
lex-model.zip
file. - Set the Fulfillment logic to
AWS Lambda function
, and select your function from the drop-down list. Save the Intent. - Repeat step 6 for each Intent.