我目前正在尝试在Python 3.6中将多个消息从AWS Lambda发送到Lex,但是Lex认为我的答案不正确。
我遇到了多个错误,例如:
所以基本上,我是这样的:
messages = [
{
'contentType': 'PlainText',
'group': 0,
'value': 'Applying this criteria, you have %d result(s) left.' % len(json.loads(session_attributes['results']))
},
{
'contentType': 'PlainText',
'group': 1,
'value': 'What do you want to do next ?'
}
]
format_message('PlainText', messages)
使用format_message临时显示为这样(因为我已经尝试了很多方法来使它工作……没有成功)–但这也不起作用:
def format_message(message_type, content):
return {'messages': content}
最后,它给出了这种响应格式(具有其他lex前提条件,例如slot等,但由于我认为不相关,因此在此不显示它们):
{'message': {'messages': [{'group': 0, 'contentType': 'PlainText', 'value': 'Applying this criteria, you have 1 result(s) left.'}, {'group': 1, 'contentType': 'PlainText', 'value': 'What do you want to do next ?'}]
我试图将'messages'数组转换为字符串,以JSON等形式发送,但似乎无济于事。
我阅读了issue ...
中列出的所有文档。请问有人已经找到解决方案了吗?
谢谢
答案 0 :(得分:1)
据我所知,Lambda不可能做到这一点。 Lex only allows one return message。当您通过命令行aws
管理实用程序(例如:
{
"metadata": {
"importFormat": "JSON",
"importType": "LEX",
"schemaVersion": "1.0"
},
"resource": {
"abortStatement": {
"messages": [
{
"content": "Sorry, I could not understand. Goodbye.",
"contentType": "PlainText"
}
]
},
"childDirected": false,
"clarificationPrompt": {
"maxAttempts": 5,
"messages": [
{
"content": "Sorry, can you please repeat that?",
"contentType": "PlainText"
}
]
},
"idleSessionTTLInSeconds": 300,
"intents": [
{
"conclusionStatement": {
"messages": [
{
"content": "Hello",
"contentType": "PlainText"
},
{
"content": "World",
"contentType": "PlainText"
}
]
},
"fulfillmentActivity": {
"type": "ReturnIntent"
},
"name": "test",
"sampleUtterances": [
"hello"
],
"slots": [],
"version": "1"
}
],
"locale": "en-US",
"name": "Test",
"version": "1",
"voiceId": "Matthew"
}
}
我认为从Lambda函数中最好的选择是像这样在Python中将字符串连接在一起:
'message': {
'contentType': 'PlainText',
'value': ('Applying this criteria, you have %d result(s) left.' % len(json.loads(session_attributes['results']))) + 'What do you want to do next ?'
}
请原谅格式。我通常不使用Python。