Adding session attributes in Python for Alexa skills

送分小仙女□ 提交于 2019-11-30 16:10:24

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

The implementation in this case:

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
    }

This creates the sessionAttributes in the recommended way in the Lambda response JSON.

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.

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,

On Service Simulator, sessions starts with Session:{ ... Attributes:{}, ... } When sessions start on the Echo, Session does not have an Attributes key at all.

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'])

First, you have to define the session_attributes.

session_attributes = {}

Then instead of using

session_attributes = create_recipient_first_attribute(recipient_first)

You should use

session_attributes.update(create_recipient_first_attribute(recipient_first)).

The problem you are facing is because you are reassigning the session_attributes. Instead of this, you should just update the session_attributes.

So your final code will become:

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

The ASK SDK for Python provides an attribute manager, to manage request/session/persistence level attributes in the skill. You can look at the color picker sample, to see how to use these attributes in skill development.

Take a look at the below:

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
}

Put the attributes dictionary in 'sessionAttributes' in your response. You should get it back in 'sessionAttributes' once Alexa replies to you.

Hope this helps.

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