【问题标题】:First alexa skill第一个alexa技能
【发布时间】:2019-05-25 20:11:50
【问题描述】:

我正在尝试使用 Node.js 开发我的第一个 Alexa 技能,每次我尝试对其进行测试时,我都会收到“请求的技能响应有问题”。

我正在尝试创建一个随机餐厅生成器。很简单,它是一系列餐厅,选择了一个随机索引,Alexa 说的是餐厅。我不知道我哪里出错了我已经上传了我的 .json 和 .js 文件,如果有人可以帮助我真的很感激。

index.js:

const Alexa = require('alexa-sdk');

const APP_ID = 'amzn1.ask.skill.9350e65b-fb41-48ce-9930-98b5156eb63c';

const handlers = {
  'LaunchRequest': function () {
    this.emit('randomRestaurantGeneratorIntent');
  },
  'randomRestaurantGeneratorIntent': function () {
    var randomResturant;
    var foodArray = ['IHOP', 'Dennys', 'burger king'];
    randomResturant = foodArray[Math.floor(Math.random() * foodArray.length)];
     
    
    this.response.speak(randomResturant);
    this.emit(':responseReady');
  },
  'AMAZON.HelpIntent': function () {
    const say = 'You can say what did I learn, or, you can say exit... How can I help you?';

    this.response.speak(say).listen(say);
    this.emit(':responseReady');
  },
  'AMAZON.CancelIntent': function () {
    this.response.speak('Bye!');
    this.emit(':responseReady');
  },
  'AMAZON.StopIntent': function () {
    this.response.speak('Bye!');
    this.emit(':responseReady');
  }
 
};

exports.handler = function (event, context, callback) {
  const alexa = Alexa.handler(event, context, callback);
  alexa.APP_ID = APP_ID;
  alexa.registerHandlers(handlers);
  alexa.execute();
};

randomResturantGeneratorIntent.JSON:

{
    "interactionModel": {
        "languageModel": {
            "invocationName": "random restaurant generator",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                },
                {
                    "name": "randomRestaurantGeneratorIntent",
                    "slots": [],
                    "samples": [
                        "Launch Random Restaurant Generator "
                    ]
                }
            ],
            "types": []
        }
    }
}

谢谢

【问题讨论】:

  • 嘿马里奥,拿了你的代码 JSON 和 Index.js 并且它可以工作,所以我的建议是退后一步,确保在你压缩和上传到 AWS lambda 的文件夹中它包含包含您的 alexa-sdk 库的 node_modules 文件夹。我还建议将 lambda 中的运行时设置为节点 6.10
  • 我没有上传它 AWS 我正在在内联编辑器中编写代码。有关当前项目结构,请参阅imgur.com/a/KT7T0M3 屏幕截图。谢谢
  • Mario,查看您的屏幕截图,您显然在该路径中没有一个名为 node_modules 的文件夹,该文件夹将包含 alexa-sdk 节点库,这是您的 index.js 文件的第一个要求 const Alexa = require( 'alexa-sdk');您的代码将无法按您希望的方式运行,除非它具有此功能。
  • 嗯,有道理。这是我可以手动添加这些文件的任何方式吗?我在 IDE 中工作。或者你能给我指个教程吗?谢谢

标签: node.js amazon-web-services alexa alexa-skill alexa-app


【解决方案1】:

在内联编辑器中尝试此功能以获得您的第一个技能。并尝试使用打开随机餐厅生成器进行测试,

/**
 * Called when the user launches the skill without specifying what they want.
 */
function onLaunch(launchRequest, session, callback) {
    console.log(`onLaunch requestId=${launchRequest.requestId}, sessionId=${session.sessionId}`);

    // Dispatch to your skill's launch.
    getWelcomeResponse(callback);
}


function buildResponse(sessionAttributes, speechletResponse) {
    return {
        version: '1.0',
        sessionAttributes,
        response: speechletResponse,
    };
}

function getWelcomeResponse(callback) {
    // If we wanted to initialize the session to have some attributes we could add those here.
    const sessionAttributes = {};
    const cardTitle = 'Welcome';
    const speechOutput = 'Welcome to Your First Alexa Skill';
    // If the user either does not reply to the welcome message or says something that is not
    // understood, they will be prompted again with this text.
    const repromptText = 'Please tell me What do you want to know?';
    const shouldEndSession = false;

    callback(sessionAttributes,
        buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: 'PlainText',
            text: output,
        },
        //For testing purpose only
        // card: {
        //     type: 'Simple',
        //     title: `SessionSpeechlet - ${title}`,
        //     content: `SessionSpeechlet - ${output}`,
        // },
        reprompt: {
            outputSpeech: {
                type: 'PlainText',
                text: repromptText,
            },
        },
        shouldEndSession,
    };
}

exports.handler = (event, context, callback) => {
    try {
        console.log(`event.session.application.applicationId=${event.session.application.applicationId}`);


        if (event.request.type === 'LaunchRequest') {
            onLaunch(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        }

    }
    catch (err) {
        callback(err);
    }
};

【讨论】:

    【解决方案2】:

    我已经使用 lambda 两年了,在我开始使用 aws cloud9 之前,为我调试和部署非常糟糕。

    我建议您使用 aws cloud9,它是一个用于编写、运行和调试代码的云 IDE。您可以将 lambda 函数作为本地环境运行。

    查看他们的website 以获取更多信息。这很耗时,但完全值得,尤其是如果您想开发 Alexa 技能。

    【讨论】:

    • 看起来他正在使用内联云 9。在这种情况下,aws-sdk 和其他模块已经包含在内。不需要额外的节点模块目录。我注意到在一处使用了 console.log。我已经使用它和创建的日志来研究我的回复内容以及我在代码中的位置。再洒一些,看看记录器——只是一些调试的想法。祝你好运。
    【解决方案3】:

    大多数时候你会因为两件事得到这个错误:

    1. 您的 lambda 函数中没有触发器“Alexa Skill Kit”。如果没有,可以在lambda函数的配置的设计器选项卡中添加一个。

    2. 您的 lambda 函数中没有必要的模块。您可以使用“npm install ask-sdk-core”在本地添加它们,然后上传文件夹。

    【讨论】:

      【解决方案4】:

      尝试以这种方式呈现响应。

      var speechOutput =  'Your response here';
      var reprompt = "How can I help?";
      this.response.speak(speechOutput);
      this.response.listen(reprompt);
      this.emit(":responseReady");
      

      【讨论】:

      • 请添加一些解释它是如何工作的或原始代码有什么问题。
      【解决方案5】:

      这样使用:

      var speechOutput =  'Your response here';
      var reprompt = "How can I help?";
      this.response.speak(speechOutput);
      this.response.listen(reprompt);
      this.emit(":responseReady");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-21
        • 1970-01-01
        • 1970-01-01
        • 2021-02-27
        • 2020-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多