作为刚刚实现了同样情况的人(在 @Sid8491 的帮助下),我可以就我如何管理它提供一些见解。
请注意,我使用 C# 是因为我工作的公司就是使用 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 应用程序所需调用的方法。整个过程由我们的 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;
}
之后,只需将结果显示给用户即可。如果您需要更多说明,请告诉我!
UPDATE:
写入 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 是您最好的朋友。希望这有帮助!