node.js 从意图中获取 Alexa Slot 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34887451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:47:32  来源:igfitidea点击:

Getting an Alexa Slot value out of an intent

jsonnode.jsalexaalexa-skills-kitalexa-skill

提问by Bill L

I'm working to build an Alexa skill and have run into a roadblock on getting my slot values out of the intent object. The intent object JSON looks like so:

我正在努力构建 Alexa 技能,但在从意图对象中获取我的槽值时遇到了障碍。意图对象 JSON 如下所示:

"intent": {
    "name": "string",
    "slots": {
      "string": {
        "name": "string",
        "value": "string"
      }
   }
}

My question is what will that first "string" value be to identify the slots? The documentation has this:

我的问题是第一个“字符串”值是什么来标识插槽?文档有这个:

A map of key-value pairs that further describes what the user meant based on a predefined intent schema. The map can be empty.

    The key is a string that describes the name of the slot. Type: string.
    The value is an object of type slot. Type: object. See Slot Object.

Does this mean the key to get the slot is the name I set in the Interaction Model? The only reason I'm hesitant to think that is because we already have a name object in the slot, which is definitely the name of the slot -- so if the way to access a specific slot was via the name, the name parameter would be redundant (you already know the name from accessing the slot.)

这是否意味着获得插槽的关键是我在交互模型中设置的名称?我犹豫不决的唯一原因是因为我们已经在插槽中有一个名称对象,这绝对是插槽的名称——所以如果访问特定插槽的方式是通过名称,则名称参数将是多余的(您已经通过访问插槽知道了名称。)

Does anyone know how I go about accessing individual slot values in an Alexa skill?

有谁知道我如何访问 Alexa 技能中的各个插槽值?

I'm using NodeJS for this skill by the way.

顺便说一下,我正在使用 NodeJS 来完成这项技能。

回答by Thomas Kekeisen

Since I had a lot of trouble with this, a more concrete answer (now) is:

由于我在这方面遇到了很多麻烦,更具体的答案(现在)是:

'PersonIntent': function () {
    var text = 'Hello ' + this.event.request.intent.slots.FirstPerson.value;

    this.emit(':tell', text);
},

With this intent schema:

有了这个意图架构:

{
    "intents": [
        {
            "intent": "PersonIntent",
            "slots": [
                {
                    "name": "FirstPerson",
                    "type": "AMAZON.DE_FIRST_NAME"
                }
            ]
        }
    ]
}

回答by Octavio

This is an example of an intent with your structure:

这是您的结构意图的示例:

"intent": {
    "name": "MovieIntent",
    "slots": {
     "string": {
        "name": "Movie",
        "value": "Titanic"
     }
  }
}

So, this means that the Intent you are using is named: Movie Intent (the one you set in your Interaction Model). And you will get the value this way:

因此,这意味着您使用的 Intent 被命名为:Movie Intent(您在交互模型中设置的那个)。您将通过以下方式获得价值:

var movieSlot = intent.slots.Movie.value;

回答by navule

As of Jul, 2019: You can access it in your intent handler as follows

截至 2019 年 7 月:您可以在您的意图处理程序中访问它,如下所示

if (handlerInput.requestEnvelope.request.intent.slots.SLOTNAME === undefined) {
    text = 'Hello! SLOTNAME not identified';
} else {
    text = 'Hello!, SLOTNAME identified: ' + handlerInput.requestEnvelope.request.intent.slots.SLOTNAME.value;
}

Replace SLOTNAMEwith your slot name. It is also very important that undefinedis just as it is as an object, and not undefined

替换SLOTNAME为您的插槽名称。同样非常重要的undefined是,就像作为一个对象一样,而不是undefined

回答by Suneet Patil

Lets assume we have a intent with name create meeting request in interaction model.

让我们假设我们有一个意图,在交互模型中创建会议请求。

"intents": [
    {
        "name": "CreateMeetingRequest",
        "slots": [
            {
                "name": "meetingdate",
                "type": "AMAZON.DATE",
                "samples": [
                    "{meetingdate} at {meetingTime} for {duration}",
                    "{meetingdate} at {meetingTime}",
                    "{meetingdate} and {meetingTime}",
                    "{meetingdate}"
                ]
            },
            {
                "name": "meetingTime",
                "type": "AMAZON.TIME",
                "samples": [
                    "{meetingTime} for {duration}",
                    "{meetingTime}"
                ]
            },
            {
                "name": "duration",
                "type": "AMAZON.DURATION",
                "samples": [
                    "{duration}"
                ]
            },
            {
                "name": "subject",
                "type": "AMAZON.SearchQuery",
                "samples": [
                    "{subject}"
                ]
            },
            {
                "name": "toName",
                "type": "AMAZON.US_FIRST_NAME",
                "samples": [
                    "{toName}"
                ]
            }
        ],
        "samples": [
            "meeting invite",
            "set up meeting",
            "setup meeting",
            "{meetingdate} at {meetingTime}",
            "create meeting with {toName}",
            "schedule a meeting with {toName} on {meetingdate} at {meetingTime} for {duration}",
            "Setup meeting with {toName}",
            "create meeting",
            "Setup meeting on {meetingdate} at {meetingTime}",
            "Setup new meeting",
            "create meeting request"
        ]
    }
]

And you wanna access it in lambda code. You can use below lines.

你想在 lambda 代码中访问它。您可以使用以下几行。

'CreateMeetingRequest': function () {
    const toName = (this.event.request.intent.slots.toName.value ? this.event.request.intent.slots.toName.value.toLowerCase() : null);
    const subject = (this.event.request.intent.slots.subject.value ? this.event.request.intent.slots.subject.value.toLowerCase() : null);
    const meetingdate = (this.event.request.intent.slots.meetingdate.value ? this.event.request.intent.slots.meetingdate.value : null);
    const meetingTime = (this.event.request.intent.slots.meetingTime.value ? this.event.request.intent.slots.meetingTime.value : null);
    const duration = (this.event.request.intent.slots.duration.value ? this.event.request.intent.slots.duration.value : null);

    console.log("toName:",toName,", meetingdate:",meetingdate,", meetingTime:", meetingTime);
    //your business logic
},

thisis the Object contains complete JSON request from alexa. So you can access any slot by using slotname Eg: if we want to access subject slotthen we can use this.event.request.intent.slots.subject.value

this是包含来自 alexa 的完整 JSON 请求的对象。所以你可以通过使用 slotname 来访问任何插槽 例如:如果我们想访问subject slot那么我们可以使用this.event.request.intent.slots.subject.value