从 Lambda 函数调用 REST API
Calling a REST API from a Lambda function
我正在尝试构建一种调用 REST API 来获取数据的技能。我正在使用 HelloWorld 示例并对其进行修改以满足我的需要。我正在使用请求节点 (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
将你的 return
放在 if else 语句中。
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";
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
} else {
speechText = "ERROR";
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
}
}
这将强制您的处理程序return您的 if else 语句中的结果。
我能够使用 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);
}
},
};
对于它的价值,我 运行 遇到了同样的问题并发现(困难的方式)我从我的 api 调用返回的响应包含 SSML 的格式不正确(在我的例子中,返回的字符串包含一个“&”)。
所以我发现这个库似乎不仅有帮助,而且如果您不能 100% 确定结果,通常也是一个好主意。
参见:https://www.npmjs.com/package/ssml-builder
希望这对某人有所帮助。
~罗布
我想我最好也添加一个代码示例。这没有经过测试,但是使用我在那里提到的 lib 你的代码可能看起来像 ^^^。
const Speech = require( 'ssml-builder' );
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");
let speechText = 'Hello World';
Request.get(url, function(error, response, body) {
console.log("I'm here");
let data = JSON.parse(body);
let result = data.records.totalNum;
if (result > 0) {
let speech = new Speech();
speech.say(`There are ${result} matches`);
speechText = speech.ssml(true);
} else {
speechText = "ERROR";
}
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
},
};
我正在尝试构建一种调用 REST API 来获取数据的技能。我正在使用 HelloWorld 示例并对其进行修改以满足我的需要。我正在使用请求节点 (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
将你的 return
放在 if else 语句中。
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";
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
} else {
speechText = "ERROR";
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
}
}
这将强制您的处理程序return您的 if else 语句中的结果。
我能够使用 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);
}
},
};
对于它的价值,我 运行 遇到了同样的问题并发现(困难的方式)我从我的 api 调用返回的响应包含 SSML 的格式不正确(在我的例子中,返回的字符串包含一个“&”)。
所以我发现这个库似乎不仅有帮助,而且如果您不能 100% 确定结果,通常也是一个好主意。
参见:https://www.npmjs.com/package/ssml-builder
希望这对某人有所帮助。
~罗布
我想我最好也添加一个代码示例。这没有经过测试,但是使用我在那里提到的 lib 你的代码可能看起来像 ^^^。
const Speech = require( 'ssml-builder' );
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");
let speechText = 'Hello World';
Request.get(url, function(error, response, body) {
console.log("I'm here");
let data = JSON.parse(body);
let result = data.records.totalNum;
if (result > 0) {
let speech = new Speech();
speech.say(`There are ${result} matches`);
speechText = speech.ssml(true);
} else {
speechText = "ERROR";
}
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
},
};