我如何解决Alexa停止,取消意图不起作用的问题

时间:2018-11-05 04:37:24

标签: alexa

我正在使用大量的脚本,因为我不知道编码是如何诚实的,并且大部分情况下它都可以工作,但是我在欢迎讲话中努力停止,帮助和取消,一次停止/取消工作正常您将进入下一部分,但是在欢迎辞中,示例会放大

  

用户:“ Alexa开放布雷克兰天气”   技能:“欢迎来到布雷克兰天气。请向我询问天气,以下提示,天气如何或当前情况如何。”   用户:“停止” /用户:“取消” /用户:“帮助”   技能:“要求的技能响应出现问题”

有意图,我加了话以防万一,但还是一样,所以我觉得代码中有错误,可能大部分是大声笑,请你们中的一个在闲暇时细读一下,看看是否可以发光一点?

exports.handler = (event, context, callback) => {
    try {
        //console.log(`event.session.application.applicationId=${event.session.application.applicationId}`);
        
        //if (event.session.application.applicationId !== APP_ID) {
        //     callback('Invalid Application ID');
        //}
        

        if (event.session.new) {
            onSessionStarted({ requestId: event.request.requestId }, event.session);
        }

        if (event.request.type === 'LaunchRequest') {
            onLaunch(event.request,
                event.session, (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        } else if (event.request.type === 'IntentRequest') {
            onIntent(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        } else if (event.request.type === 'SessionEndedRequest') {
            onSessionEnded(event.request, event.session);
            callback();
        }
    } catch (err) {
        callback(err);
    }
};


const isDebug = false; 
const APP_ID = 'amzn1.ask.skill.d31840b5-27f2-4f07-9e19-ec0c73d78b39';
const url_weather = 'http://www.brecklandweather.com/currentout.txt';
const APP_NAME = 'Breckland Weather';
const STR_REPROMPT = '\nPlease ask me for the weather, heres a hint, whats the weather like or whats the current conditions.';
const HELP_MESSAGE = 'You can say whats the weather like, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';


function getWeather(intent, session, callback, numLetters) {
    


    let speechOutput = ''; 
    let cardTitle = 'Weather output';
    getWebRequest(url_weather, function webResonseCallback(err, data) {
        if (err) {            
            speechOutput = `DOH! somethings done gone bad, self destuct in 3 2 ah, false alarm, still broken though, carry on.`;
            callback({}, buildSpeechletResponse(cardTitle, speechOutput, STR_REPROMPT));     
        } else {
            //if (isDebug) {"SolveAnagram::data = " + console.log(data)};
            speechOutput = data;            
            callback({}, buildSpeechletResponse(cardTitle, speechOutput,  STR_REPROMPT));
        }    
    });
    
    
}

//Simple welcome intent handler
function getWelcomeResponse(callback) {
    console.log("START session");
    if (isDebug) {console.log("getWelcomeResponse()")}
        
    const cardTitle = APP_NAME;
    const speechOutput = 'Welcome to '+APP_NAME+'. '+ STR_REPROMPT;
        
    // 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 = STR_REPROMPT;
    const shouldEndSession = false;

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


//My effort of code

const handlers = {
    'LaunchRequest': function () {
        // this.emit('myIntent');
        this.emit(':tell', '2 Hello, what would you like to do?');
    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_REPROMPT;

        this.response.speak(speechOutput).listen(reprompt);
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
};





//Simple end session intent handler
function handleSessionEndRequest(callback) {
    console.log("END session");
    
    const cardTitle = 'Goodbye';
    const speechOutput = 'Thanks for using '+APP_NAME+'.';
    const shouldEndSession = false;

    callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}




function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    if (isDebug) {console.log(`buildSpeechletResponse(title:${title}, shouldEndSession:${shouldEndSession}, reprompt:${repromptText})`)}
    return {
        outputSpeech: {
            type: 'PlainText',
            text: output,
        },
        card: {
            type: 'Simple',
            title: `${title}`,
            content: `${output}`,
        },
        reprompt: {
            outputSpeech: {
                type: 'PlainText',
                text: repromptText,
            },
        },
        shouldEndSession,
    };
}

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


//----------------- Web service helper ----------------------//
var http = require('http');
 
function getWebRequest(url,doWebRequestCallBack) {
    try
    {
        http.get(url, function (res) {
            var webResponseString = '';
            if (isDebug) {console.log('Status Code: ' + res.statusCode)}
     
            if (res.statusCode != 200) {
                doWebRequestCallBack(new Error("Non 200 Response"));
                return;
            }
     
            res.on('data', function (data) {
                webResponseString += data;
            });
     
            res.on('end', function () {
                //if (isDebug) {console.log('getWebRequest::Got some data: '+ webResponseString)};     
                
                //the weather isn't JSON so just return the string
                //var webResponseObject = JSON.parse(webResponseString);   
                doWebRequestCallBack(null, webResponseString);
                
                
            });
        }).on('error', function (e) {
            if (isDebug) {console.log("Communications error: " + e.message)}
            doWebRequestCallBack(new Error(e.message));
        });
    } 
    catch(err)
    {
        doWebRequestCallBack(new Error(err.message));
    }
}



// --------------- Events -----------------------

/**
 * Called when the session starts.
 */
function onSessionStarted(sessionStartedRequest, session) {
    //console.log(`onSessionStarted requestId=${sessionStartedRequest.requestId}, sessionId=${session.sessionId}`);
}

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


/**
 * Called when the user specifies an intent for this skill.
 */
function onIntent(intentRequest, session, callback) {
    
    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;
    
    console.log("  ");
    console.log("== New Intent ==");
    console.log(`onIntent(${intentName})`);

    if (intentName === 'GetWeather') {
        getWeather(intent, session, callback,1); 
    } 
    
}

/**
 * Called when the user ends the session.
 * Is not called when the skill returns shouldEndSession=true.
 */
function onSessionEnded(sessionEndedRequest, session) {
    //console.log(`onSessionEnded requestId=${sessionEndedRequest.requestId}, sessionId=${session.sessionId}`);
    // Add cleanup logic here
}

1 个答案:

答案 0 :(得分:0)

您的设计模式非常混乱。但是我知道你的问题在哪里。您根本没有使用handlers对象来处理意图请求。根据您的设计模式,您可以进行如下更改:

/**
 * Called when the user specifies an intent for this skill.
 */
function onIntent(intentRequest, session, callback) {

    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;

    console.log("  ");
    console.log("== New Intent ==");
    console.log(`onIntent(${intentName})`);

    if (intentName === 'GetWeather') {
        getWeather(intent, session, callback,1); 
    }else if(intentName === 'AMAZON.StopIntent'){
        getStopResponse(intent, session, callback,1)
    }else if(intentName === 'AMAZON.HelpIntent'){
        getHelpResponse(intent, session, callback,1)
    }

}

响应构建器的功能如下:

//Simple welcome intent handler
function getStopResponse(callback) {
    console.log("START session");
    if (isDebug) {console.log("getStopResponse()")}

    const cardTitle = APP_NAME;
    const speechOutput = STOP_MESSAGE
    const repromptText = STOP_MESSAGE;
    const shouldEndSession = true;

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

类似的人会去getHelpResponse()。我会留给你。

告诉我是否有帮助。