在SDK v2上使用Alexa和Lambda进行HTTP请求,如何使其有效? [英] HTTP request with Alexa and Lambda on SDK v2, how to make it works?
问题描述
我正在使用Amazon提供的ASK SDK v2,以使其能够熟练使用Alexa,但我遇到了体系结构问题:
I am playing with ASK SDK v2 provided by Amazon in order to make Skill for Alexa but I face an architectural problem :
首先,HTTP请求有效有点像魅力,但我想仅当我的HTTP请求完成时才返回speach响应,但我什至不知道是否有可能,因为 handle函数应该返回某些内容(查看注释):
First of all, the HTTP request works like a charm but I would like to return speach response if and only if my HTTP request is complete but I don't even know if it's possible because of the "handle" function that should return something (look at comments) :
const MyIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest' || (request.type === 'IntentRequest' && request.intent.name === 'MyIntent');
},
handle(handlerInput) {
var options = {
host: 'http://foo.com',
port: 80,
path: '/mypath',
method: 'GET'
};
var req = http.request(options, function(result){
result.on("end", function(){
//I would like to return speak here like that :
//return handlerInput.responseBuilder.speak("It works").withSimpleCard("MyTestApp", "It works").getResponse()
})
});
req.end();
//And I would like to remove this line to manage response in result.on("end", function(){}) above
return handlerInput.responseBuilder.speak("It works").withSimpleCard("MyTestApp", "It works").getResponse();
},
};
有什么想法要解决吗?
推荐答案
我找到了制作它的官方方法:
I found the official way to make it :
1)创建一种管理http请求并返回诺言的新功能:
1) Create a new funtion that manage http request and return a promise :
function httpGet(options) {
return new Promise(((resolve, reject) => {
const request = http.request(options, (response) => {
response.setEncoding('utf8');
let returnData = '';
if (response.statusCode < 200 || response.statusCode >= 300) {
return reject(new Error(`${response.statusCode}: ${response.req.getHeader('host')} ${response.req.path}`));
}
response.on('data', (chunk) => {
returnData += chunk;
});
response.on('end', () => {
resolve(JSON.parse(returnData));
});
response.on('error', (error) => {
reject(error);
});
});
request.on('error', function (error) {
reject(error);
});
request.end();
}));
}
2)在Intent中,返回承诺,您将在其中调用httpGet函数:
2) In the Intent, return a promise in which you call your httpGet function :
const MyIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest' || (request.type === 'IntentRequest' && request.intent.name === 'MyIntent');
},
handle(handlerInput) {
var options = {
host: 'http://foo.com',
port: 80,
path: '/mypath',
method: 'GET'
};
return new Promise((resolve, reject) => {
httpGet(options).then((response) => {
resolve(handlerInput.responseBuilder.speak("It is done.").getResponse());
}).catch((error) => {
resolve(handlerInput.responseBuilder.speak('Thor is not available at the moment. Please try again later or contact your administrator.')
.getResponse());
});
});
},
};
这是正确执行此操作的方法。我的示例基于 alexa petmatch示例。
This is the way to do it properly. My example is base on alexa petmatch sample.
这篇关于在SDK v2上使用Alexa和Lambda进行HTTP请求,如何使其有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!