Alexa Custom Slot Type: No value in intent

Korgen picture Korgen · Mar 10, 2017 · Viewed 8.3k times · Source

I've already posted this question to the amazon developer forum but don't receive an answer there. I guess Stackoverflow should've been the first choice from the beginning:

From my understanding if I use a Custom Slot Type even if the list of its possible values does not contain the spoken word the spoken word is still passed to the function. The documentation says

A custom slot type is not the equivalent of an enumeration. Values outside the list may still be returned if recognized by the spoken language understanding system.

Now I have a Custom Slot Type LIST_OF_PERSONS with values Matthias|Max and an utterance of

EmployeeDetailsIntent {Person} 

If I call this intend with a value not in LIST_OF_PERSONS the Intent still gets called but the JSON does not contain a "value" key for the Slot:

"request": {
    "type": "IntentRequest",
    "requestId": "EdwRequestId.a943e233-0713-4ea5-beba-d9287edb6083",
    "locale": "de-DE",
    "timestamp": "2017-03-09T14:38:29Z",
    "intent": {
      "name": "EmployeeDetailsIntent",
      "slots": {
        "Person": {
          "name": "Person"
        }
      }
    }
  }

Is this "works as designed" or a bug? How do I access the spoken word in the Intent then? As this.event.request.intent.slots.Person.value is undefined?

My code lives in AWS lambda and I'm using the nodejs alexa-sdk Version 1.0.7. The language of my Skill is German.

Answer

Atlas7 picture Atlas7 · Jun 21, 2017

(disclaimer: this post summarises my own "workaround". It might or might not be the "best way". Seems to have worked for me so thought I would share / document it here briefly)

I've recently bumped into similar issues for an utterance that looks like this:

"tell me about {townName}"

If I say "tell me about London", it works.

If I say "tell me about" (deliberately missing a {townName}), the program "dies" (and returns a JSON looking similar to your one, with undefined this.event.request.intent.slots.townName.value)

Though I'm not 100% sure whether this is meant to be a "feature" (i.e. we need to write smarter code to work around this) or "problem" (i.e. Alexa team needs to address or fix). This scenario has caused a real issue when it came to the certification process for me recently.

To get through this, I've implemented a workaround (or fix, whatever you call it) to avoid Alexa from "dying" as a result of this edge case.

From the Alexa skill-sample-nodejs-trivia index.js file, I've found a snippet function that helped me work around this (I've edited it a bit for my example for simplicity):

function isAnswerSlotValid(intent) {
    var answerSlotFilled = intent && intent.slots &&
        intent.slots.townName && intent.slots.townName.value;
    return answerSlotFilled
}

(i.e. this function returns True for valid values for the slot townName and and False for undefined / otherwise).

When it comes to defining the intent, I could use this function to "get around" an empty slot value scenario:

var startHandlers = Alexa.CreateStateHandler(states.START,{

  // bla bla bla//

  "AnswerIntent": function() {  

    // handel missing slot value
    var answerSlotValid = isAnswerSlotValid(this.event.request.intent);

    if (answerSlotValid && moreConditions) {
      // do something fun
    }
    else {
      // handle empty slot scenario
    }
  }

  // bla bla bla//

}

Would be interested to see if there are better / more "proper" solutions to this to handle empty / undefined slots more elegantly.