Table of contents
- Prerequisite :
- Basic knowledge of AWS services.
- Step 1 : Creating a bot with Amazon Lex
- Step 2 : Testing your bot
- Step 3 : Creating an AWS Lambda function
- Step 4 : Testing your Lambda function
- Step 5 : Updating the intent of your bot
- Step 6 : Building and testing your bot
- Incorporating your bot into a webpage
Prerequisite :
Basic knowledge of AWS services.
Step 1 : Creating a bot with Amazon Lex
\=> On the AWS Management Console, on the Search, Write Amazon Lex and choose Amazon Lex.
\=> On the Amazon Lex page, select Return to the V1 console, then click on Create.
\=> On the Create your bot page, choose the ScheduleAppointment
blueprint.
\=> For the Language section, select English (US).
\=> For the COPPA section, select No, and Click on Create.
Step 2 : Testing your bot
\=> When you see the status Ready Build complete, test your bot by entering the following values :
Test your bot for example use below text to test your bot .
I would like to make an appointment
virtual meeting
4/4/1970
4:00 PM
Yes
Here we saw that our bot booked our appointment on date 4/4/1970 this is not possible.
So now we have to Create Lambda Function to make our Bot more powerful
Step 3 : Creating an AWS Lambda function
\=> On the AWS Management Console, on the Search, Write Lambda and choose Lambda.
\=> Choose Create a Function.
\=> Choose the Use a blueprint tab.
\=> In the search box, filter for Amazon Lex blueprints by entering Lex
. Select the Make an appointment with Lex python3.8 blueprint
\=> For the Function name, enter MakeAppointmentCodeHook
.
\=> For the Execution role, Select Create a new role from AWS policy templates
For the Role name, enter myLexrole
.
\=> Choose to Create function.
When your function is ready, it should be in the Lamba Designer window
Step 4 : Testing your Lambda function
\=> On the Designer page, choose Test.
\=> On the Configure test event page, for the name of the event, enter MyMakeAppointmentEvent
\=> Choose to Save.
\=> Choose Test.
You see the message: Execution result: succeeded. You can examine the details of the test by expanding the Details section.
Step 5 : Updating the intent of your bot
Each bot you create in Amazon Lex has an intent. The intent is an action that the bot will fulfill. In this task, you will update the intent to use the AWS Lambda function that you created.
\=> On the AWS Management Console, on the Search, Write Amazon Lex and choose Amazon Lex.
\=> From the Bots list, select the ScheduleAppointment bot that you created in Step 1.
\=> Expand the Lambda initialization and validation section.
You should get a message that you are going to give Amazon Lex permission to invoke your Lambda function.
\=> Choose OK.
\=> Select the Initialization and validation code hook option.
\=> From the dropdown menu, select MakeAppointmentCodeHook.
In addition to adding the code for initializing the bot, you must also add code to fulfill the request.
\=> In the Fulfillment section, choose the AWS Lambda function radio button.
\=> From the dropdown list of Lambda functions, select MakeAppointmentCodeHook.
\=> Choose Save Intent.
Step 6 : Building and testing your bot
Now, you will test your bot to make sure that it uses the Lambda function.
\=> Select Build.
You are prompted with a message that says that you can continue to edit your bot while it builds.
\=> Select Build
After the build is complete, you should receive a confirmation that your bot is successfully build.
\=> Test your bot by entering the following details.
Enter
Make an appointment
Select root canal.
Select one of the times that are displayed. If you select any of the available times and do not get a prompt to confirm the appointment, continue to the next step.
Confirm the appointment by selecting yes.
Now that you have a working version of your bot, you must publish it so that you can test it by calling it from a webpage.
\=> Click on Publish, For your bot's alias enter a unique name. Choose Publish.
After the bot finishes publishing, you should get a confirmation notification. Make a note of the alias because you will need it in the next task.
\=> Close the Publish ScheduleAppointment notification by choosing Close.
Incorporating your bot into a webpage
Now that your bot is working, It's time to test it by running it from a webpage. The easiest way to do the test is to create a static webpage and host it on Amazon S3. The webpage will invoke the Amazon Lex API to load your bot. Visitors to the webpage can then interact with it.
Step 7 : Setting up an Amazon Cognito identity pool
You will now set up a webpage for testing your appointment bot. This webpage will be hosted in Amazon S3 as a static webpage. To add security to this page, you must first set up an Amazon Cognito identity pool.
To set up the identity pool:
\=> On the AWS Management Console, on the Search write Cognito and choose Cognito.
\=> Choose Grant access to AWS services and click on Create user pool.
In the Getting started wizard page, for the Identity pool name, enter :
myidentitypool
Select Enable access to unauthenticated identities and choose Create Pool.
Expand the View Details section.
Make a note of the two role names ( one role will end with Auth_Role and the other role will end with Unauth_Role). You will need both of these role names later.
Choose Allow.
Make a note of the IdentityPoolID because you will need this ID later.
Step 8: Modifying IAM roles to allow access to Amazon Lex
The webpage that hosts your bot must be allowed to access Amazon Lex. To do this, the two roles that were created in the Amazon Cognito identity pool need permissions to access Amazon Lex. You must configure AWS Identity and Access Management (IAM) to grant these permissions to the identity pool roles.
On the AWS Management Console, on the Services menu, choose IAM.
Choose Roles.
Enter the first few characters of the Auth_Role name you noted in Step 7.
From the list of search results, select the role that you created.
Choose Attach policies.
In the search box, enter
AmazonLex
.Select the AmazonLexReadOnly and AmazonLexRunBotsOnly policies.
Click On Add permissions.
Use the same process to attach the same two policies to the Unauth_Role.
Step 9: Creating an S3 bucket
Now that you set up the security permissions, you must create an S3 bucket to host your webpage.
- First, download the following two webpage files and extract them to a local file directory.
index.html
error.html
The index.html file includes the script that will load your bot.
On the AWS Management Console, on the Services menu, choose S3.
On the Amazon S3 page, choose Create bucket.
Enter a name for your bucket, Because all S3 bucket names must be unique, try entering
lex-demo31
and six random letters.Select Create bucket.
On the Buckets page, select the bucket that you just created.
On the Bucket overview page, select Upload.
On the Upload page, select Add files.
Browser to the location for the index.html and error.html files that you downloaded previously.
Select both files and then select Upload.
After the upload is complete, return to the Bucket overview page by selecting Exit.
On the Properties tab, scroll down to the Static website hosting section and select Edit.
Select Enable.
For the Index document, enter:
index.html
For the error page, enter:
error.html
Choose Save changes.
Step 10: Updating and testing the demonstration file
You must now update the demonstration HTML file so that it uses the Amazon Cognito identity pool that you created in Step 6.
Use a text editor to make the following changes to the HTML page.
On line 144 of the script, add the
IdentitypoolID
for the identity pool that you created in Step 6.On lines 185 and 186 of the script, add the
botAlias
andbotName
for your bot.Save your updated HTML page locally.
Choose the Objects tab for the bucket that you created in Step 9.
Choose Upload and upload the HTML file that you edited.
Choose Exit to close the Upload page.
Choose the Permissions tab.
In the Block public access section, choose Edit.
Clear the following options:
Block all public access
Block public access to buckets and objects granted through new public bucket or access point policies
Block public and cross-account access to buckets and objects through any public bucket or access point policies
Choose Save changes.
In the confirmation dialog box, enter
Confirm
and then choose Confirm.In the Bucket policy section, choose Edit.
To grant public read access to the webpage, copy the following bucket policy in the Bucket policy editor.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::lex-demo31/*"
}
]
}
In the Resource part of the policy, edit the value of the Amazon Resource Name (ARN) by replacing lex-demo31 with the name of your bucket.
Choose Save changes. You will get a warning that the bucket has public access.
Choose the Properties tab.
Scroll down to the Static website hosting section.
At the bottom of the Static website hosting section, choose the URL.
Your webpage opens, and you can interact with your bot as you did in the AWS Management Console.
Conclusion
You now have successfully created a bot with Amazon Lex and tested it on a webpage.