node.js - 通过 HTTP 使 Alexa Skill 从 JSON 文件中正常工作
问题描述
如果我调用 doHttp 函数,它会毫无问题地获取日志中的数据。我似乎无法让数据返回并允许说话。我正在使用使用 nodejs 的 Visual Studio 代码。我对此很陌生,所以我知道我错过了一些东西。
const url = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY";
const linkIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'linkIntent';
},
handle(handlerInput) {
var data = doHttp();
var speechText = data;
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Card title', speechText)
.getResponse();
},
};
function doHttp() {
var data2 = '';
https.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
data2 = JSON.parse(data).title;
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
return data2;
}
//Working function
function doHttp() {
https.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).title);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
解决方案
HTTP 请求是一个异步函数,您的代码不会等待响应的到来。您可以将 http 函数调用包装在一个 Promise 中并返回它。然后可以在句柄输入函数中应用 async/await。下面是使用天气 API 的示例代码。
const url =
"https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";
const linkIntentHandler = {
canHandle(handlerInput) {
return (
handlerInput.requestEnvelope.request.type === "IntentRequest" &&
handlerInput.requestEnvelope.request.intent.name === "linkIntent"
);
},
async handle(handlerInput) {
const data = await doHttp();
console.log("data in handle input ", data);
const speechText = data;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard(speechText, speechText)
.getResponse();
}
};
function doHttp() {
var data2 = "";
return new Promise((resolve, reject) => {
https
.get(url, resp => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", chunk => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
console.log("data ", data);
data2 = JSON.parse(data).weather[0].description;
console.log("weather ", data2);
resolve(data2);
});
})
.on("error", err => {
console.log("Error: " + err.message);
});
});
}
使用以下链接了解有关进行外部 API 调用的更多信息。 https://developer.amazon.com/blogs/alexa/post/4a46da08-d1b8-4d8e-9277-055307a9bf4a/alexa-skill-recipe-update-call-and-get-data-from-external-apis
推荐阅读
- mysql - 如何将表作为参数发送到 MySQL 中的存储过程?
- c++ - 编译器如何编译模板和类型检查?(c++)
- python - 找不到文件错误,但显示错误的文件内容
- python - AttributeError:在 geopandas 中绘图时,“PandasArray”对象没有属性“_str_startswith”
- c# - 为什么 PlayerPrefs 中没有减去数字?
- java - couchbase 服务器的内存问题(4.5.0-2601 社区版)
- javascript - 如何防止 Mouse4 单击向后导航?
- sql - 根据值从表中获取范围
- asp.net-core - 带有 ASP.NET Core 3.1 Razor 页面的 Microsoft 身份平台中的可疑错误
- c++ - 在 gcc windows api 字符串处理中启用 UNICODE