在 Alexa Skill 中创建一系列卡片并展示它们

Create an array of cards in Alexa Skill and show them

我正在尝试用 SimpleCard () 列出我的 API 收到的物品,但我在互联网上没有找到任何说这是可能的。

我设法做的是 Alexa 说出我找到的所有项目。

遵循我的代码。

const ProcuraProdutoIntentHandler = {
canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
        && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ProcuraProduto';
},
async handle(handlerInput) {
    let speakOutput = "This is the default message.";
    let totalResult = 0;
    let slotValue = handlerInput.requestEnvelope.request.intent.slots.produto.value;
    const titleCard = `Procurando por ${slotValue}, aguarde...`
    let names = '';
    await axios.get(`https://myapi.com/search=${slotValue}`).then((response) =>{
        if(response.data.length === 0){
            totalResult = `Não encontrei nenhum resultado.`
        //Se encontrar apenas 1 resultado
        } else if(response.data.length === 1){
            response.data.map(item => {
              names += item.name;
            });
            console.log("Produtos ", names);
            totalResult = `Eu encontrei ${response.data.length} resultado.`
        //Se encontrar mais de 1 resultado
        } else {
            response.data.map(item => {
              names += `${item.name} <break time="2s"/>, `;
            });
            console.log("Produtos ", names);
            totalResult = `Eu encontrei ${response.data.length} resultados.`
        }
    }).catch((erro) =>{
        speakOutput = 'Ocorreu um erro. Tente novamente.';
    })
    const speechText = `<speak> Os produtos que encontrei foram: <break time="1s"/> ${names}</speak>`;

    return handlerInput.responseBuilder
        .speak(speechText)
        .withSimpleCard('Pesquisa concluída!', totalResult)
        //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
        .getResponse();
}
};

您将如何循环为找到的每个产品创建一个 withSimpleCard?

withSimpleCard 不会在这里帮助你,因为它 returns 只是一个元素,而你想要显示一个列表。

相反,您应该使用 Display template 功能。确保满足文档中提到的所有限制。

您可以在 Alexa Blog 上的相关文章中找到更多信息。

为了让事情变得更简单 - 显示指令 sample from alexa-cookbook(他们对存储库进行了一些重构,文章中的链接导航到 404 页面)。