LexChat: Crafting AI Chatbots with Amazon Lex on AWS

LexChat: Crafting AI Chatbots with Amazon Lex on AWS

ยท

8 min read

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.

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

  1. In the Getting started wizard page, for the Identity pool name, enter : myidentitypool

  2. Select Enable access to unauthenticated identities and choose Create Pool.

  3. Expand the View Details section.

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

  5. Choose Allow.

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

  1. On the AWS Management Console, on the Services menu, choose IAM.

  2. Choose Roles.

  3. Enter the first few characters of the Auth_Role name you noted in Step 7.

  4. From the list of search results, select the role that you created.

  5. Choose Attach policies.

  6. In the search box, enter AmazonLex.

  7. Select the AmazonLexReadOnly and AmazonLexRunBotsOnly policies.

  8. Click On Add permissions.

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

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

download folder

  1. On the AWS Management Console, on the Services menu, choose S3.

  2. On the Amazon S3 page, choose Create bucket.

  3. Enter a name for your bucket, Because all S3 bucket names must be unique, try entering lex-demo31 and six random letters.

  4. Select Create bucket.

  5. On the Buckets page, select the bucket that you just created.

  6. On the Bucket overview page, select Upload.

  7. On the Upload page, select Add files.

  8. Browser to the location for the index.html and error.html files that you downloaded previously.

  9. Select both files and then select Upload.

  10. After the upload is complete, return to the Bucket overview page by selecting Exit.

  11. On the Properties tab, scroll down to the Static website hosting section and select Edit.

  12. Select Enable.

  13. For the Index document, enter: index.html

  14. For the error page, enter: error.html

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

  1. On line 144 of the script, add the IdentitypoolID for the identity pool that you created in Step 6.

  2. On lines 185 and 186 of the script, add the botAlias and botName for your bot.

  3. Save your updated HTML page locally.

  4. Choose the Objects tab for the bucket that you created in Step 9.

  5. Choose Upload and upload the HTML file that you edited.

  6. Choose Exit to close the Upload page.

  7. Choose the Permissions tab.

  8. In the Block public access section, choose Edit.

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

  10. Choose Save changes.

  11. In the confirmation dialog box, enter Confirm and then choose Confirm.

  12. In the Bucket policy section, choose Edit.

  13. 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/*"
        }
    ]
}
  1. 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.

  2. Choose Save changes. You will get a warning that the bucket has public access.

  3. Choose the Properties tab.

  4. Scroll down to the Static website hosting section.

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

Did you find this article valuable?

Support AyushDabhi by becoming a sponsor. Any amount is appreciated!

ย