在Alexa Skill中创建纸牌阵列并显示它们

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

[我正在尝试使用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?

node.js alexa alexa-skill
1个回答
1
投票

[withSimpleCard]在这里无济于事,因为它仅返回一个元素,并且您要显示一个列表。

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

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

并且为了使事情更容易-从sample显示指令alexa-cookbook(他们在存储库中进行了一些重构,文章中的链接导航到404页)。

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