首页 > 解决方案 > Alexa Skill 与 Cheerio

问题描述

您好,我正在尝试在 Alexa Skill 中使用cheerio 从网站获取数据并添加技能。意图代码

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
        && Alexa.getIntentName(handlerInput.requestEnvelope) === 'all_titles';
},
handle(handlerInput) {
    //Lógica para speak output
    var options = {
        uri: 'https://es.pagetest.com/',
        transform: function (body) {
            return cheerio.load(body);
        }
    };

    rp(options)
    .then(function ($) {
        var arr_response = []
        var titles = $('.ms-short-title');
        titles.each((i, a) =>{
            if(a.parent.attribs.title !== undefined)arr_response.push(a.parent.attribs.title);
        });

        const speakOutput = insert_in_string(arr_response);

        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
        function insert_in_string (arr_titulars){
            var string_text = '';
            for(var titular of arr_titulars){
                string_text += titular + ' Siguiente titular. ';
            }
            return string_text;
        }
    })
    .catch(function (err) {
        return err;
    });

}

};

我已经在本地测试了逻辑并且它工作正常,通过将它放在 alexa 代码编辑器中,在测试中,返回错误消息,但没有跟踪¿任何想法?谢谢

标签: alexacheerioalexa-skillalexa-voice-service

解决方案


如果您使用的是 Alexa 托管的技能,您将已经内置 CloudWatch 集成。只需转到您的 Amazon 开发人员控制台,导航到您的技能代码选项卡,滚动到底部并单击左下方的日志:Amazon CloudWatch链接。

现在,每次您发送时console.log,它都会被发送到 CloudWatch。所以,在你的catch处理程序中,添加console.log(err),你应该能够看到出了什么问题。

这篇博文也可能有帮助:https ://developer.amazon.com/blogs/alexa/post/71ac4c05-9e33-41d2-abbf-472ba66126cb/3-tips-to-troubleshoot-your-custom-alexa-skill-s -后端


推荐阅读