我的意图架构中有3个插槽(account,dollar_value,recipient_first)用于Alexa技能,我想保存演讲者在会话属性中提供的任何插槽.
I have 3 slots (account, dollar_value, recipient_first) within my intent schema for an Alexa skill and I want to save whatever slots are provided by the speaker in the session Attributes.
我正在使用以下方法来设置会话属性:
I am using the following methods to set session attributes:
def create_dollar_value_attribute(dollar_value): return {"dollar_value": dollar_value} def create_account_attribute(account): return {"account": account} def create_recipient_first_attribute(recipient_first): return {"recipient_first": recipient_first}但是,正如您可能猜到的,如果要在sessionAttributes中保存多个插槽作为数据,则sessionAttributes会被覆盖,如下所示:
However, as you may guess, if I want to save more than one slot as data in sessionAttributes, the sessionAttributes is overwritten as in the following case:
session_attributes = {} if session.get('attributes', {}) and "recipient_first" not in session.get('attributes', {}): recipient_first = intent['slots']['recipient_first']['value'] session_attributes = create_recipient_first_attribute(recipient_first) if session.get('attributes', {}) and "dollar_value" not in session.get('attributes', {}): dollar_value = intent['slots']['dollar_value']['value'] session_attributes = create_dollar_value_attribute(dollar_value)我的lambda函数对语音输入的JSON响应如下,其中提供了两个插槽(dollar_value和recipient_first)(我的猜测是第二个if语句中的the create_dollar_value_attribute方法将覆盖第一个):
The JSON response from my lambda function for a speech input in which two slots (dollar_value and recipient_first) were provided is as follows (my guess is that the create_dollar_value_attribute method in the second if statement is overwriting the first):
{ "version": "1.0", "response": { "outputSpeech": { "type": "PlainText", "text": "Some text output" }, "card": { "content": "SessionSpeechlet - Some text output", "title": "SessionSpeechlet - Send Money", "type": "Simple" }, "reprompt": { "outputSpeech": { "type": "PlainText" } }, "shouldEndSession": false }, "sessionAttributes": { "dollar_value": "30" } }sessionAttributes的正确答案应该是:
"sessionAttributes": { "dollar_value": "30", "recipient_first": "Some Name" },如何创建此回复?是否有更好的方法在JSON响应中将值添加到sessionAttributes?
How do I create this response? Is there a better way to add values to sessionAttributes in the JSON response?
推荐答案在我看来,用Python添加sessionAttributes的最简单方法似乎是使用字典.例如,如果您想在会话属性中存储一些插槽以备将来使用:
The easiest way to add sessionAttributes with Python in my opinion seems to be by using a dictionary. For example, if you want to store some of the slots for future in the session attributes:
session['attributes']['slotKey'] = intent['slots']['slotKey']['value']接下来,您可以将其传递给构建响应方法:
Next, you can just pass it on to the build response method:
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.
This creates the sessionAttributes in the recommended way in the Lambda response JSON.
如果不存在,仅添加一个新的sessionAttribute也不会覆盖最后一个.它将仅创建一个新的键值对.
Also just adding a new sessionAttribute doesn't overwrite the last one if it doesn't exist. It will just create a new key-value pair.
请注意,这在服务模拟器中可能会很好地工作,但是在实际的Amazon Echo上进行测试时可能会返回键属性错误.根据此帖子,
Do note, that this may work well in the service simulator but may return a key attribute error when testing on an actual Amazon Echo. According to this post,
在服务模拟器上,会话以会话:{...属性:{},...}开始 当会话从Echo开始时,会话根本没有属性键.
On Service Simulator, sessions starts with Session:{ ... Attributes:{}, ... } When sessions start on the Echo, Session does not have an Attributes key at all.
解决此问题的方法是,只要创建新会话,就在lambda处理程序中手动创建它:
The way I worked around this was to just manually create it in the lambda handler whenever a new session is created:
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'])更多推荐
在Python中添加会话属性以实现Alexa技能
发布评论