从意图中获取Alexa Slot值

时间:2016-01-19 21:39:51

标签: json node.js alexa alexa-skills-kit alexa-skill

我正在努力构建一个Alexa技能,并且在将插槽值从intent对象中取出时遇到了障碍。意图对象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 个答案:

答案 0 :(得分:17)

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

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

答案 1 :(得分:11)

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

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

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

var movieSlot = intent.slots.Movie.value;

答案 2 :(得分:0)

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

contact

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

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

'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 }, 是对象包含来自Alexa的完整JSON请求。因此,您可以使用slotname访问任何插槽 例如:如果我们要访问this,则可以使用subject slot

答案 3 :(得分:0)

自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