Alexa技能套件,无法使会话属性持久化

编程入门 行业动态 更新时间:2024-10-09 23:16:11
本文介绍了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技能套件,无法使会话属性持久化

本文发布于:2023-11-28 02:59:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:套件   持久   属性   技能   Alexa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!