sessionAttributes
在我看来,用Python添加最简单的方法似乎是使用字典。例如,如果要在会话属性中存储某些插槽以备将来使用:
session['attributes']['slotKey'] = intent['slots']['slotKey']['value']
接下来,您可以将其传递给构建响应方法:
buildresponse(session['attributes'], buildSpeechletResponse(title, output, reprompt, should_end_session))
在这种情况下的实现:
def buildSpeechletResponse(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'Simple',
'title': "SessionSpeechlet - " + title,
'content': "SessionSpeechlet - " + output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
def buildresponse(session_attributes, speechlet_response):
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}
这将以推荐的方式在Lambda响应JSON中创建sessionAttributes。
如果不存在,仅添加一个新的sessionAttribute也不会覆盖最后一个。它将仅创建一个新的键值对。
请注意,这在服务模拟器中可能会很好地工作,但是在实际的Amazon Echo上进行测试时可能会返回键属性错误。根据这篇文章,
在Service Simulator上,会话以Session:{…属性:{},…}开头。在Echo上启动会话时,Session根本没有属性键。
解决此问题的方法是,每当创建新会话时,都在lambda处理程序中手动创建它:
if event['session']['new']:
event['session']['attributes'] = {}
onSessionStarted( {'requestId': event['request']['requestId'] }, event['session'])
if event['request']['type'] == 'IntentRequest':
return onIntent(event['request'], event['session'])