ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

在Python中为Alexa技能添加会话属性

2019-10-06 04:55:49  阅读:370  来源: 互联网

标签:alexa alexa-skill python json aws-lambda


我在我的意图架构中有一个Alexa技能的3个插槽(account,dollar_value,recipient_first),我想在会话属性中保存扬声器提供的任何插槽.

我使用以下方法来设置会话属性:

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_value和recipient_first)的语音输入如下(我的猜测是第二个if语句中的create_dollar_value_attribute方法覆盖了第一个):

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

解决方法:

在我看来,用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不会覆盖最后一个sessionAttribute.它只会创建一个新的键值对.

请注意,这可能在服务模拟器中运行良好,但在实际的Amazon Echo上进行测试时可能会返回键属性错误.根据这个post,

在服务模拟器上,会话以Session开始:{… Attributes:{},…}
当会话在Echo上开始时,Session根本没有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'])

标签:alexa,alexa-skill,python,json,aws-lambda
来源: https://codeday.me/bug/20191006/1858493.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有