Alexa skills kit, trouble getting session attributes to persist

那年仲夏 提交于 2019-12-25 08:17:08

问题


I have been working on a skill where I am using Login With Amazon account linking so I can grab the user email address and name to use in my skill. I am doing something similar to the scoreKeeper sample, using the eventHandlers.js and the storage.js for saving items to a database. In the eventHandlers.onLaunch I am successfully getting the profile name and email address from Amazon and I save it to the session.attributes like this:

      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);

The console log shows the name so I know it is being saved in the session.attributes, but when I try to access the session.attributes in my storage.js or intentHandlers.js, it shows it as being empty. What am I missing? Thanks in advance. This has been driving me crazy.


回答1:


I got this working by adding session to the callback of the response.ask function, like this:

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

It had session: this._session for saving the session, but that didn't seem to be working for me. Passing it as a variable did.




回答2:


Let's say you want to obtain city name from the user and be able to access that value later in the Alexa session:

1) Pre-define your key/value pairs in the JSON response:

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

2) In global scope, declare the variable as an empty string:

city_name = "",

3) Whenever you update that variable within a function, make sure you reference that variable as global. For example:

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) When creating a function that needs to access this variable, pass the global variable as an argument into the function then, within the function, expose all session key/pair values like so:

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

To test, enter sample utterances into Service Simulator then check if values are persisting in your Lambda JSON response.



来源:https://stackoverflow.com/questions/41755696/alexa-skills-kit-trouble-getting-session-attributes-to-persist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!