如何将AWS Lex chatbot集成到我的网站?

Win*_*eng 9 php integration chatbot amazon-web-services

我的网站正在执行客户服务和支持票证系统。

但是,集成AWS lex的方法似乎不像FB那样简单。

我想做的就是让Lex Bot在我的网站上为客户回复票。


我需要首先学习AWS Lambda和API网关以集成Lex吗?

我想知道如何在PHP curl中调用lex bot API。

正如API Docs所说。

但是我不确定为什么POST URL像相对路径一样。

无论如何,感谢您的帮助。

Jay*_*mmu 5

要将lex bot集成到网站,您需要了解AWS Lex运行时API,AWS IAM和Cognito配置。这是将bot集成到网站的最安全方法。

以下是将lex bot添加到您的网站的步骤:

1.创建新的身份池

在Amazon Cognito控制台中,选择管理新身份池,然后选择创建新身份池。您提供一个池名称(testPoolName),选择“启用对未经身份验证的身份的访问”,然后选择“创建池”。记下身份池ID。

2.向lex bot授予对身份池的访问权限

转到IAM服务。选择角色。查找Cognito_testPoolNameUnauth_Role。单击附加策略。搜索AmazonLexRunBotsOnly并将其附加。

3.从网站进行Lex运行时调用:这是该网站的示例代码

在以下代码中填写身份池ID。要了解此代码,您需要了解AWS Lex运行时API。

    <!DOCTYPE html>
<html>

<head>
    <title>Amazon Lex for JavaScript - Sample Application (BookTrip)</title>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.41.0.min.js"></script>
    <style language="text/css">
        input#wisdom {
            padding: 4px;
            font-size: 1em;
            width: 400px
        }
    input::placeholder {
        color: #ccc;
        font-style: italic;
    }

    p.userRequest {
        margin: 4px;
        padding: 4px 10px 4px 10px;
        border-radius: 4px;
        min-width: 50%;
        max-width: 85%;
        float: left;
        background-color: #7d7;
    }

    p.lexResponse {
        margin: 4px;
        padding: 4px 10px 4px 10px;
        border-radius: 4px;
        text-align: right;
        min-width: 50%;
        max-width: 85%;
        float: right;
        background-color: #bbf;
        font-style: italic;
    }

    p.lexError {
        margin: 4px;
        padding: 4px 10px 4px 10px;
        border-radius: 4px;
        text-align: right;
        min-width: 50%;
        max-width: 85%;
        float: right;
        background-color: #f77;
    }
</style>
Run Code Online (Sandbox Code Playgroud)

<body>
    <h1 style="text-align:  left">Amazon Lex - BookTrip</h1>
    <p style="width: 400px">
        This little chatbot shows how easy it is to incorporate
        <a href="https://aws.amazon.com/lex/" title="Amazon Lex (product)" target="_new">Amazon Lex</a> into your web pages.  Try it out.
    </p>
    <div id="conversation" style="width: 400px; height: 400px; border: 1px solid #ccc; background-color: #eee; padding: 4px; overflow: scroll"></div>
    <form id="chatform" style="margin-top: 10px" onsubmit="return pushChat();">
        <input type="text" id="wisdom" size="80" value="" placeholder="I need a hotel room">
    </form>
    <script type="text/javascript">
        // set the focus to the input box
    document.getElementById("wisdom").focus();

    // Initialize the Amazon Cognito credentials provider
    AWS.config.region = 'us-east-1'; // Region
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    // Provide your Pool Id here
        IdentityPoolId: 'us-east-1:XXXXX',
    });

    var lexruntime = new AWS.LexRuntime();
    var lexUserId = 'chatbot-demo' + Date.now();
    var sessionAttributes = {};

    function pushChat() {

        // if there is text to be sent...
        var wisdomText = document.getElementById('wisdom');
        if (wisdomText && wisdomText.value && wisdomText.value.trim().length > 0) {

            // disable input to show we're sending it
            var wisdom = wisdomText.value.trim();
            wisdomText.value = '...';
            wisdomText.locked = true;

            // send it to the Lex runtime
            var params = {
                botAlias: '$LATEST',
                botName: 'BookTrip',
                inputText: wisdom,
                userId: lexUserId,
                sessionAttributes: sessionAttributes
            };
            showRequest(wisdom);
            lexruntime.postText(params, function(err, data) {
                if (err) {
                    console.log(err, err.stack);
                    showError('Error:  ' + err.message + ' (see console for details)')
                }
                if (data) {
                    // capture the sessionAttributes for the next cycle
                    sessionAttributes = data.sessionAttributes;
                    // show response and/or error/dialog status
                    showResponse(data);
                }
                // re-enable input
                wisdomText.value = '';
                wisdomText.locked = false;
            });
        }
        // we always cancel form submission
        return false;
    }

    function showRequest(daText) {

        var conversationDiv = document.getElementById('conversation');
        var requestPara = document.createElement("P");
        requestPara.className = 'userRequest';
        requestPara.appendChild(document.createTextNode(daText));
        conversationDiv.appendChild(requestPara);
        conversationDiv.scrollTop = conversationDiv.scrollHeight;
    }

    function showError(daText) {

        var conversationDiv = document.getElementById('conversation');
        var errorPara = document.createElement("P");
        errorPara.className = 'lexError';
        errorPara.appendChild(document.createTextNode(daText));
        conversationDiv.appendChild(errorPara);
        conversationDiv.scrollTop = conversationDiv.scrollHeight;
    }

    function showResponse(lexResponse) {

        var conversationDiv = document.getElementById('conversation');
        var responsePara = document.createElement("P");
        responsePara.className = 'lexResponse';
        if (lexResponse.message) {
            responsePara.appendChild(document.createTextNode(lexResponse.message));
            responsePara.appendChild(document.createElement('br'));
        }
        if (lexResponse.dialogState === 'ReadyForFulfillment') {
            responsePara.appendChild(document.createTextNode(
                'Ready for fulfillment'));
            // TODO:  show slot values
        } else {
            responsePara.appendChild(document.createTextNode(
                '(' + lexResponse.dialogState + ')'));
        }
        conversationDiv.appendChild(responsePara);
        conversationDiv.scrollTop = conversationDiv.scrollHeight;
    }
</script>
Run Code Online (Sandbox Code Playgroud)


tip*_*chi 2

最近玩了一下 AWS Lex,看来您确实无法避免使用 Lambda 代码。

首先,验证和实现代码挂钩将是 lambda 函数,对于任何半体面的 lex 机器人对话,您将需要它们。

其次是聊天客户端:如果您不想使用现有的本机聊天机器人集成列表(当前为 Facebok、Twilio SMS 和 Slack),您将需要自定义实现。直接 PHP 卷曲可能是一种选择(直接访问 API),但我强烈建议使用标准 AWS API Gateway / AWS lambda 设置来创建 lex 客户端并使用 SDK 的便利性。这是一个非常灵活的设置,非常简单。我们在几天之内就学会了,使用 boto3 SDK 的最小 Python 代码库,几乎没有 Python 经验。

希望能帮助到你!