是否可以使用 AMAZON LEX 构建一个连接数据库和存储在客户端的 Web 服务的聊天机器人?

Is it possible to use AMAZON LEX to build a chatbot which connects with database and Web service stored on client side?

我们的组织想要开发一个 "LOST & FOUND System Application" 使用集成在网站中的聊天机器人。

每当用户开始与聊天机器人对话时,聊天机器人应询问丢失物品或找到物品的详细信息,并将详细信息存储在数据库中。

我们该怎么做?

我们可以使用我们自己的网络服务吗,因为组织不想将数据库保留在亚马逊的服务器中。

是的,这是可能的。

您可以从您的网站向 Lex 发送请求,这将提取意图和实体。
一旦你掌握了这些,你就可以用你选择的任何语言编写后端代码,并使用你想要的任何数据库。
在您的用例中,您可能只想使用 Lex。 PostText 将是您要调用的主要函数。

您将需要在 Lex 中创建一个具有多个插槽 LosingDateLosingPlace 或您想要的任何内容的意图,然后它将能够从用户那里获取所有这些信息并通过它到您的网络应用程序。

作为刚刚实施过同样情况的人(在@Sid8491 的大力帮助下),我可以就我是如何管理它给出一些见解。

请注意,我正在使用 C#,因为这是我工作的公司所使用的。

首先,机器人需要用户的输入来决定调用什么意图。为此,我实现了对 Lex API.

的 PostText 调用
PostTextRequest lexTextRequest = new PostTextRequest()
        {
            BotName = botName,
            BotAlias = botAlias,
            UserId = sessionId,
            InputText = messageToSend
        };

        try
        {
            lexTextResponse = await awsLexClient.PostTextAsync(lexTextRequest);
        }
        catch (Exception ex)
        {
            throw new BadRequestException(ex);
        }

请注意,这需要您创建一个 Cognito 对象来验证您的 AmazonLexClient(如下所示):

protected void InitLexService()
    {
        //Grab region for Lex Bot services
        Amazon.RegionEndpoint svcRegionEndpoint = Amazon.RegionEndpoint.USEast1;

        //Get credentials from Cognito
        awsCredentials = new CognitoAWSCredentials(
                            poolId,                     // Identity pool ID
                            svcRegionEndpoint);         // Region

        //Instantiate Lex Client with Region
        awsLexClient = new AmazonLexClient(awsCredentials, svcRegionEndpoint);
    }

在我们得到机器人的响应后,我们使用一个简单的 switch case 来正确识别我们需要为我们的网络应用程序调用的方法 运行。整个过程由我们的 web 应用程序处理,我们仅使用 Lex 来识别用户的请求和插槽值。

//Call Amazon Lex with Text, capture response
var lexResponse = await awsLexSvc.SendTextMsgToLex(userMessage, sessionID);

//Extract intent and slot values from LexResponse
string intent = lexResponse.IntentName;
var slots = lexResponse.Slots;

//Use LexResponse's Intent to call the appropriate method 
switch (intent)
{
    case: /*Your intent name*/:
    /*Call appropriate method*/;
    break;
}

之后,只需要将结果显示给用户即可。如果您需要更多说明,请告诉我!

更新:

要写入 SQL 的插槽数据的示例实现(同样在 C# 中)如下所示:

 case "LostItem":                                              
      message = "Please fill the following form with the details of the item you lost.";
      LostItem();
      break;

这将带您进入 LostItem() 方法,您可以使用该方法填写表格。

public void LostItem()
{
    string itemName = string.Empty;
    itemName = //Get from user
    //repeat with whatever else you need for a complete item object

    //Implement a SQL call to a stored procedure that inserts the object into your database. 
    //You can do a similar call to the database to retrieve an object as well
}

这应该能为您指明正确的方向。如果您需要有关 SQL 存储过程的帮助,Google 是您最好的朋友。希望这对您有所帮助!