我正在尝试使用lambda函数根据传入槽的值将lex发送到新的intent:
像这样
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,ElicitSlot,ElicitIntent?此外,我确实需要传回插槽,我希望新的意图使用它自己的提示来填充与该意图相关的插槽。
谢谢
你应该使用ConfirmIntent
并提供你要切换到的意图的intentName
和slots
。
ConfirmIntent - 通知Amazon Lex,用户应该给出是或否答案以确认或拒绝当前意图。 您必须包含intentName和slots字段。 slots字段必须包含为指定intent配置的每个插槽的条目。如果插槽的值未知,则必须将其设置为null。如果intent的confirmationPrompt字段为null,则必须包含消息字段。如果同时指定message字段和confirmationPrompt字段,则响应将包含confirmationPrompt字段的内容。 responseCard字段是可选的。
因此,您可以提供自己的消息,但请确保将其写为是/否问题。因为ConfirmIntent
会期待用户的回答是/否。
此方法将始终触发您在intentName
中提供的意图。所以你必须在那里处理用户的响应。
如果用户说“是”,那么confirmationStatus
将保持值Confirmed
。
如果用户说“不”,那么confirmationStatus
将保持值Denied
。
确保您传回的插槽对于该新意图是正确的。你可以预先填写它们以使用用户已经给你的东西;或设置为null
以允许新意图要求用户再次填充这些插槽。
您可以像Jay建议的那样使用ConfirmIntent
,但是如果您不想向用户提示任何内容,则有一种实现此目的的hacky方法:
以下是相同的示例代码:
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']
希望能帮助到你。