[英]Adding session attributes in Python for Alexa skills
我有3個插槽( account
, dollar_value
, recipient_first
我對於Alexa的技術意圖模式中),我想保存在會話屬性的揚聲器提供任何插槽。
我使用以下方法來設置會話屬性:
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
被覆蓋,如下例所示:
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
)被提供如下(我的猜測是, the create_dollar_value_attribute
在第二種方法中,如果語句覆蓋第一個):
{
"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
添加值?
在我看來,使用 Python 添加sessionAttributes
的最簡單方法似乎是使用字典。 例如,如果您想在會話屬性中存儲一些插槽以備將來使用:
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:{ ... Attributes:{}, ... } 當會話在 Echo 上開始時,Session 根本沒有 Attributes 鍵。
我解決這個問題的方法是在創建新會話時在 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'])
首先,您必須定義 session_attributes。
session_attributes = {}
然后而不是使用
session_attributes = create_recipient_first_attribute(recipient_first)
你應該使用
session_attributes.update(create_recipient_first_attribute(recipient_first))
。
您面臨的問題是因為您正在重新分配 session_attributes。 取而代之的是,您應該只更新 session_attributes。
所以你的最終代碼將變成:
session_attributes = {}
if session.get('attributes', {}) and "recipient_first" not in session.get('attributes', {}):
recipient_first = intent['slots']['recipient_first']['value']
session_attributes.update(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.update(create_dollar_value_attribute(dollar_value))
ASK SDK for Python提供了一個屬性管理器,用於管理技能中的請求/會話/持久化級別屬性。 您可以查看顏色選擇器示例,了解如何在技能開發中使用這些屬性。
看看下面的內容:
account = intent['slots']['account']['value']
dollar_value = intent['slots']['dollar_value']['value']
recipient_first = intent['slots']['recipient_first']['value']
# put your data in a dictionary
attributes = {
'account':account,
'dollar_value':dollar_value,
'recipient_first':recipient_first
}
將屬性字典放在響應中的'sessionAttributes'中。 一旦 Alexa 回復您,您應該在“sessionAttributes”中取回它。
希望這可以幫助。
以下代碼片段還將防止覆蓋會話屬性:
session_attributes = session.get('attributes', {})
if "recipient_first" not in session_attributes:
session_attributes['recipient_first'] = intent['slots']['recipient_first']['value']
if "dollar_value" not in session_attributes:
session_attributes['dollar_value'] = = intent['slots']['dollar_value']['value']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.