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

Use Alexa Presentation Language with the ASK SDK v2 for Node.js

This document shows an example of how you can use APL directives with a skill that uses the ASK SDK v2 for Node.js. This example just displays "Hello World" on the screen.

Before you update your code, configure your skill to support the APL interface.

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.

Copied to clipboard.

{
  "type": "APL",
  "version": "1.2",
  "description": "A simple hello world APL document.",
  "settings": {},
  "theme": "dark",
  "import": [],
  "resources": [],
  "styles": {},
  "onMount": [],
  "graphics": {},
  "commands": {},
  "layouts": {},
  "mainTemplate": {
    "parameters": [
      "payload"
    ],
    "items": [
      {
        "type": "Text",
        "height": "100vh",
        "textAlign": "center",
        "textAlignVertical": "center",
        "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();
  },
};