

# Call center agent assistant
<a name="example-call-center"></a>

In this tutorial, you use Amazon Lex V2 with Amazon Kendra to build an agent assist bot that assists customer support agents and publish it as a web application. Amazon Kendra is an enterprise search service that uses machine learning to search through documents to find answers. For more information about Amazon Kendra, see [the *Amazon Kendra Developer Guide*](https://docs.aws.amazon.com/kendra/latest/dg/what-is-kendra.html). 

Amazon Lex V2 bots are widely used in call centers as the first point of contact for customers. A bot is often capable of resolving customer questions. When a bot can't answer a question, it transfers the conversation to a customer support employee. 

In this tutorial, we create an Amazon Lex V2 bot that agents use to answer customer queries in real time. By reading the answers that the bot provides, the agent is spared from looking up answers manually. 

The bot and web application that you create in this tutorial helps agents respond to customers efficiently and accurately by quickly providing the right resources. The following diagram shows how the web application works. 

![\[alt text not found\]](http://docs.aws.amazon.com/lexv2/latest/dg/images/agent-tutorial.png)






As the diagram shows, the Amazon Kendra index of documents is stored in an Amazon Simple Storage Service (Amazon S3) bucket. If you don't already have an S3 bucket, you can set one up when you create the Amazon Kendra index. In addition to Amazon S3, you will use Amazon Cognito for this tutorial. Amazon Cognito manages permissions for deploying the bot as a web application.

In this tutorial, you create an Amazon Kendra index that provides answers to customer questions, create the bot and add intents that allow it to suggest answers based on the conversation with the customer, set up Amazon Cognito to manage access permissions, and deploy the bot as a web application.

**Estimated time:** 75 minutes

**Estimated cost: **\$12.50 per hour for an Amazon Kendra index and \$10.75 for 1000 Amazon Lex V2 requests. Your Amazon Kendra index continues to run after you are finished with this exercise. Be sure to delete it to avoid unnecessary costs. 

**Note: **Make sure that you choose the same AWS Region for all the services used in this tutorial.

**Topics**
+ [

# Step 1: Create an Amazon Kendra Index
](agent-step-1.md)
+ [

# Step 2: Create an Amazon Lex V2 Bot
](agent-step-2.md)
+ [

# Step 3: Add a Custom and Built-in Intent
](agent-step-3.md)
+ [

# Step 4: Set up Amazon Cognito
](agent-step-4.md)
+ [

# Step 5: Deploy Your Bot as a Web Application
](agent-step-5.md)
+ [

# Step 6: Use the Bot
](agent-step-6.md)

# Step 1: Create an Amazon Kendra Index
<a name="agent-step-1"></a>

Begin by creating an Amazon Kendra index of documents that answer customer questions. An index provides a search API for client queries. You create the index from source documents. Amazon Kendra returns answers it finds in indexed documents to the bot, which displays them to the agent.

The quality and accuracy of the responses suggested by Amazon Kendra depend on the documents that you index. Documents should include files that are frequently accessed by the agent and must be stored in an S3 bucket. You can index unstructured and semi-structured data in .html, Microsoft Office (.doc, .ppt), PDF, and text formats. 

To create an Amazon Kendra index, see [Getting started with an S3 bucket (console)](https://docs.aws.amazon.com/kendra/latest/dg/gs-console.html) in the *Amazon Kendra Developer Guide*.

To add questions and answers (FAQs) that help answer customer queries, see [Adding questions and answers](https://docs.aws.amazon.com/kendra/latest/dg/in-creating-faq.html) in the *Amazon Kendra Developer Guide*. For this tutorial, use the [ML\$1FAQ.csv file on GitHub.](https://github.com/awsdocs/amazon-lex-developer-guide/blob/master/example_apps/agent_assistance_bot/ML_FAQ.csv)

## Next step
<a name="agent-step-1-next"></a>

[Step 2: Create an Amazon Lex V2 Bot](agent-step-2.md)

# Step 2: Create an Amazon Lex V2 Bot
<a name="agent-step-2"></a>

Amazon Lex V2 provides an interface between the call center agent and the Amazon Kendra index. It keeps track of the conversation between the agent and the customer and calls the `AMAZON.KendraSearchIntent` intent based on the questions the customer asks. An *intent* is an action that the user wants to perform.

Amazon Kendra searches the indexed documents and returns an answer to Amazon Lex V2 that it displays in the bot. This answer is visible only to the agent.

**To create an agent assistant bot**

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. In the navigation pane, choose **Bots**.

1. Choose **Create**.

1. Choose **Custom bot** and configure the bot.

   1. **Bot name** – Enter a name that indicates the bot's purpose, such as **AgentAssistBot**.

   1. **Output voice** – Choose **None**.

   1. **Session timeout** – Enter **5**.

   1. **COPPA** – Choose **No**.

1. Choose **Create**. After creating the bot, Amazon Lex V2 displays the bot editor tab.

## Next step
<a name="agent-step-2-next"></a>

[Step 3: Add a Custom and Built-in Intent](agent-step-3.md)

# Step 3: Add a Custom and Built-in Intent
<a name="agent-step-3"></a>

An *intent* represents an action that the call center agent wants the bot to perform. In this case, the agent wants the bot to suggest responses and helpful resources based on the agent's conversation with the customer. 

Amazon Lex V2 has two types of intents: custom intents and built-in intents. `AMAZON.KendraSearchIntent` is a built-in intent. The bot uses the `AMAZON.KendraSearchIntent` intent to query the index and display the responses suggested by Amazon Kendra. 

The bot in this example doesn't need a custom intent. However, to build the bot, you must create at least one custom intent with at least one sample utterance. This intent is required only to build your agent assistant bot. It doesn’t perform any other function. The utterance for the intent must not answer any of the questions that the customer might ask. This ensures that the `AMAZON.KendraSearchIntent` is called to answer customer queries. For more information, see [AMAZON.KendraSearchIntent](built-in-intent-kendra-search.md).

**To create the required custom intent**

1. On the **Getting started with your bot** page, choose **Create intent**.

1. For **Add intent**, choose **Create intent**.

1. In the **Create intent** dialog box, enter a descriptive name for the intent, such as **RequiredIntent**.

1. For **Sample utterances**, enter a descriptive utterance, such as **Required utterance**.

1. Choose **Save intent**.

**To add the `AMAZON.KendraSearchIntent` intent and response message**

1. In the navigation pane, choose the plus sign (\$1) next to **Intents**.

1. Choose **Search existing intents**.

1. In the **Search intents** box, enter **AMAZON.KendraSearchIntent**, then choose it from the list.

1. Give the intent a descriptive name, such as **AgentAssistSearchIntent**, then choose **Add**.

1. In the intent editor, choose **Amazon Kendra query** to open the query options.

1. Choose the index that you want the intent to search,

1. In the **Response** section, add the following three messages to a message group.

   ```
   I found an answer for the customer query: ((x-amz-lex:kendra-search-response-question_answer-question-1)) and the answer is ((x-amz-lex:kendra-search-response-question_answer-answer-1)).
   I found an excerpt from a helpful document: ((x-amz-lex:kendra-search-response-document-1)).
   I think this answer will help the customer: ((x-amz-lex:kendra-search-response-answer-1)).
   ```

1. Choose **Save intent**. 

1. Choose **Build** to build the bot.

## Next step
<a name="agent-step-3-next"></a>

[Step 4: Set up Amazon Cognito](agent-step-4.md)

# Step 4: Set up Amazon Cognito
<a name="agent-step-4"></a>

To manage permissions and users for the web application, you need to set up Amazon Cognito. Amazon Cognito ensures that the web application is secure and has access control. Amazon Cognito uses identity pools to provide AWS credentials that grant your users access to other AWS services. For this tutorial, it provides access to Amazon Lex V2.

When creating an identity pool, Amazon Cognito provides you with AWS Identity and Access Management (IAM) roles for authenticated and unauthenticated users. You modify the IAM roles by adding policies that grant access to Amazon Lex V2. 

**To set up Amazon Cognito**

1. Sign into the AWS Management Console and open the Amazon Cognito console at [https://console.aws.amazon.com/cognito/](https://console.aws.amazon.com/cognito).

1. Choose **Manage Identity Pools**.

1. Choose **Create new identity pool**.

1. Configure the identity pool.

   1. **Identity pool name** – Enter a name that indicates the pool's purpose, such as **BotPool**.

   1. In the **Unauthenticated identities** section, choose **Enable access to unauthenticated identities**.

1. Choose **Create Pool**.

1. On the **Identify the IAM roles to use with your new identity pool** page, choose **View Details**.

1. Record the IAM role names. You will modify them later.

1. Choose **Allow**.

1. On the **Getting Started with Amazon Cognito** page, for **Platform**, choose **JavaScript**.

1. In the **Get AWS Credentials** section, find and record the **Identity pool ID**.

1. To allow access to Amazon Lex V2, modify the authenticated and unauthenticated IAM roles.

   1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

   1. In the navigation pane, under **Access Management**, choose **Roles**.

   1. In the search box, enter the name of the authenticated IAM role and choose the checkbox next to it. 

      1. Choose **Attach policies**.

      1. In the search box, enter **AmazonLexRunBotsOnly** and choose the checkbox next to it.

      1. Choose **Attach policy**.

   1. Enter the name of the unauthenticated IAM role in the search box and choose the checkbox next to it. 

      1. Choose **Attach policies**.

      1. In the search box, enter **AmazonLexRunBotsOnly** and choose the checkbox next to it.

      1. Choose **Attach policy**.

## Next step
<a name="agent-step-4-next"></a>

[Step 5: Deploy Your Bot as a Web Application](agent-step-5.md)

# Step 5: Deploy Your Bot as a Web Application
<a name="agent-step-5"></a>

**To deploy your bot as a web application**

1. Download the repository at [https://github.com/awsdocs/amazon-lex-developer-guide/blob/master/example\$1apps/agent\$1assistance\$1bot/ ](https://github.com/awsdocs/amazon-lex-developer-guide/blob/master/example_apps/agent_assistance_bot/) to your computer.

1. Navigate to the downloaded repository and open the index.html file in an editor.

1. Make the following changes.

   1. In the `AWS.config.credentials` section, enter your Region name and your identity pool ID.

   1. In the` Amazon Lex V2 runtime parameters` section, enter the bot name.

   1. Save the file.

# Step 6: Use the Bot
<a name="agent-step-6"></a>

For demo purposes, you provide input to the bot as the customer and as the agent. To differentiate between the two, questions asked by the customer begin with “Customer:” and answers provided by the agent begin with “Agent:”. You can choose from a menu of suggested inputs. 

Run your web application by opening `index.html` to engage in a conversation with your bot like this:.

![\[An example conversation between a call center bot and the Agent Assistant.\]](http://docs.aws.amazon.com/lexv2/latest/dg/images/agent-tutorial-ss.png)


The `pushChat()` function in the index.html file is explained below.

```
            
            var endConversationStatement = "Customer: I have no more questions. Thank you." 
            // If the agent has to send a message, start the message with 'Agent'
            var inputText = document.getElementById('input');
            if (inputText && inputText.value && inputText.value.trim().length > 0 && inputText.value[0]=='Agent') {               
                showMessage(inputText.value, 'agentRequest','conversation');
                inputText.value = "";
            }
            // If the customer has to send a message, start the message with 'Customer'
            if(inputText && inputText.value && inputText.value.trim().length > 0 && inputText.value[0]=='Customer') {  
                // disable input to show we're sending it
                var input = inputText.value.trim();
                inputText.value = '...';
                inputText.locked = true;
                customerInput = input.substring(2);

                // Send it to the Lex runtime
                var params = {
                    botAlias: '$LATEST',
                    botName: 'KendraTestBot',
                    inputText: customerInput,
                    userId: lexUserId,
                    sessionAttributes: sessionAttributes
                };

                showMessage(input, 'customerRequest', 'conversation');
                if(input== endConversationStatement){
                    showMessage('Conversation Ended.','conversationEndRequest','conversation');
                }
                lexruntime.postText(params, function(err, data) {
                    if (err) {
                        console.log(err, err.stack);
                        showMessage('Error:  ' + err.message + ' (see console for details)', 'lexError', 'conversation1')
                    }

                    if (data &&input!=endConversationStatement) {
                        // capture the sessionAttributes for the next cycle
                        sessionAttributes = data.sessionAttributes;
                        
                            showMessage(data, 'lexResponse', 'conversation1');
                    }
                    // re-enable input
                    inputText.value = '';
                    inputText.locked = false;
                });
            }
            // we always cancel form submission
            return false;
```

 When you provide input as a customer, the Amazon Lex V2 runtime API sends it to Amazon Lex V2.

The `showMessage(daText, senderRequest, displayWindow)` fuction displays the conversation between the agent and the customer in the chat window. Responses suggested by Amazon Kendra are shown in an adjacent window. The conversation ends when customer says **“I have no more questions. Thank you.”**

**Note: **Please delete your Amazon Kendra index when not in use.