简体   繁体   English

alexa第一技能

[英]First alexa skill

I am trying to develop my first Alexa skill using Node.js, and every time I try to test it I get "There was a problem with the requested skill's response".我正在尝试使用 Node.js 开发我的第一个 Alexa 技能,每次我尝试对其进行测试时,我都会收到“请求的技能响应出现问题”。

I am trying create a random restaurant generator.我正在尝试创建一个随机餐厅生成器。 Pretty simple its an array of restaurants, a random index is selected, and Alexa says the restaurant.非常简单,它是一系列餐厅,选择了一个随机索引,Alexa 说餐厅。 I don't know where I went wrong I have uploaded my .json and .js files if anyone can help i'd really appreciate it.我不知道我哪里出错了我已经上传了我的 .json 和 .js 文件,如果有人可以帮助我真的很感激。

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

Thank you谢谢

Try this function in inline editor for your first skill. 在内联编辑器中尝试此功能以获取第一个技能。 and try to test with open random restaurant generator , 并尝试使用开放式随机餐厅生成器进行测试,

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

I've been using lambda for two years and it's terrible to debug and deploy for me until I started to use aws cloud9. 我已经使用lambda两年了,在我开始使用AWS cloud9之前,为我调试和部署是很糟糕的。

I suggest that you use aws cloud9 which is a cloud IDE for writing, running and debugging code. 我建议您使用aws cloud9,这是用于编写,运行和调试代码的云IDE。 You could run the lambda function as local environment. 您可以将lambda函数作为本地环境运行。

Check their website to get more information. 检查他们的网站以获取更多信息。 It's time consuming, but totally worth it, especially if you want to develop an Alexa skill. 这很耗时,但完全值得,特别是如果您想发展Alexa技能。

Most of the times you get that error for 2 things: 在大多数情况下,您会因为两件事而收到该错误:

  1. You don't have the trigger "Alexa Skill Kit" in your lambda function. 您的lambda函数中没有触发器“ Alexa Skill Kit”。 If you don't have it, you can add one in the designer tab of the configuration of the lambda function. 如果没有,则可以在lambda函数的配置的设计器选项卡中添加一个。

  2. You don't have the neccesary modules in your lambda function. 您的lambda函数中没有必要的模块。 You can add them locally with "npm install ask-sdk-core" and then upload the folder. 您可以使用“ npm install ask-sdk-core”在本地添加它们,然后上传文件夹。

Try this way to render responses. 尝试这种方式来呈现响应。

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

Use this way:使用这种方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM