当前位置 : 主页 > 网络编程 > 其它编程 >

如何在Alexa技能中使用特定于会话的变量

来源:互联网 收集:自由互联 发布时间:2023-07-02
ImdevelopingafactskillforAlexausingtheirSpaceGeektemplate.Thetemplateitselfisveryst I'm developing a fact skill for Alexa using their SpaceGeek template. The template itself is very straight forward, but I'm trying to improve it by making su
ImdevelopingafactskillforAlexausingtheirSpaceGeektemplate.Thetemplateitselfisveryst

I'm developing a fact skill for Alexa using their SpaceGeek template. The template itself is very straight forward, but I'm trying to improve it by making sure the facts used will not come up again in the same session. So I delete the element after it is used. However, now it comes to a problem that, those elements deleted in the session won't even come up in future sessions. So I assume the global variable stays in the backend and thus created a copy-array as below. But it still won't work. So after using all the facts once, I'll always get "That's all the facts we have for now". Even if I start a new session. Any help will be appreciated.

我正在使用他们的SpaceGeek模板为Alexa开发一个事实技能。模板本身非常直接,但我试图通过确保所使用的事实不会在同一会话中再次出现来改进它。所以我在使用后删除了元素。但是,现在遇到的问题是,会话中删除的那些元素甚至不会出现在将来的会话中。所以我假设全局变量保留在后端,因此创建了一个副本数组,如下所示。但它仍然无法奏效。因此,在使用了所有事实之后,我将永远得到“这就是我们现在拥有的所有事实”。即使我开始新的会议。任何帮助将不胜感激。

function handleNewFactRequest(response) { var COPY_FACTS= SOME_FACTS.splice(0); if(COPY_FACTS.length>0){ var factIndex = Math.floor(Math.random() * COPY_FACTS.length); var fact = COPY_FACTS[factIndex]; // Create speech output var speechOutput = "Here's your random fact: " + fact + " would you like more?"; var repromptOutput = "would you like more random facts?"; COPY_FACTS.splice(factIndex, 1); response.ask(speechOutput, repromptOutput); }else{ var speechOutput = "That's all the facts we have for now."; response.tell(speechOutput); } }

2 个解决方案

#1

3

The correct way to handle this is to store your array as a session variable, rather than as a global object. An example which shows how to do this in detail would be the History Buff example skill, but generally speaking, the process is as follows:

处理此问题的正确方法是将数组存储为会话变量,而不是全局对象。显示如何详细执行此操作的示例将是History Buff示例技能,但一般而言,该过程如下:

When handling the user's first request, create an object which contains any variables you want to maintain throughout the session and assign it to session.attributes. You'd want to store your array as a property on that object.

处理用户的第一个请求时,创建一个对象,该对象包含要在整个会话期间维护的任何变量,并将其分配给session.attributes。您希望将数组存储为该对象的属性。

Then, in future event handlers, you will be able to access those stored session-specific variables (i.e. your array) as properties of that session.attributes object.

然后,在将来的事件处理程序中,您将能够访问那些存储的特定于会话的变量(即您的数组)作为该session.attributes对象的属性。

In the linked example, in subsequent intent handlers they include the snippet sessiOnAttributes= session.attributes to provide a more convenient handle to access those variables.

在链接的示例中,在后续的intent处理程序中,它们包含snippet sessiOnAttributes= session.attributes,以提供访问这些变量的更方便的句柄。

#2

-2

Instead of using splice(0) to duplicate the array, use slice() .

不使用splice(0)复制数组,而是使用slice()。

The splice() function changes the original array, where as slice() does not. See Sirko's response on this question.

splice()函数更改原始数组,而slice()则不会。请参阅Sirko对此问题的回答。

【本文由:大丰网站制作 http://www.1234xp.com/dafeng.html 复制请保留原URL】
上一篇:nodeJS之TCP模块net
下一篇:没有了
网友评论