第一个亚历克萨技能



我正在尝试使用Node.js开发我的第一个Alexa技能,每次我尝试测试它时,我都会得到"请求技能的响应有问题"。

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

索引.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": []
        }
    }
}

谢谢

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

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

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

/**
 * 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);
    }
};

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

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

查看他们的网站以获取更多信息。这很耗时,但完全值得,特别是如果你想发展Alexa技能。

大多数情况下,您会收到以下两件事的错误:

  1. 您的 lambda 函数中没有触发器"Alexa 技能工具包"。如果没有,可以在 lambda 函数配置的设计器选项卡中添加一个。

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

使用这种方式:

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

最新更新