I am trying to build a skill that calls a REST API to get data. I am using the HelloWorld sample and modifying it to fit my need. I am using the Request node (node.js) to issue the request.
However, for the hell of me I can't get it to work. I see in the log that the function is called and the correct result is coming back, yet the response sent to Alexa is empty!! Any idea what I am missing?
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();
});
},
};
The error in the log is
Error handled: speechOutput.trim is not a function
I was able to get this to work using Axios instead of 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);
}
},
};