gpt4 book ai didi

alexa - 从 Lambda 函数调用 REST API

转载 作者:行者123 更新时间:2023-12-04 16:01:28 30 4
gpt4 key购买 nike

我正在尝试构建一种调用 REST API 来获取数据的技能。我正在使用 HelloWorld 示例并对其进行修改以满足我的需要。我正在使用 Request 节点 (node.js) 发出请求。

但是,对于我来说,我无法让它工作。我在日志中看到函数被调用并且返回了正确的结果,但是发送给 Alexa 的响应是空的!!知道我错过了什么吗?

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
console.log("HelloWorldIntentHandler 1: ");
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {
console.log("HelloWorldIntentHandler 2");
var speechText = 'Hello World';

Request.get(url, function(error, response, body) {
console.log("I'm here")
var data = JSON.parse(body)
var result = data.records.totalNum
if (result > 0) {
speechText = "There are " + result + " matches";
} else {
speechText = "ERROR";
}

return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
},
};

日志中的错误是

Error handled: speechOutput.trim is not a function

最佳答案

我能够使用 Axios 而不是 Request 来实现它。

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
console.log("HelloWorldIntentHandler 1: ");
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
async handle(handlerInput) {
console.log("HelloWorldIntentHandler 2");
var speechText = 'default';

try {
const response = await Axios.get(url);
var result = response.data.totalRecs;
if (result > 0) {
speechText = "There are " + result + " matches";
} else {
speechText = "ERROR";
}
console.log("text=" + speechText);
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
} catch (error) {
console.log(error);
}
},
};

关于alexa - 从 Lambda 函数调用 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50357042/

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