gpt4 book ai didi

node.js - 使 Alexa Skill 通过 HTTP 正常工作从 JSON 文件说话

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:58 33 4
gpt4 key购买 nike

如果我调用 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

关于node.js - 使 Alexa Skill 通过 HTTP 正常工作从 JSON 文件说话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54719241/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com