alexa-samples/skill-sample-nodejs-decision-tree

I'm having trouble treading the decision tree code in index.js

Blummer92 opened this issue · 4 comments

I was looking specifically at the helper function and I didn't understand what the helper functions were or how to customize them.

`function getSlotValues(filledSlots) {
const slotValues = {};

console.log(The filled slots: ${JSON.stringify(filledSlots)});
Object.keys(filledSlots).forEach((item) => {
const name = filledSlots[item].name;

if (filledSlots[item] &&
  filledSlots[item].resolutions &&
  filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
  filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
  filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
  switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
    case 'ER_SUCCESS_MATCH':
      slotValues[name] = {
        synonym: filledSlots[item].value,
        resolved: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
        isValidated: true,
      };
      break;
    case 'ER_SUCCESS_NO_MATCH':
      slotValues[name] = {
        synonym: filledSlots[item].value,
        resolved: filledSlots[item].value,
        isValidated: false,
      };
      break;
    default:
      break;
  }
} else {
  slotValues[name] = {
    synonym: filledSlots[item].value,
    resolved: filledSlots[item].value,
    isValidated: false,
  };
}

}, this);

return slotValues;
}
`

Hi @Blummer92 - the helper functions are 'generic' functions which handle some routine tasks that aren't specific to the code's business logic, instead they help with routine tasks that could apply to multiple skills. The SDK has some now built-in (referred to as utility functions) such isNewSession(), getSlotValue(), etc. In an ideal situation, you wouldn't need to customize them, just use them.

In this case, the helper getSlotValues function parses the request json, and creates an object attributes for each slot accessible by name. Each slot's attribute's value is an object with a synonym attribute, a resolved attribute and an isValidated attribute. When using this helper function, you don't have to write the code to check the schema, etc. This version may not be a good fit if you are using the id value in entity resolution or dynamic entities. (Nothing to say you couldn't modify it.) If neither of these apply, then you could use this.

Here's how each property works:
isValidated is a boolean indicating if the value spoken matched a value in your voice model.
synonym is the value spoken
resolved is the canonical (or reference value) from your model if there was a match, or the spoken value again if there wasn't a match. If you aren't using entity resolution, then synonym and resolved values are what was spoken.

@franklin-lobb could you show me where you found this, please

Utility function details are in the SDK docs: https://developer.amazon.com/docs/alexa-skills-kit-sdk-for-nodejs/utilities.html

The rest is based on my experience working with the code. I haven't seen any documentation per se.

If the SDK utility functions meet your use, I'd suggest using them.

aszk commented

Closing due to inactivity.