2017-01-20 33 views
0

我一直在努力的技能,我使用的登錄與亞馬遜帳戶鏈接,所以我可以抓住用戶的電子郵件地址和名稱使用我的技能。我正在做一些類似於scoreKeeper示例的事情,使用eventHandlers.js和storage.js將項目保存到數據庫。在eventHandlers.onLaunch我成功獲得來自亞馬遜的配置文件名和電子郵件地址,我將它保存到session.attributes這樣的:Alexa技能包,麻煩獲取會話屬性持續

 var profile = JSON.parse(body); 
       speechOutput="Hello, " + profile.name.split(" ")[0] + "."; 
       var sessionAttributes = {}; 
       sessionAttributes = { name: profile.name, email: profile.email }; 
       session.attributes = sessionAttributes; 
       console.log("Name in session:", session.attributes.name); 

控制檯日誌顯示的名字,所以我知道它被保存在session.attributes,但是當我嘗試訪問我的storage.js或intentHandlers.js中的session.attributes時,它顯示爲空。我錯過了什麼?提前致謝。這一直讓我瘋狂。

回答

0

我加入會話到response.ask功能的回調這方面的工作,像這樣:

ask: function (speechOutput, repromptSpeech, _session) { 
     this._context.succeed(buildSpeechletResponse({ 
      session: _session, 
      output: speechOutput, 
      reprompt: repromptSpeech, 
      shouldEndSession: false 
     })); 
    }, 

這屆會議:this._session保存會話,但似乎沒有給爲我工作。把它作爲一個變量來做。

0

比方說,你想從用戶那裏獲取城市名稱和以後能夠訪問Alexa的會話值:

1)預定義鍵/在JSON響應值對:

def build_response(session_attributes, speechlet_response): 
return { 
    'version': '1.0', 
    'sessionAttributes': { 
     "session_attributes": { 
      'NameOfCity': city_name, 
     } 
}, 
    'response': speechlet_response 
} 

2)在全球範圍內,變量聲明爲空字符串:

city_name = "", 

3)當你在一個函數中更新該變量,請確保您引用variab像全球一樣。例如:

def set_city_name_in_session(intent, session): 

card_title = intent['name'] 
session_attributes = {} 
should_end_session = False 

if 'CityIntent' in intent['slots']: 
    global city_name 
    city_name = intent['slots']['CityIntent']['value'] 

4)當創建需要訪問該變量的函數,傳遞全局變量作爲自變量到函數然後,該函數內,露出像這樣所有會話密鑰/對值:

def access_variables(intent, session, city_name): 

card_title = intent['name'] 
session_attributes = {} 
should_end_session = False 
exposed_attributes = session['attributes']['session_attributes'] 
city_name_attribute_value = exposed_attributes.get('NameOfCity') 

if (some_kind_of_condition): 
    do something awesome with city_name_attribute_value 

要測試,請將示例話語輸入到Service Simulator中,然後檢查值是否在您的Lambda JSON響應中持久化。