最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

首先Alexa的技能

运维笔记admin11浏览0评论

首先Alexa的技能

首先Alexa的技能

我想开发使用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();
};
回答如下:

尝试在行内编辑器这个功能为您的第一个技能。并尝试打开随机餐厅发电机进行测试,

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论