我编写了这个函数,当它只是作为字符串返回时,它返回正常。我非常严格地遵循响应卡的语法,它通过了我在 lambda 中的测试用例。然而,当它通过 Lex 调用时,它会抛出一个错误,我将在下面发布。它说fulfillmentState 不能为null,但在它抛出的错误中表明它不为null。
我试过切换对话状态和响应卡的顺序,我试过切换“type”和“fulfillmentState”的顺序。功能:
def backup_phone(intent_request):
back_up_location = get_slots(intent_request)["BackupLocation"]
phone_os = get_slots(intent_request)["PhoneType"]
try:
from googlesearch import search
except ImportError:
print("No module named 'google' found")
# to search
query = "How to back up {} to {}".format(phone_os, back_up_location)
result_list = []
for j in search(query, tld="com", num=5, stop=5, pause=2):
result_list.append(j)
return {
"dialogAction": {
"fulfilmentState": "Fulfilled",
"type": "Close",
"contentType": "Plain Text",
'content': "Here you go",
},
'responseCard': {
'contentType': 'application/vnd.amazonaws.card.generic',
'version': 1,
'genericAttachments': [{
'title': "Please select one of the options",
'subTitle': "{}".format(query),
'buttons': [
{
"text": "{}".format(result_list[0]),
"value": "test"
},
]
}]
}
}
在 lambda 中传递的测试用例的屏幕截图:https ://ibb.co/sgjC2WK Lex 中的错误抛出屏幕截图:https ://ibb.co/yqwN42m
Lex 中的错误文本:
“发生错误:无效的 Lambda 响应:收到来自 Lambda 的无效响应:无法构造 CloseDialogAction 的实例,问题:在 [Source: {"dialogAction": {"fulfilmentState": "Fulfilled ", "type": "Close", "contentType": "Plain Text", "content": "Here you go"}, "responseCard": {"contentType": "application/vnd.amazonaws.card.generic" , "version": 1, "genericAttachments": [{"title": "请选择其中一个选项", "subTitle": "如何将 Iphone 备份到 windows", "buttons": [{"text": ” https://www.easeus。com/iphone-data-transfer/how-to-backup-your-iphone-with-windows-10.html ", "value": "test"}]}]}}; line: 1, column: 121]"