Alexa技能套件,无法使会话属性持久化 [英] Alexa skills kit, trouble getting session attributes to persist

查看:103
本文介绍了Alexa技能套件,无法使会话属性持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一项技能,该技能正在使用通过亚马逊帐户登录链接,因此我可以获取要在我的技能中使用的用户电子邮件地址和名称。我正在执行类似于scoreKeeper示例的操作,使用eventHandlers.js和storage.js将项目保存到数据库。在eventHandlers.onLaunch中,我成功地从亚马逊获取了个人资料名称和电子邮件地址,并将其保存到session.attribute中,如下所示:

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

控制台日志显示该名称,因此我知道它已保存在session.attributes中,但是当我尝试访问storage.js或intentHandlers.js中的session.attributes,它显示为空。我想念什么?提前致谢。

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.

推荐答案

假设您要从用户获取城市名称并能够访问该值稍后在Alexa会话中:

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)在JSON响应中预定义键/值对:

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)在全局范围内,将变量声明为空字符串:

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

city_name = "",

3)每当您在函数中更新该变量时,请确保将该变量引用为全局变量。例如:

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)创建函数时需要访问此变量,将全局变量作为参数传递给函数,然后在函数内公开所有会话密钥/对值,如下所示:

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

T o测试,将样本话语输入到Service Simulator中,然后检查Lambda JSON响应中的值是否持久。

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

这篇关于Alexa技能套件,无法使会话属性持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆