如何使用 AWS Lambda & Lex 将履行状态设置为 'ReadyForFulfillment'?



在尝试设置事件响应对象时,我一直得到lambda响应错误,该对象包含多个满足状态的插槽,当前设置为none。我想将它们设置为"ReadyForFulfillment",这显然是一个dialogAction选项。我已经在这个问题上尝试了15个多小时了,但我还是找不到解决方案。

在我的cloudwatch日志中,没有出现错误,这使得调试变得很痛苦。

我有一个名为'policy'的插槽,我正在代码中验证它,根据验证,它向lex发送两个响应中的一个。一个dialogAction请求一个新的策略格式(可以工作),一个dialogAction如果正确验证并将状态更改为'ReadyForFulfillment',它将继续执行

这是我当前的Lambda:

import json
import logging
import re
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def dispatch(event):
slots = event["currentIntent"]["slots"]
policy = slots["policy"]
if re.match(r"policy.([w]+.)+[w]+(?=[s]|$)", policy):
print("Valid input.")
retStr = "valid"
savePolicy = policy
else:
print("Invalid input.")
retStr = "invalid"
if retStr == "invalid":
return {
"dialogAction": { 
"type": "ElicitSlot",
"message": {
"contentType": "PlainText",
"content": "Invalid policy format entered. Please try another format now:"
},
"intentName": "policyprovider",
"slots": {
"policy": policy
},
"slotToElicit": "policy"
}
}
else:
return {
"dialogAction": {
"type": "Close",
"fulfillmentState": "ReadyForFulfillment",
}

}
def lambda_handler(event, context):

logger.debug('event={}'.format(event))
response = dispatch(event)
logger.debug(response)
logger.debug('eventAfter={}'.format(event))
try:
return response
except Exception as e:
logger.debug("Exception: {}".format(e))

这将产生一个Lex聊天机器人,类似于以下内容:

Lex: What is your policy name?
User Input: test 
Lex: Invalid policy format entered. Please try another format now:
User Input: policy.foobar.foobar
Lex: An error has occurred: The server encountered an error processing the Lambda response

在Cloudwatch上,没有此错误的迹象或导致此错误的进一步详细信息。lex intent也不会更改为'ReadyForFulfillment'。

Lex上的事件响应:

RequestID: blah-blah-blah
{
"activeContexts": [],
"alternativeIntents": [
{
"intentName": "AMAZON.FallbackIntent",
"nluIntentConfidence": null,
"slots": {}
},
{
"intentName": "alternativePolicy",
"nluIntentConfidence": {
"score": 0.31
},
"slots": {
"TestItem": null
}
}
],
"botVersion": "$LATEST",
"dialogState": "ElicitSlot",
"intentName": "policyprovider",
"message": "Invalid policy format entered. Please try another format now:",
"messageFormat": "PlainText",
"nluIntentConfidence": {
"score": 1
},
"responseCard": null,
"sentimentResponse": null,
"sessionAttributes": {},
"sessionId": "2022-05-04T11:22:40-blah-blah",
"slotToElicit": "policy",
"slots": {
"policy": "test"

我假设您正在使用AWS Lex V1。如果是这样,很可能是因为在用户输入有效的情况下,Lambda函数返回的响应消息包含一个无效的值。

根据Lex V1的开发人员指南,fulfillmentState的可接受响应值为FulfilledFailed

这将有效地告诉Lex交互已成功完成。假设您希望在有效输入的情况下执行一些自定义处理,请将该操作包含在现有的Lambda函数中,以便在您回复用户时,实现确实已经执行。

最新更新