动态使用 Alexa 表示语言

Dynamic use of Alexa Presentation Language

我正在尝试使用 Alexa Presentation Language. I want to know how to incorporate dynamic strings (like Output Speech and Title) in node.js (Binding 来具体说明。

如果我为 outputSpeech 使用一些静态字符串并将其放入 apl_template_export.json,那么该技能将正常运行并且我可以在设备显示中看到输出。但是当我尝试使用 binding 时,该技能失败了。虽然没有错误,但我也看不到设备显示中的任何输出(见图)。

这是我到目前为止一直在尝试的方法:

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}"
            }
        ]
    }
}

如果我将 ${dataSources.bodyTemplate1Data.textContent.primaryText.text} 替换为实际文本(如 "Hello World"),该技能将按预期工作。

我参考了here and here, original repository: https://github.com/alexa-labs/skill-sample-nodejs-level-up-riddles

谁能告诉我这里出了什么问题?

更新

我将 text 变量更改为:

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

我得到这个作为输出:

Type: undefined
Datasources: false 
BodyTemplate: false 

所以问题不在于呈现输出,而是 模板无法加载 dataSources,这才是真正的问题。

甚至无法加载 type 值已在模板中定义的变量。

如果您要从 APL Authoring Tool 生成 template,请记住以下几点:

  1. 在将 directive 添加到 responseBuilder 时,请确保添加名为 datasourceskey
  2. 从您的模板中引用 datasource 时,请确保将其命名为 payload。所以你的 datasource 被引用为 payload.

例如:

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

如果您想访问 value,则将其引用为 ${payload.text.value}

  1. 如果这些不起作用,请从您的控制台重新启用 APL。