【发布时间】:2019-01-16 18:10:05
【问题描述】:
我想从 Lambda 函数调用 api。我的处理程序由包含两个必需插槽的意图触发。因此我事先不知道我是否会返回一个Dialog.Delegate 指令或我对api 请求的响应。在调用意图处理程序时如何保证这些返回值?
这是我的处理程序:
const FlightDelayIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'MyIntent';
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
if (request.dialogState != "COMPLETED"){
return handlerInput.responseBuilder
.addDelegateDirective(request.intent)
.getResponse();
} else {
// Make asynchronous api call, wait for the response and return.
var query = 'someTestStringFromASlot';
httpGet(query, (result) => {
return handlerInput.responseBuilder
.speak('I found something' + result)
.reprompt('test')
.withSimpleCard('Hello World', 'test')
.getResponse();
});
}
},
};
这是我发出请求的辅助函数:
const https = require('https');
function httpGet(query, callback) {
var options = {
host: 'myHost',
path: 'someTestPath/' + query,
method: 'GET',
headers: {
'theId': 'myId'
}
};
var req = https.request(options, res => {
res.setEncoding('utf8');
var responseString = "";
//accept incoming data asynchronously
res.on('data', chunk => {
responseString = responseString + chunk;
});
//return the data when streaming is complete
res.on('end', () => {
console.log('==> Answering: ');
callback(responseString);
});
});
req.end();
}
所以我怀疑我将不得不使用 Promise 并在我的句柄函数前面放置一个“异步”?我对这一切都很陌生,所以我不知道这意味着什么,特别是考虑到我有两个不同的返回值,一个是直接返回值,另一个是延迟返回值。我将如何解决这个问题?
提前谢谢你。
【问题讨论】:
标签: node.js aws-lambda alexa alexa-skills-kit alexa-slot