AWS Machine Learning Blog
Enhancing your chatbot experience with web browsing
Chatbots are popping up everywhere. They are qualifying leads, assisting with sales, and automating customer service. However, conversational chatbot experiences have been limited to the space available within the chatbot window.
What if these web-based chatbots could provide an interactive experience that expanded beyond the chat window to include relevant web content based on user inputs? In a previous post we showed you how to deploy a web UI for your chatbot. In this post we will show you how to enhance that experience.
Here is an example of how we add an interactive web UI to the Order Flowers chatbot with the lex-web-ui customization.
Installing the chatbot UI
To install your chatbot, complete the following steps:
- Deploy the chatbot UI in your AWS account by launching the following AWS CloudFormation stack:
- Set EnableCognitoLogin to true in the parameters.
- To check if it’s working, on the AWS CloudFormation console, choose Stacks.
- Choose the stack you created.
- In the Outputs section, choose ParentPageURL.
You have now deployed the bot in the CloudFront distribution you created.
Installing the chatbot UI enhancer
After you install the chatbot UI, launch the following AWS CloudFormation stack:
There are two parameters for this stack:
- BotName – The chatbot UI bot you deployed. The parameter value of WebUiOrderFlowers is populated by default.
- lexwebuiStackName – The name of the deployed in the previous step. The parameter value of lex-web-ui is populated by default.
When the stack is complete, find the URL for the new demo site on the Outputs tab on the AWS CloudFormation console.
Enhancing the existing bot with AWS Lambda
To enhance the existing bot with AWS Lambda, complete the following steps:
- On the Amazon Lex console, choose Bots.
- Choose the bot you created.
- In the Lambda initialization and validation section, for Lambda function, choose the function you created as part of the CloudFormation stack (enhanced-orderflowers-<stackname>).
For production workloads, you should publish a new version of the bot. Amazon Lex takes a snapshot copy of the $LATEST version to publish a new version. For more information, see Exercise 3: Publish a Version and Create an Alias.
Enhancing authentication
You have now set up the enhanced chatbot UI. It’s recommended that you authenticate for a production environment. This post uses Amazon Cognito to add a social identity provider (Google) to your user pool. For instructions, see Adding Social Identity Providers to a User Pool.
This step allows your bot to display your Google calendar while you order your flowers. If you skip this step, the bot still functions normally.
Dynamically viewing content on your webpage
Having content appear and disappear on your website based on your interactions with the bot is a powerful feature.
For example, if you ask the bot to order flowers, the bot messaging interface and the webpage change. This example actively builds HTML on the fly with values that the bot sends back to the end-user.
Enhancing pages with external content to help with flower selection
When you ask the bot to buy roses, the result depends on if you’re in unauthenticated or authenticated mode.
In authenticated mode, the iframe changes from the default homepage to a Wikipedia page about roses. The Area chart also changes to a Roses Sold graph that shows the number of roses sold per year.
In authenticated (with Google) mode, the iframe changes to your Google calendar to help you schedule a delivery day. The Area chart still changes to the Roses Sold graph.
This powerful tool allows content from various parts of the website or the internet to appear by interacting with the bot. This also allows the bot to recognize if you’re authenticated or not and tailor your browsing experience.
Parent page, iframes, session attributes, and dynamic HTML tags
Four main components make up the Order Flowers bot page and how the various pieces interact with each other:
- Parent page – This page houses all the various components, including the chatbot iframe, dynamically created HTML, and the navigation portal (an iframe that displays various HTML pages external and internal to the website).
- Chatbot iframe – This is the chatbot UI that the end-user interacts with. The chatbot is loaded using a JavaScript snippet that mounts an iframe to the bottom right of the parent page and preloads it with an API to interact with the parent page.
- Session attributes – These are arbitrary values that get sent back and forth from the chatbot UI backend to the parent page. You can manipulate these values in Lambda. On the parent page, the session attributes event data is made available in a variable called
sessionAttributes
. - Dynamic HTML <Div> tags – This appears on the top right of the page and displays various charts based on the question asked. You can populate it with any data, not just charts. You manipulate the data by returning the values through the session attributes fields. In the parent page,
sessionAttributes.appContext
houses this data.
The following diagram illustrates the solution architecture.
Chatbot UI user login with Amazon Cognito
When you’re authenticated through the integrated Amazon Cognito feature, the chatbot UI attaches a signed token as a session attribute. The enhanced Order Flowers webpage uses the token to make additional user attributes available, including fields such as given name, family name, and email address. These fields help return personalized information (for example, addressing you by your name).
Limitations
There are certain limitations to displaying outside webpages and content through the chatbot UI parent page.
If cross-origin resource sharing (CORS) is enabled on the external content that is being pulled into the parent page iframe navigation portal, the browser blocks the content. Browsers don’t block different webpages from the same domain or external webpages that don’t have CORS enabled (for example, Wikipedia). For more information, see Cross-Origin Resource Sharing (CORS) on the MDN web docs website.
In most use cases, you should use the navigation portal to pull in content from your own domain, due to the inherent limitations of iframes and CORS.
Additional Resources
The concepts discussed in this blogpost can be used with the QnaBot. The following README goes in detailed instructions on setting up the solution.
Conclusion
This post demonstrates how to enhance the Order Flowers bot with a Lambda function that parses your JWT token and extracts the relevant information. If you are authenticated through Google, the bot extracts information like your name and email address, and displays your Google calendar to help you schedule your delivery date. The function also verifies that the JWT token signature is valid.
The chatbot UI in this post is based on the aws-lex-web-ui open-source project. For more information, see the GitHub repo.
About the Authors
Mohamed Khalil is a Consultant for AWS Professional Services. Bob Strahan is a Principal Consultant for AWS Professional Services. Bob Potterveld is a Senior Consultant for AWS Professional Services. They help our customers and partners on a variety of projects.