2017-03-24 43 views
1

我在我的意圖模式中有3個插槽(account,dollar_value,,0 )用於Alexa技能,我想保存講話者在會話屬性中提供的任何插槽。在Python中爲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_valuerecipient_first)中提供的是如下語音輸入(我的猜測是,the create_dollar_value_attribute方法在第二if語句覆蓋第一):

{ 
    "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添加值?

回答

2

在我看來,用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上測試時可能會返回關鍵屬性錯誤。根據此post,

在服務模擬器上,會話以會話開始:{...屬性:{},...} 當會話在Echo上啓動時,會話根本沒有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']) 
+0

我沒有在'事件屬性[「會議」]'但是我不知道如何實例化這個'事件[「會議」] [」屬性'] = {}'會解決它? alexa/lambda如何知道? 'onSessionStarted'函數做了什麼? – Jonathan

0

首先,你必須定義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))