

终止支持通知：2025年9月15日， AWS 我们将停止对Amazon Lex V1的支持。2025 年 9 月 15 日之后，您将无法再访问 Amazon Lex V1 控制台或 Amazon Lex V1 资源。如果您使用的是 Amazon Lex V2，请改为参阅 [Amazon Lex V2 指南](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 步骤 6：使用机器人
<a name="agent-step-6"></a>

出于演示目的，您以客户和客服的身份向机器人提供输入。为了区分两者，客户提出的问题以“客户：”开头，客服提供的答案以“客服：”开头。您可以从建议的输入菜单中进行选择。

打开 `index.html` 并与机器人进行类似于下图的对话来运行您的 Web 应用程序：

![\[与呼叫中心机器人的两个对话示例。在第一篇文章中，客户询问什么是亚马逊 SageMaker 人工智能，以及何时使用Amazon Polly代替Amazon Lex。在第二个对话中，Amazon Kendra 找到了这两个问题的常见问题答案。\]](http://docs.aws.amazon.com/zh_cn/lex/latest/dg/images/agent-tutorial-ss.png)


index.html 文件中的 `pushChat()` 函数说明如下。

```
            
            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;
```

 当您以客户身份提供输入时，Amazon Lex 运行时 API 会将其发送给 Amazon Lex。

`showMessage(daText, senderRequest, displayWindow)` 函数在聊天窗口中显示客服和客户之间的对话。Amazon Kendra 建议的响应显示在相邻的窗口中。当客户说 **“I have no more questions. Thank you.”** 时，对话就结束了

**注意：**不使用 Amazon Kendra 索引时，请将其删除。