您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中添加会话属性以实现Alexa技能

在Python中添加会话属性以实现Alexa技能

sessionAttributes在我看来,用Python添加最简单的方法似乎是使用字典。例如,如果要在会话属性中存储某些插槽以备将来使用:

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:{…属性:{},…}开头。在Echo上启动会话时,Session根本没有属性键。

解决此问题的方法是,每当创建新会话时,都在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'])
python 2022/1/1 18:34:22 有222人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶