0

我試圖從一個Lambda函數發佈到IoT主題,這是從Alexa技能觸發的。在lambda函數,我這樣做是物聯網發佈:AWS:intentHandler.call()在Alexa技能模板中取消了前面的函數調用

var params = { 
    topic: 'testTopic', 
    payload: new Buffer('test message'), 
    qos: 1 
}; 

iotData.publish(params, function(err, data) { 
    if (err) console.log('ERR: ', err); // an error occurred 
    else if (data) console.log('DATA: ', data); // successful response 
}); 

如果我運行在一個lambda函數的代碼(只有該代碼),它工作正常。但是,如果我將參數和發佈函數放在Alexa Skill模板的onIntent函數中,並將其放入新的Lambda函數中,則不起作用。 (兩個Lambda函數都具有相同的配置和策略。)

如果我註釋掉intentHandler.call(),那麼iotData.publish()會運行,因此出現intentHandler調用取消它的原因是我不知道不明白。

onIntent: function (intentRequest, session, response) { 
    var intent = intentRequest.intent, 
     intentName = intentRequest.intent.name, 
     intentHandler = this.intentHandlers[intentName]; 

    if (intentHandler) { 
    console.log('dispatch intent = ' + intentName); 

    var params = { 
     topic: 'testTopic', 
     payload: new Buffer('test message'), 
     qos: 1 
    }; 

    iotData.publish(params, function(err, data) { 
     if (err) console.log('ERR: ', err); // an error occurred 
     else if (data) console.log('DATA: ', data); // successful response 
    }); 

    intentHandler.call(this, intent, session, response); 
    } else { 
    throw 'Unsupported intent = ' + intentName; 
    } 
} 

回答

0

這似乎與Lambda函數上下文有關。 intentHandler.call()稱爲有問題的意圖,而這又意味着context.succeed()。我假設這意味着lambda函數結束iotData.publish()調用之前運行的可被執行,因爲當我把intentHandler.call()在回調iotData.publish()這樣,一切工作正常:

onIntent: function (intentRequest, session, response) { 
    var intent = intentRequest.intent, 
     intentName = intentRequest.intent.name, 
     intentHandler = this.intentHandlers[intentName]; 

    if (intentHandler) { 
    console.log('dispatch intent = ' + intentName); 

    var params = { 
     topic: 'signals', 
     payload: new Buffer(intentName), 
     qos: 1 
    }; 

    iotData.publish(params, function(err, data) { 
     if (err) console.log(err, err.stack); // an error occurred 
     else { 
     console.log(data);   // successful response 
     intentHandler.call(this, intent, session, response); 
     }  
    }); 
    } else { 
    throw 'Unsupported intent = ' + intentName; 
    } 
}