问题
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:
"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?
I'm using NodeJS for this skill by the way.
回答1:
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"
}
]
}
]
}
回答2:
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:
var movieSlot = intent.slots.Movie.value;
回答3:
As of Jul, 2019: You can access it in your intent handler as follows
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 SLOTNAME
with your slot name.
It is also very important that undefined
is just as it is as an object, and not undefined
回答4:
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.
'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
is the Object contains complete JSON request from alexa. So you can access any slot by using slotname
Eg: if we want to access subject slot
then we can use this.event.request.intent.slots.subject.value
来源:https://stackoverflow.com/questions/34887451/getting-an-alexa-slot-value-out-of-an-intent