Python 无法让AWS Lambda向Lex发送提示消息
我试图制作一个模板,为AWS Lambda编写新函数,以验证来自Lex的输入。我在实现正确的输入方面没有任何问题,只是在插槽填充错误时让Lex中继提示消息Python 无法让AWS Lambda向Lex发送提示消息,python,amazon-web-services,aws-lambda,chatbot,amazon-lex,Python,Amazon Web Services,Aws Lambda,Chatbot,Amazon Lex,我试图制作一个模板,为AWS Lambda编写新函数,以验证来自Lex的输入。我在实现正确的输入方面没有任何问题,只是在插槽填充错误时让Lex中继提示消息 def hello_world(intent_request): object_of_greeting = get_slots(intent_request)["HelloObject"] type_of_greeting = get_slots(intent_request)["Greeting
def hello_world(intent_request):
object_of_greeting = get_slots(intent_request)["HelloObject"]
type_of_greeting = get_slots(intent_request)["GreetingType"]
# add more of these calls if more slots are involved
source = intent_request['invocationSource']
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slot(s).
# Use the elicitSlot dialog action to re-prompt for the first violation detected.
slots = get_slots(intent_request)
validation_result = validate_greeting(object_of_greeting, type_of_greeting)
if not validation_result['isValid']:
slots[validation_result['violatedSlot']] = None
return elicit_slot(intent_request['sessionAttributes'],
intent_request['currentIntent']['name'],
slots,
validation_result['violatedSlot'],
validation_result['message'])
等等。。。然后,验证功能:
def validate_greeting(object_of_greeting, type_of_greeting):
objects = ['world', 'kitty', 'my dear']
# these should match the listed examples of their corresponding slot, which is 'HelloObject' here
if object_of_greeting is not None and object_of_greeting.lower() not in objects:
return build_validation_result(False,
'HelloObject',
f'You cannot greet {object_of_greeting},
would you like to greet someone else?')
greetings = ['hello', "gday", "whassup"]
if type_of_greeting is not None and type_of_greeting.lower() not in greetings:
return build_validation_result(False,
'GreetingType',
f'You cannot say {type_of_greeting} to anyone.
How about gday or whassup?')
return build_validation_result(True, None, None)
我能够为build\u validation\u result
函数返回正确的JSON和测试脚本,如下所示:
{
'isValid': False,
'violatedSlot': 'HelloObject',
'message': {
'contentType': 'PlainText',
'content': 'You cannot greet foobar, would you like to greet someone else?
}
}
我还编写了一个针对错误输入的Lambda测试,它给出了以下响应:
{
"sessionAttributes": {},
"dialogAction": {
"type": "ElicitSlot",
"intentName": "HelloWorld",
"slots": {
"HelloObject": null,
"GreetingType": "howdy"
},
"slotToElicit": "HelloObject",
"message": {
"contentType": "PlainText",
"content": "You cannot greet foobar, would you like to greet someone else?"
}
}
}
不知何故,验证结果并没有返回到Lex,因此它只是一遍又一遍地返回控制台提示“你想问候谁?”然后在n次尝试后放弃
我的测试表明,Lambda函数正在输出正确的JSON,因此我不知道为什么我永远无法在聊天机器人中显示它们的提示消息。非常感谢您的帮助。如果需要,将发布更多详细信息。为了增加混乱,我发现有两个插槽需要填充,我可以在尝试为第二优先级插槽(而不是第一优先级插槽)插入插槽时得到Lambda提示。为了增加混乱,我发现有两个插槽需要填充,我可以在尝试为第二个优先级的插槽创建一个Lambda提示,但不是第一个。