Ti ringraziamo per la visita. Questa pagina è per il momento disponibile solo in inglese.

Add APL Support to Your Skills that Use the Node.js 2.0 SDK

If you have created an Alexa skill using the Node.js v2.0 SDK, you can update your skill to support APL. For general information on adding APL support to your skill, see Add APL Support to Your Skill for general information on adding support for APL to your skill.

The following example uses a simple function for a "Hello World" skill. Adapt it for your skill.

  • Start with a LaunchHandler function, as follows.
const LaunchHandler = (input) => {
    const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!';
    const repromptText = 'Try to say hello!';

    return input.responseBuilder
      .speak(speechText)
      .reprompt(repromptText)
      .getResponse();
  },
};
  • Next, create an APL template for the screen display, and save it in a file called main.json.
{
  "type": "APL",
  "version": "1.1",
  "import": [
    {
      "name": "alexa-layouts",
      "version": "1.1.0"
    }
  ],
  "mainTemplate": {
    "parameters": [
      "payload"
    ],
    "items": [
      {
        "type": "Text",
        "text": "Hello World"
      }
    ]
  }
}
  • Finally, modify the original function so that its response includes the APL template. To do so, add an Alexa.Presentation.APL.RenderDocument directive by calling the addDirective function on the responseBuilder. To include the JSON content you copied from the authoring tool, use the Node.js require() function to import this JSON content as part of the APLTemplate document.

This function has been modified to include APL in its response.

const LaunchHandler = (input) => {
    const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!';
    const repromptText = 'Try to say hello!';

    return input.responseBuilder
            .speak(speechText)
            .reprompt(repromptText)
            .addDirective({
                type: 'Alexa.Presentation.APL.RenderDocument',
                token: '[SkillProvidedToken]',
                version: '1.0',
                document: myDocument,
                datasources: {}
            })
            .getResponse();
  },
};