一个函数,如果代码只在开头工作

时间:2017-01-10 18:10:57

标签: node.js aws-lambda mqtt

var AWS = require('aws-sdk');
    var iotdata = new AWS.IotData({
        endpoint: 'endpoint'
    });
    exports.handler = function (event, context) {
        var params = {
        topic: 'topic',
        payload: 'payload',
        qos: 0
        };
        try {
            console.log("event.session.application.applicationId=" + event.session.application.applicationId);
            if (event.session.new) {
               console.log("NEW SESSION");
            }
            switch (event.request.type){ 
                case "LaunchRequest": 
                console.log(`LAUNCH REQUEST`);
                iotdata.publish(params, function(err, data){
                    if(err){
                        console.log(err);
                    }
                    else{
                        console.log("!!!!!!!!");
                            context.succeed(
                                generateResponse(
                                    buildSpeechletResponse("Text1", true),
                                    {}
                            ));
                    }   
                });
                break;

            case "IntentRequest":
               console.log(`INTENT REQUEST`);
                if (event.request.intent.name==="Intent"){
                var number2 = event.request.intent.slots.Number.value;
                NumberS = NumberSwitch(number2);
                payLoad = 'payload';
                params={
                    topic: 'topic',
                    payload: payLoad,
                    qos: 0
                };
                iotdata.publish(params, function(err, data){
                    if(err){
                        console.log(err);
                    }
                    else{
                        console.log("success");
                            context.succeed(
                                generateResponse(
                                    buildSpeechletResponse("Text2", true),
                                    {}
                            ));
                    }   
                });
            }else{throw "Invalid intent";}  
            break;

            case "SessionEndedRequest":
            console.log(`SESSION ENDED REQUEST`);
            break;

            default:
            context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);
        }

    } catch (e) {
        context.fail("Exception: " + e);
    }
};

启动会话的执行结果是

  

“version”:“1.0”,“sessionAttributes”:{},“response”:{       “outputSpeech”:{         “type”:“PlainText”,         “text”:“Text1”

"shouldEndSession": true

但是我在json文件上写了意图名称 执行结果是

  

“errorMessage”:“例外:无效意图”

我的问题是iot.publish功能仅在启动请求中起作用的原因 并且在意图请求中不起作用????

有什么解释吗?

0 个答案:

没有答案