Dynamic use of Alexa Presentation Language

1.2k Views Asked by At

I'm trying to use Alexa Presentation Language. I want to know how to incorporate dynamic strings (like Output Speech and Title) in node.js (Binding to be specific).

If I use some static strings for outputSpeech and put it in apl_template_export.json, then the skill functions properly and I can see output in Device Display. But when I try to use binding, the skill fails. Although there is no error, but I can't see any output in Device Display either (See Image). enter image description here

Here is what I've been trying so far:

handlerInput.responseBuilder
            .addDirective({
                  type: 'Alexa.Presentation.APL.RenderDocument',
                  version: '1.0',
                  document: require('./apl_template_export.json'),
                  dataSources: {
                    "bodyTemplate1Data": {
                        "type": "object",
                        "objectId": "bt1Sample",
                        "title": urlParams.type,
                        "textContent": {
                            "primaryText": {
                                "type": "PlainText",
                                "text": outputSpeech
                            }
                        }
                    }
                  }
              })
            .speak(outputSpeech)
            .getResponse();

apl_template_export.json:

{
    "type": "APL",
    "version": "1.0",
    "import": [
        {
            "name": "alexa-layouts",
            "version": "1.0.0"
        }
    ],
    "mainTemplate": {
        "parameters": [
            "payload"
        ],
        "items": [
            {
                "type": "Text",
                "text": "${dataSources.bodyTemplate1Data.textContent.primaryText.text}"
            }
        ]
    }
}

If I replace ${dataSources.bodyTemplate1Data.textContent.primaryText.text} with actual text (like "Hello World"), the skill works as intended.

I took reference from here and here, original repository: https://github.com/alexa-labs/skill-sample-nodejs-level-up-riddles

Can anyone tell me what is going wrong here?

UPDATE

I changed the text variable to this:

"items": [
            {
                "type": "Text",
                "text": "Type: ${type}\nDatasources: ${dataSources != null} \nBodyTemplate: ${dataSources.bodyTemplate1Data != null}"
            }
        ]

And I'm getting this as output:

Type: undefined
Datasources: false 
BodyTemplate: false 

So the problem is not in rendering the output, instead the template is not able to load the dataSources, that is the actual problem.

It is not even able to load type variable whose value is already defined in the template.

1

There are 1 best solutions below

0
On BEST ANSWER

If you're generating a template from APL Authoring Tool, then here are the points to remember:

  1. While adding a directive to your responseBuilder, make sure that you add a key named datasources.
  2. When referencing a datasource from your template, make sure that you address it as payload. So your datasource is referenced as payload.

For eg:

datasources : {
    'type': 'AlexaHeader',
    'text': {
        'value': 'Hello World'
    }
}

If you want to access value then reference it as ${payload.text.value}.

  1. If these doesn't work, then re-enable APL from your console.