通过 API 调用创建的 APL 数据

问题描述 投票:0回答:1

我使用 Alexa Skills Kit 从 api 获取数据,在其中我获取类别 {name、id 和 imgurl} 以及该类别中的视频(name、id 和thumbnailImg),然后我获取视频本身。

我正在尝试设计一个 APL,其中数据使用从 api 调用获取的类别名称和图像 url,但我什么也没得到。

这是我的实际代码视图:

{
  "type": "APL",
  "version": "1.6",
  "mainTemplate": {
    "items": [
      {
        "type": "Pager",
        "id": "myPager",
        "data": "${payload.imageListData.listItems}",
        "navigation": "wrap",
        "items": {
          "type": "Container",
          "items": [
            {
              "type": "Image",
              "source": "${data.imageSource}",
              "scale": "best-fill",
              "width": "100%",
              "height": "100%"
            },
            {
              "type": "Text",
              "text": "${ordinal(data.index + 1)}. ${data.primaryText}",
              "fontSize": "20dp",
              "color": "white",
              "position": "absolute",
              "bottom": "10%",
              "left": "5%"
            }
          ]
        }
      }
    ]
  }
}

只有一个 .在预览中

这是我的index.js代码:

/* *
 * This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
 * Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
 * session persistence, api calls, and more.
 * */
const api = require('./api.js');
const Alexa = require("ask-sdk-core");
const Country = require('./country.js');
const DOCUMENT_ID = "carousel";
const fetchData = async () => {
    return await api.getAllCountry();
}
const createDirectivePayload = (aplDocumentId, dataSources = {}, tokenId = "documentToken") => {
    return {
        type: "Alexa.Presentation.APL.RenderDocument",
        token: tokenId,
        document: {
            type: "Link",
            src: "doc://alexa/apl/documents/" + aplDocumentId
        },
        datasources: dataSources
    }
};

// function addOrUpdateSlotValues(handlerInput, slotType, values) {
//     const directive = {
//         type: 'Dialog.UpdateDynamicEntities',
//         updateBehavior: 'REPLACE',
//         types: [
//             {
//                 name: slotType,
//                 values: values.map(value => ({ id: value, name: { value } }))
//             }
//         ]
//     };

//     handlerInput.responseBuilder.addDirective(directive);
// }

const updateCategories = (handlerInput, categories) => {
    const directive = {
        type: 'Dialog.UpdateDynamicEntities',
        updateBehavior: 'REPLACE',
        types: [
            {
                name: 'Categories',
                values: categories.map(category => ({ id: category.primaryText, name: { value: category.primaryText } }))
            }
        ]
    };
    handlerInput.responseBuilder.addDirective(directive);
};

const SampleAPLRequestHandler = {
    canHandle(handlerInput) {
        // handle named intent
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'category';
    },
    async handle(handlerInput) {
        const countryData = await fetchData();
        const datasource = {
            "imageListData": {
                "type": "object",
                "objectId": "imageListSample",
                "backgroundImage": {
                    "contentDescription": null,
                    "smallSourceUrl": null,
                    "largeSourceUrl": null,
                    "sources": [
                        {
                            "url": "",
                            "size": "large"
                        }
                    ]
                },
                "title": "Categorías",
                "listItems": countryData.map(country => ({
                    'primaryText': country.name,
                    'imageSource': country.image_url,
                })),
                "logoUrl": "",
                "hintText": "Prueba: \"Alexa, selecciona el número 1\""
            }
        }
        updateCategories(handlerInput, datasource.listItems);
        if (Alexa.getSupportedInterfaces(handlerInput.requestEnvelope)['Alexa.Presentation.APL']) {
            const aplDirective = createDirectivePayload(DOCUMENT_ID, datasource);
            handlerInput.responseBuilder.addDirective(aplDirective);
        }
        return handlerInput.responseBuilder.getResponse();
    }
};

exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(SampleAPLRequestHandler)
    .lambda();

我对数据进行了硬编码并显示了出来,但是当我尝试使用 api 中的数据时,没有显示任何内容。

alexa-skills-kit
1个回答
0
投票

可能没有任何区别,但我发现你的 mainTemplate 文档中没有“参数”声明。

"mainTemplate": {
    "parameters": [
      "payload"
    ],

首先尝试,但如果仍然损坏,则注销生成的 APL 数据源对象并将其 + APL 文档粘贴到 apl.ninja 之类的内容中,然后查看 APL 是否正确呈现。

© www.soinside.com 2019 - 2024. All rights reserved.