Lambda 函数将 Lex 定向到新的意图



我正在尝试使用 lambda 函数根据传入插槽的值将 lex 发送到新的意图:

喜欢这个

public override LexResponse Process(LexEvent lexEvent, ILambdaContext context)
{
var slots = lexEvent.CurrentIntent.Slots;
var sessionAttributes = lexEvent.SessionAttributes ?? new Dictionary<string, string>();
DesiredVehicleYesNo type;
if (slots.ContainsKey("DesiredVehicleYesNo"))
{
Enum.TryParse(slots["DesiredVehicleYesNo"], out type);
}
else
{
type = DesiredVehicleYesNo.Null;
}
switch (type)
{
case DesiredVehicleYesNo.YES:
Dictionary<string, string> s = new Dictionary<string, string>();
s.Add("DesiredVehicle", null);
//return ConfirmIntent(sessionAttributes, "DesiredVehicleYes", s, new LexResponse.LexMessage() { Content = "That's great! Let's get started.", ContentType = MESSAGE_CONTENT_TYPE });
//return ElicitSlot(sessionAttributes,"DesiredVehicleYes",null,"DesiredVehicle", new LexResponse.LexMessage() { Content = "That's great! Let's get started.", ContentType = MESSAGE_CONTENT_TYPE });
case DesiredVehicleYesNo.NO:
return ConfirmIntent(sessionAttributes, "DesiredVehicleNo", new Dictionary<string,string>(), new LexResponse.LexMessage() { Content = "Well, that's ok, I can help you choose", ContentType = MESSAGE_CONTENT_TYPE });
}

我只是不确定我应该为此使用哪种返回类型?确认意图,引出插槽,启发意图?此外,我确实需要将插槽传回,我希望新意图使用它自己的提示来填充与该意图相关的插槽。

谢谢

您应该使用ConfirmIntent并提供要切换到的意图的intentNameslots

Lex 响应格式文档

确认意图 — 通知 Amazon Lex 用户应给出"是"或"否"答案,以确认或拒绝当前意图。
您必须包含目的名称和插槽字段。插槽字段必须包含为指定目的配置的每个插槽的条目。如果插槽的值未知,则必须将其设置为 null。如果意图的确认提示字段为空,则必须包含消息字段。如果同时指定消息字段和确认提示字段,则响应将包括确认提示字段的内容。响应卡字段是可选的。

因此,您可以提供自己的消息,但请确保将其写为是/否问题。因为ConfirmIntent需要用户的"是/否"响应。

此方法将始终触发您在intentName中提供的意图。因此,您必须在那里处理用户的响应。
如果用户说"是",则confirmationStatus将保存该值Confirmed
如果用户说"否",则confirmationStatus将保存该值Denied

确保您传回的插槽对于该新意图是正确的。您可以预先填充它们以使用用户已经给您的内容;或设置为null以允许新意向要求用户再次填充这些插槽。

你可以按照Jay的建议使用ConfirmIntent,但是如果你不想向用户提示任何提示,有一种黑客方法可以实现这一点:

  • 授予调用 Lambda 函数的访问权限 调用您的调用 Lambda 函数,即意图 A 的 lambda 函数
  • 获取意图 B 的后端 lambda 函数的名称
  • 使用 boto3 调用具有所有输入的 lambda 函数
  • 响应
  • 将在响应对象的"有效负载"键中
  • 使用 read(( 方法获取响应
  • 在 ['
  • dialogAction']['message']['content'] 中获取实际输出
  • 使用默认 close(( 方法返回

下面是相同的示例代码:

import boto3
client = boto3.client('lambda')
data = {'messageVersion': '1.0', 'invocationSource': 'FulfillmentCodeHook', 'userId': '###', 
'sessionAttributes': {}, 'requestAttributes': None, 
'bot': {'name': '###', 'alias': '$LATEST', 'version': '$LATEST'}, 
'outputDialogMode': 'Text', 
'currentIntent': {'name': '###', 'slots': {'###': '###'}, 
'slotDetails': {'###': {'resolutions': [], 'originalValue': '###'}}, 
'confirmationStatus': 'None'}, 
'inputTranscript': '###'}
response = client.invoke(
FunctionName='{intent-B lambda function}',
InvocationType='RequestResponse',
Payload=json.dumps(data)
)
output = json.loads(response['Payload'].read())['dialogAction']['message']['content']

希望对您有所帮助。

最新更新