6

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

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

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

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.

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

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

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

4

4 回答 4

18

由于我对此有很多麻烦,因此(现在)更具体的答案是:

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

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

使用此意图架构:

{
    "intents": [
        {
            "intent": "PersonIntent",
            "slots": [
                {
                    "name": "FirstPerson",
                    "type": "AMAZON.DE_FIRST_NAME"
                }
            ]
        }
    ]
}
于 2017-03-15T23:00:39.450 回答
12

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

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

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

var movieSlot = intent.slots.Movie.value;
于 2016-02-02T21:42:37.070 回答
5

截至 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;
}

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

于 2019-07-17T14:12:27.670 回答
0

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

"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"
        ]
    }
]

你想在 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
},

this是 Object 包含来自 alexa 的完整 JSON 请求。因此,您可以使用 slotname 访问任何插槽 例如:如果我们想访问,subject slot那么我们可以使用this.event.request.intent.slots.subject.value

于 2018-07-10T09:16:27.143 回答