【问题标题】:Dynamic use of Alexa Presentation Language动态使用 Alexa 演示语言
【发布时间】:2019-01-07 05:14:23
【问题描述】:

我正在尝试使用Alexa Presentation Language。我想知道如何在 node.js 中合并动态字符串(如 Output SpeechTitle)(具体来说是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”),该技能将按预期工作。

我参考了herehere,原始存储库: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 变量。

【问题讨论】:

  • 您能否捕获 cloudwatch 错误并将其发布在您的问题中?
  • 没有错误。我收到一个输出(见图)。但 Alexa 显示设备无法呈现它。
  • 请在日志中确认...获得输出并不意味着没有错误。我遇到了同样的错误,查看日志进一步帮助了我(输出可见但 ui 为空白)您是否也定义了 SessionEndedRequestHandler ?尝试评论它
  • 我检查了日志中的最后 5 个条目,没有错误。我确实定义了我的SessionEndedRequestHandler。但我认为这不是SessionEndedRequestHandler 的问题,因为我正在启动技能并终止它。 (shouldEndSessionFalse
  • @AmodGokhale 请查看问题中的更新。

标签: node.js alexa alexa-skill alexa-presentation-language


【解决方案1】:

如果您从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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多