30 seconds and beyond

Keep your APL document and skill session running past the 30-second limit.

Alexander Martin, Jan 15, 2022 2 min read

Keep your APL document on the screen and your skill session alive

With two components, you can keep your APL document and skill session running past the 30-second limit. Here's how:

Step 1: Keep the APL document visible

First you need a video component and a video (with a minimum duration of 1 second) in your APL document that runs forever.

{
  "type": "Video",
  "position": "absolute",
  "width": 1,
  "height": 1,
  "top": -100,
  "left": -100,
  "source": [{
    "url" : "https://alexaskills.dev/noop.mp4",
    "repeatCount": -1
  }],
  "autoplay": true
}

With repeatCount -1 we define that our document should be repeated forever.

The video must be at least 1x1px, therefore we place it with position absolute outside the visible area. It is also important to set the autoplay property on the video component to true, otherwise we would have to start the video manually in the onMount handler.

The video shown in my sample snippet has a playing time of 1 minute and can be downloaded here.

Learn more about the APL Video component

Step 2: Keep the skill session alive

To keep our skill session alive, we play ping-pong between our APL document and our skill backend. For this we use a tick event handler and send an event to our skill backend every 15 seconds.

You can add the tick handler either to your previously created video component or directly to your APL document.

"handleTick": [{
  "minimumDelay": 15000,
  "commands": [{
    "type": "SendEvent",
    "sequencer": "ping",
    "arguments": ["PING"]
  }]
}]

Learn more about tick event handlers

Now add an intent handler to your skill backend

import {
  getRequest,
  getRequestType,
} from 'ask-sdk-core';

const UserEventPingHandler = {
  canHandle({ requestEnvelope }) {
    if (getRequestType(requestEnvelope) === 'Alexa.Presentation.APL.UserEvent') {
      const request = getRequest(requestEnvelope);
      const [arg0] = request.arguments;

      return arg0 === 'PING';
    }

    return false;
  },

  async handle({ responseBuilder }) {
    return responseBuilder.withShouldEndSession(undefined).getResponse();
  },
};

export default UserEventPingHandler;

We simply send an empty response to the PING request. Do not forget to register your handler.

With "withShouldEndSession(undefined)" we prevent the microphone from being opened on the device.

Learn more about the lifecycle of a skill session

import UserEventPingHandler from './handlers/UserEventPingHandler';

const skill = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        UserEventPingHandler
    )
    .create()

Well, that's it. Have fun keeping your APL document and skill session alive! If you liked the post, leave a comment and ❤️.

Comments

Be the first to comment.