Alexa API 技能 - nodejs 获取请求未执行
Alexa API skill - nodejs get request not executing
我正在处理我的第一个 Alexa skill,作为起点,我希望 Alexa 声明从简单的 GET 请求中检索到的数据(请参阅下面的 lambda 函数)。然而,出于某种原因,请求实际上似乎并未执行 - request.get() 内部没有任何内容打印到控制台,并且在处理程序执行后 speechOutput 为 'Outside Request'。我也不熟悉查看 CloudWatch 日志,无法找到有关网络请求的任何信息,甚至无法知道是否正在尝试这样做。欢迎任何帮助!
'use strict';
//Required node packages
const alexa = require('./node_modules/alexa-sdk');
const request = require('request');
// var https = require('https')
//this is the handler, when the lambda is invoked, this is whats called
exports.handler = function (event, context, callback) {
const skill = alexa.handler(event, context);
skill.appId = '<app_id>';
skill.registerHandlers(handlers);
skill.execute();
};
//Alexa handlers
const handlers = {
'LaunchRequest': function () {
console.log("inside of LaunchRequest");
const speechOutput = "Hello from NASA!";
this.response.speak(speechOutput).listen(speechOutput);
this.emit(':responseReady');
},
//Entering our main, part finding function
'GetAPOD': function () {
const intent_context= this
const speechOutput = getData()
intent_context.response.speak(speechOutput).listen(speechOutput);
intent_context.emit(':responseReady');
},
'Unhandled': function (){
console.log("inside of unhandled");
const speechOutput = "I didn't understand that. Please try again";
this.response.speak(speechOutput).listen(speechOutput);
this.emit(':responseReady');
}
};
const getData = function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
console.log("inside get data")
request.get(url, function (error, response, body) {
console.log("inside request")
console.log('error', error) //Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
return "complete request"
return body
});
return "outside request"
}
我过去发现这样的 API 请求会被破坏,因为它们不是同步的,就像 David 所说的那样。为了解决这个问题,我不得不将请求隐藏在一个承诺中以使其得到解决,在您的情况下类似于此:
更改您的函数以包含承诺:
function getData = function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
console.log("inside get data")
return new Promise(function(resolve, reject) {
request.get(url, function (error, response, body) {
if (err) {
reject(err);
}
if (body) {
resolve(JSON.parse(body));
}
});
});
}
然后更改您的意图处理程序以使用承诺:
//Entering our main, part finding function
'GetAPOD': function () {
getData()
.then(function(body) {
let speechOutput = body;
intent_context.response.speak(speechOutput).listen(speechOutput);
intent_context.emit(':responseReady');
}
类似的东西。您需要稍微尝试一下,以确保按照您的预期生成结果。希望这可以帮助。
D
我正在处理我的第一个 Alexa skill,作为起点,我希望 Alexa 声明从简单的 GET 请求中检索到的数据(请参阅下面的 lambda 函数)。然而,出于某种原因,请求实际上似乎并未执行 - request.get() 内部没有任何内容打印到控制台,并且在处理程序执行后 speechOutput 为 'Outside Request'。我也不熟悉查看 CloudWatch 日志,无法找到有关网络请求的任何信息,甚至无法知道是否正在尝试这样做。欢迎任何帮助!
'use strict';
//Required node packages
const alexa = require('./node_modules/alexa-sdk');
const request = require('request');
// var https = require('https')
//this is the handler, when the lambda is invoked, this is whats called
exports.handler = function (event, context, callback) {
const skill = alexa.handler(event, context);
skill.appId = '<app_id>';
skill.registerHandlers(handlers);
skill.execute();
};
//Alexa handlers
const handlers = {
'LaunchRequest': function () {
console.log("inside of LaunchRequest");
const speechOutput = "Hello from NASA!";
this.response.speak(speechOutput).listen(speechOutput);
this.emit(':responseReady');
},
//Entering our main, part finding function
'GetAPOD': function () {
const intent_context= this
const speechOutput = getData()
intent_context.response.speak(speechOutput).listen(speechOutput);
intent_context.emit(':responseReady');
},
'Unhandled': function (){
console.log("inside of unhandled");
const speechOutput = "I didn't understand that. Please try again";
this.response.speak(speechOutput).listen(speechOutput);
this.emit(':responseReady');
}
};
const getData = function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
console.log("inside get data")
request.get(url, function (error, response, body) {
console.log("inside request")
console.log('error', error) //Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
return "complete request"
return body
});
return "outside request"
}
我过去发现这样的 API 请求会被破坏,因为它们不是同步的,就像 David 所说的那样。为了解决这个问题,我不得不将请求隐藏在一个承诺中以使其得到解决,在您的情况下类似于此:
更改您的函数以包含承诺:
function getData = function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
console.log("inside get data")
return new Promise(function(resolve, reject) {
request.get(url, function (error, response, body) {
if (err) {
reject(err);
}
if (body) {
resolve(JSON.parse(body));
}
});
});
}
然后更改您的意图处理程序以使用承诺:
//Entering our main, part finding function
'GetAPOD': function () {
getData()
.then(function(body) {
let speechOutput = body;
intent_context.response.speak(speechOutput).listen(speechOutput);
intent_context.emit(':responseReady');
}
类似的东西。您需要稍微尝试一下,以确保按照您的预期生成结果。希望这可以帮助。 D